Sub topic 5
Measuring contrast with WCAG ratios
This chapter assumes you know basic CSS (color properties, custom properties) and can read a short JavaScript function. The previous chapter showed that contrast makes an element stand out. Here you measure contrast with a number instead of judging it by eye.
Why a number helps
Your eyes are a poor contrast meter. Your monitor, the room lighting, and your own vision all change how a color pair looks. The Web Content Accessibility Guidelines (WCAG) define a contrast ratio that gives the same answer on every screen.
The ratio compares the relative luminance of two colors. Relative luminance is a value from 0 (black) to 1 (white) that estimates how bright a color is to the human eye. The formula is:
ratio = (L_lighter + 0.05) / (L_darker + 0.05)The result runs from 1:1 (identical colors) to 21:1 (black on white).
The thresholds
WCAG 2.x sets minimum ratios by what you are drawing. Level AA is the usual target. Level AAA is stricter.
| Content | AA minimum | AAA minimum |
|---|---|---|
| Normal body text | 4.5:1 | 7:1 |
| Large text (24px or larger, or 18.66px or larger and bold) | 3:1 | 4.5:1 |
| UI components and meaningful graphics (button borders, icons, input outlines) | 3:1 | not defined |
Placeholder text, disabled controls, and purely decorative elements are exempt. Text over photos or gradients still has to meet the ratio at its worst spot.
Computing the ratio in code
The algorithm has three steps. Each 8-bit channel is converted from the gamma-encoded sRGB value to a linear one. The three channels are then weighted by how sensitive the eye is to each (green counts most). Finally, the two luminances are compared.
function channel(value) {
const s = value / 255;
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
}
function luminance([r, g, b]) {
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
function contrastRatio(colorA, colorB) {
const a = luminance(colorA);
const b = luminance(colorB);
const lighter = Math.max(a, b);
const darker = Math.min(a, b);
return (lighter + 0.05) / (darker + 0.05);
}
function hexToRgb(hex) {
const n = parseInt(hex.replace("#", ""), 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
const white = hexToRgb("#ffffff");
console.log(contrastRatio(hexToRgb("#000000"), white).toFixed(2)); // 21.00
console.log(contrastRatio(hexToRgb("#767676"), white).toFixed(2)); // 4.54
console.log(contrastRatio(hexToRgb("#777777"), white).toFixed(2)); // 4.48contrastRatio does not care which color is the foreground. It always divides the lighter luminance by the darker one.
The last two lines show how thin the margin can be. #767676 is the lightest pure gray that passes AA for body text on white. #777777 looks identical but fails.
Checking the button from the earlier chapter
The primary button uses #0b57d0 for its fill and white for its text. Running the function on that pair gives about 6.4:1. That passes AA for normal text (4.5:1) and comes close to AAA (7:1).
The secondary button uses #0b57d0 text and a border on a white page. The text passes the 4.5:1 text threshold. The border passes the 3:1 threshold for UI components. Both buttons are legible, and the filled one still dominates because it has far more colored area.
A quick decision flow
Common errors
- Rounding up. WCAG does not round. A measured 4.48 does not count as 4.5, so don't round it up.
- Checking only the default state. Hover, focus, visited, and disabled-looking states need their own checks. A focus ring must reach 3:1 against its surroundings.
- Ignoring opacity.
opacity: 0.6or a semi-transparent color blends with whatever is behind it. Measure the blended color that appears on screen. - Assuming a passing ratio means good hierarchy. A ratio is a floor for legibility. Hierarchy also needs differences between elements, such as a heading that is darker or heavier than the captions around it.
Exercise
Exercise
A designer proposes #999999 body text on a white background. Using the thresholds in this chapter, would you expect it to pass AA for normal text? How would you confirm it, and what is the simplest fix?
Show answer
No. #999999 is lighter than #767676, which is already the lightest gray that reaches 4.5:1 on white, so #999999 falls short (about 2.8:1). Confirm it by calling contrastRatio(hexToRgb("#999999"), hexToRgb("#ffffff")) or by reading the ratio in the browser's color picker. The simplest fix is to darken the text to #767676 or darker, then re-measure.