Sub topic 10
Checking contrast on generated colors
This chapter assumes you can write oklch() colors, use color-mix(), and store colors in custom properties. Here you will learn why a generated color can look right and still fail a contrast check, and how to test for that.
Why lightness is not a guarantee
In OKLCH, the L value tracks how light a color looks. That makes it tempting to say "any color with L above 90% is safe behind any color with L below 40%". The rule is a good first guess, but it is not a guarantee.
Accessibility rules do not measure L. WCAG 2 defines a contrast ratio from relative luminance, a different formula. Chroma and hue change the luminance of a color even when L stays the same, so two colors with equal L can have different contrast against white.
The WCAG 2 thresholds are:
| Content | Minimum ratio |
|---|---|
| Normal body text | 4.5:1 |
| Large text (about 24px, or 19px bold) | 3:1 |
| UI components and meaningful graphics | 3:1 |
For a neutral color (chroma 0), the relationship is simple. Luminance is L cubed, so white text on oklch(57% 0 0) is about 4.5:1. Once chroma rises, that estimate moves. Treat L as a way to pick candidates, and measure the result.
Generated colors need their own check
Every color-mix() or relative color result is a new color the browser computes at render time. Two cases cause most failures:
- Tints. Mixing a brand color with white raises
L, so dark text on the tint usually gets safer. White text on that tint gets much worse. - Hover and disabled states. Lowering
Lfor a hover state helps white text. Lowering alpha makes the background show through, so contrast depends on what is behind it.
Check every foreground and background pair that can appear together, including the hover pair.
Computing the ratio
Browser developer tools show a contrast ratio in the color picker. For checks you can repeat, a small script works better, because you can run it whenever a token changes. This Node script converts OKLCH to linear sRGB, then computes the WCAG 2 ratio. It clamps out-of-gamut channels, which approximates what a browser does. The result is close but not identical to the browser's gamut mapping.
function toLinearSrgb(l, c, hDeg) {
const h = (hDeg * Math.PI) / 180;
const a = c * Math.cos(h);
const b = c * Math.sin(h);
const l3 = (l + 0.3963377774 * a + 0.2158037573 * b) ** 3;
const m3 = (l - 0.1055613458 * a - 0.0638541728 * b) ** 3;
const s3 = (l - 0.0894841775 * a - 1.291485548 * b) ** 3;
return [
4.0767416621 * l3 - 3.3077115913 * m3 + 0.2309699292 * s3,
-1.2684380046 * l3 + 2.6097574011 * m3 - 0.3413193965 * s3,
-0.0041960863 * l3 - 0.7034186147 * m3 + 1.707614701 * s3,
].map((v) => Math.min(1, Math.max(0, v)));
}
function luminance([r, g, b]) {
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function contrast(fg, bg) {
const y1 = luminance(toLinearSrgb(...fg));
const y2 = luminance(toLinearSrgb(...bg));
const hi = Math.max(y1, y2);
const lo = Math.min(y1, y2);
return (hi + 0.05) / (lo + 0.05);
}
// Colors are [lightness 0-1, chroma, hue in degrees]
const white = [1, 0, 0];
const brand = [0.62, 0.19, 255];
const tint = [0.943, 0.0285, 255]; // about color-mix(in oklch, brand 15%, white)
const ink = [0.25, 0.05, 255];
const pairs = {
"white text on brand": [white, brand],
"ink text on tint": [ink, tint],
};
for (const [name, [fg, bg]] of Object.entries(pairs)) {
console.log(`${name}: ${contrast(fg, bg).toFixed(2)}:1`);
}Save it as contrast.js and run node contrast.js. Compare each printed ratio with 4.5 for body text. If a pair fails, change the lightness of one color and run it again.
Building contrast into the tokens
Checking after the fact works, but it is better to design the scale so that most pairs pass. Give each role a fixed lightness range:
:root {
--brand-h: 255;
--surface: oklch(97% 0.02 var(--brand-h));
--text: oklch(25% 0.05 var(--brand-h));
--accent: oklch(50% 0.19 var(--brand-h));
--on-accent: white;
}
.button {
background: var(--accent);
color: var(--on-accent);
}
.button:hover {
background: oklch(from var(--accent) calc(l - 0.06) c h);
}The hover state is darker, so white text contrast rises. If you made hover lighter, that pair would need to be rechecked. Keeping text at a low L and surfaces at a high L leaves a wide margin, which helps when chroma or a hue change shifts luminance.
Common errors
- Checking only the base color and not its tint, shade, or hover variants.
- Assuming equal
Lmeans equal contrast across different hues. - Testing text on a semi-transparent background against the wrong backdrop.
- Measuring in the browser's wide-gamut display and ignoring that a color may be clamped on other screens.
Exercise
A teammate says white text is always safe on any color with oklch lightness below 60%. Give one reason this rule is unreliable, and say how you would verify a specific pair.
Show answer
WCAG 2 contrast uses relative luminance, not OKLCH lightness. Chroma and hue change luminance at the same L, so a colorful background at 58% can fall under 4.5:1 while a neutral one passes. Verify the exact pair by computing the ratio, either in developer tools or with a script like the one above, and compare it with 4.5:1 for body text.