#777777 on white looks fine. It's also a WCAG fail: 4.48:1, against a minimum of 4.5:1 for body text. One step darker on every channel, #767676, passes at 4.54:1.
You can't see that difference, so let's build something that can: a small React contrast checker with two inputs, a live ratio and pass/fail for each WCAG level. It's one file with no project. Along the way you'll get the contrast math and a component that needs no useEffect.
The math, before any React
WCAG defines contrast in terms of relative luminance, or how bright a colour looks to a human eye, from 0 (black) to 1 (white). Getting there takes three steps:
- Split the hex into red, green and blue, and scale each to 0–1.
- Undo the sRGB gamma curve, since stored values aren't linear light.
- Weight the channels. Green counts the most, because that's where our eyes are most sensitive.
type RGB = [number, number, number]; // "#rgb" or "#rrggbb" → [r, g, b], or null while you're still typing function parseHex(input: string): RGB | null { if (!/^#([0-9a-f]{3}|[0-9a-f]{6})$/i.test(input)) return null; let hex = input.slice(1); if (hex.length === 3) hex = hex.split("").map((c) => c + c).join(""); return [0, 2, 4].map((i) => parseInt(hex.slice(i, i + 2), 16)) as RGB; } // WCAG relative luminance: 0 for black, 1 for white function luminance(rgb: RGB): number { const [r, g, b] = rgb.map((v) => { const c = v / 255; return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; }); return 0.2126 * r + 0.7152 * g + 0.0722 * b; } function contrast(a: RGB, b: RGB): number { const [light, dark] = [luminance(a), luminance(b)].sort((x, y) => y - x); return (light + 0.05) / (dark + 0.05); } The ratio is the lighter luminance over the darker one, each plus 0.05, so it runs from 1:1 (same colour) to 21:1 (black on white).
If you've seen 0.03928 in the WCAG 2 text instead of 0.04045, that's fine too. No 8-bit channel value falls between the two, so the result is the same either way.
Check the numbers first
Before rendering anything, make sure the function agrees with the spec. Add three bare expressions under it:
contrast([255, 255, 255], [0, 0, 0]); // 21 contrast(parseHex("#777")!, parseHex("#fff")!); // 4.478… contrast(parseHex("#767676")!, parseHex("#fff")!); // 4.542… There's a trap here. Round 4.478 to one decimal and you get 4.5, which looks like a pass. Round for display only, and always compare the raw number.
The component
const checks = [ { label: "AA body text", min: 4.5 }, { label: "AA large text", min: 3 }, { label: "AAA body text", min: 7 }, ]; export default function ContrastChecker() { const [fg, setFg] = useState("#777777"); const [bg, setBg] = useState("#ffffff"); // Derived on every render. No useEffect, no extra state. const fgRgb = parseHex(fg); const bgRgb = parseHex(bg); const ratio = fgRgb && bgRgb ? contrast(fgRgb, bgRgb) : null; return ( <div style={{ fontFamily: "system-ui", padding: 24, maxWidth: 360 }}> <label> Text <input value={fg} onChange={(e) => setFg(e.target.value)} /> </label>{" "} <label> Background <input value={bg} onChange={(e) => setBg(e.target.value)} /> </label> {ratio === null ? ( <p>Type two hex colours, like #2f52c9.</p> ) : ( <> <p style={{ color: fg, background: bg, padding: 16, borderRadius: 8, fontSize: 18 }}> The quick brown fox jumps over the lazy dog. </p> <h2>{ratio.toFixed(2)}:1</h2> <ul> {checks.map(({ label, min }) => ( <li key={label}> {ratio >= min ? "pass" : "fail"}: {label} ({min}:1) </li> ))} </ul> </> )} </div> ); } Add import { useState } from "react"; at the top and the three code blocks together make one file.
Three decisions worth stealing:
- State holds the raw string, exactly as typed. On the way from
#2f52c9to#2f52c8, the input briefly holds#2f52c, which isn't a colour.parseHexreturnsnull, and the component shows a hint instead of breaking. - The ratio is derived, not stored. The tempting version keeps
ratioin state and updates it in auseEffect. That renders once with the old ratio, then again with the new one, and gives you two sources of truth. Computing it during render is one line and is always right. - The swatch only renders when both colours parse. The browser never receives half a colour.
Run it without a project
I ran this in JSLab, a JavaScript & TypeScript scratchpad for macOS. It's our own tool, so weigh the praise accordingly. The setup is the part a scaffold usually takes care of:
- Pick a browser runtime. A component needs a page. Click + in the tab bar and choose React, and JSLab sets the tab's language, runtime and framework, installs the packages and opens starter code. Paste the file over it. If you start from a plain tab instead, pick Browser in the runtime menu in the status bar, because the default Bun runtime has no page and so no Web View. Plain Browser is enough here, since nothing in this file needs Node.
- Render it. Default-export the component and JSLab renders it into the Web View tile beside the console. There's no
createRootand noindex.html. The Framework menu in the status bar shows that it found React from your imports. Ifreact-domisn't installed, the console names the package to install, and ⌘I opens the package sheet. - Watch it rerender. Auto Run runs the tab a moment after you stop typing. Change the default
"#777777"to"#767676", and both the ratio and the AA body text line in the Web View update. The threecontrast(...)lines show their values in the output, each next to the line that produced it.
One thing to know: every run reloads the page, so the DOM starts clean each time. Editing the code resets the inputs to their useState defaults. When you want a colour pair to stick around while you work on the component, make it the default.
Make it yours
- Add a swap button.
<button onClick={() => { setFg(bg); setBg(fg); }}>Swap</button>. Contrast is symmetric, so the ratio shouldn't move. That's a free test. - Accept
rgb(…)too. OnlyparseHexchanges. Everything downstream takes anRGB. - Check your own palette. Swap the inputs for an array of
[fg, bg]pairs from your design tokens, and render one row per pair.
The Web View, the Framework menu and the other frameworks it renders (Vue, Solid, Angular) are covered in the docs: https://js-lab.app/docs/web-view

