Sub topic 8
Checking contrast against WCAG
This chapter assumes you have a palette with primary, neutral, and accent scales, as built in the earlier chapters. It also assumes you can read HSL values and edit CSS custom properties. Here you will measure whether your colour pairs are readable and usable by people with low vision, and fix the pairs that are not.
What WCAG measures
The Web Content Accessibility Guidelines (WCAG) are the standard that most accessibility laws and procurement rules refer to. WCAG 2.x defines contrast as a ratio between the relative luminance of two colours. Relative luminance is a number from 0 (pure black) to 1 (pure white) that approximates how bright a colour looks to the human eye.
The ratio runs from 1:1 (identical colours) to 21:1 (black on white). Hue does not enter the formula. A saturated red and a mid grey with the same luminance have a ratio of 1:1, even though they look different. This is why a palette can look distinct and still be hard to read for many people.
The formula is:
- Convert each 8-bit sRGB channel to the range 0 to 1.
- Linearise each channel, which undoes the gamma curve that screens apply.
- Weight the channels: 0.2126 for red, 0.7152 for green, 0.0722 for blue. The weights are uneven because the eye is most sensitive to green.
- Compute
(lighter + 0.05) / (darker + 0.05).
The 0.05 term models ambient light reflecting off the screen. It keeps the ratio finite when one colour is black.
The thresholds
WCAG has two conformance levels you will meet in practice. Level AA is the usual legal and contractual target. Level AAA is stricter, and most sites apply it only to selected content.
| What you are checking | AA | AAA | WCAG criterion |
|---|---|---|---|
| Normal text | 4.5:1 | 7:1 | 1.4.3 and 1.4.6 |
| Large text | 3:1 | 4.5:1 | 1.4.3 and 1.4.6 |
| UI components and meaningful graphics | 3:1 | none | 1.4.11 |
Large text means at least 24 CSS pixels (18 pt) at regular weight, or at least about 18.66 CSS pixels (14 pt) in bold. Anything smaller counts as normal text.
The non-text rule (1.4.11) covers the visual parts a user needs in order to find and operate a control. Examples are the border of a text input, the outline of a checkbox, the focus indicator, and the segments of a chart. Each must reach 3:1 against its neighbouring colours.
Some things are exempt: disabled controls, purely decorative elements, logos, and text that is part of a picture of something. Do not stretch these exemptions. Placeholder text is not exempt, because users read it.
Calculating the ratio in code
The function below is the one from the topic plan, extended with a hex parser and a grading helper. It runs in Node.js or a browser console.
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 h = hex.replace("#", "");
const full = h.length === 3 ? h.split("").map((c) => c + c).join("") : h;
const n = parseInt(full, 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
function grade(ratio, { large = false } = {}) {
const aa = large ? 3 : 4.5;
const aaa = large ? 4.5 : 7;
if (ratio >= aaa) return "AAA";
if (ratio >= aa) return "AA";
return "fail";
}
const pairs = [
["#000000", "#ffffff"],
["#767676", "#ffffff"],
["#777777", "#ffffff"],
];
for (const [fg, bg] of pairs) {
const ratio = contrastRatio(hexToRgb(fg), hexToRgb(bg));
console.log(fg, bg, ratio.toFixed(2), grade(ratio));
}The output is:
#000000 #ffffff 21.00 AAA
#767676 #ffffff 4.54 AA
#777777 #ffffff 4.48 fail#767676 is the lightest grey that passes AA for normal text on white. One step lighter fails, even though the two look identical.
The grade function compares the raw ratio, not the rounded one. toFixed(2) is only for display. A ratio of 4.499 displays as 4.50 but does not pass. WCAG does not allow rounding up, so never compare a rounded value to the threshold.
Applying it to the palette
Take the palette from the topic plan. The primary-500 value hsl(220 80% 50%) converts to roughly rgb(26, 94, 230). Against white it gives a ratio of about 5.5:1, so white button labels on --primary-500 pass AA at normal size.
The accent-500 value hsl(35 95% 50%) is a bright orange. Against white it gives about 2.3:1. That fails for text, and it also fails the 3:1 rule for UI components. If you put white text on an orange button, the button fails. Two fixes are available:
- Use dark text, such as
--neutral-900, on the accent background. - Use a darker accent step for any surface that carries white text.
The border colour --neutral-300 against --neutral-50 gives about 1.5:1. That is fine for a decorative divider. It fails 1.4.11 when it is the only thing that shows where a text input is. For form fields, use a darker border, such as a step near --neutral-700, or add another visible cue such as a filled background that reaches 3:1.
Treat these numbers as estimates and run your own palette through the script. They depend on how your tool rounds the HSL to RGB conversion.
Fixing a failing pair
Because the palette is built in HSL with a fixed hue, the fix is usually mechanical. Keep hue and saturation, and change only lightness. Move the foreground darker, or the background lighter, until the ratio passes. Stay within the scale you already defined where you can. A new in-between step is fine, but name it and add it to the scale so the fix does not become a one-off value.
Decide in advance which pairs you allow. A common approach is a short list of approved combinations:
--neutral-900text on--neutral-50for body copy.--primary-700links on--neutral-50.- White text on
--primary-500or darker for buttons. --neutral-900text on--accent-500for highlights.
Components then use only these pairs, and the testing chapter can check the list instead of every possible combination.
Edge cases that the ratio does not cover
The formula needs two solid colours. Real pages often do not provide them.
- Transparency. A colour with an alpha value has no fixed luminance. Composite it over the actual background first, then measure the result. The same
rgba()text can pass on white and fail on a tinted card. - Text over images or gradients. Measure at the worst point, meaning the place where the text sits on the part of the background closest in luminance to the text. A semi-opaque overlay behind the text is a common fix.
- States. Check hover, focus, active, visited, and selected states, not only the default. A hover background that lightens a button can drop white label text below 4.5:1.
- Links inside paragraphs. If a link is distinguished from surrounding text by colour alone, the link colour needs 3:1 against the body text colour, and it should also have a non-colour cue such as an underline. The simplest rule is to keep underlines on links in running text.
- Focus indicators. A focus ring must reach 3:1 against the colours next to it, on both the inside and the outside when the ring sits between two surfaces. A two-tone ring survives any background:
:focus-visible {
outline: 3px solid var(--primary-700);
outline-offset: 2px;
box-shadow: 0 0 0 5px var(--neutral-50);
}- Dark mode. The thresholds are the same, but the pairs are different, so every pair needs a new measurement. A scale that passes on light surfaces can fail when the same step sits on a dark background. The dark mode chapter returns to this.
Known limits of the WCAG 2 formula
The WCAG 2 ratio is a simple model, and it has known weaknesses. It is not perceptually uniform. It tends to overstate the contrast of some dark pairs and understate the readability of some light-on-dark text. It ignores font weight and size, apart from the single large-text cut-off. The WCAG 3 drafts propose a different algorithm called APCA, which does account for these things.
For compliance, WCAG 2 is still the standard that regulators and auditors use, so measure against it. Treat the ratio as a minimum, not as a guarantee. Where you have a choice, exceed the threshold for small or thin text, and test with real content and real devices.
Checking in practice
Use the script above for batch checks, and use tools for quick checks while you design:
- The contrast checker in browser developer tools, which shows the ratio and AA or AAA marks next to a colour picker.
- A standalone contrast checker site for comparing two hex values.
- Automated audits such as Lighthouse or axe, which find text contrast failures on a rendered page. They cannot judge text over images or the non-text rules reliably, so they do not replace a manual check.
A practical routine is to measure each approved pair once when you define the palette, record the ratios next to the tokens, and re-measure when a token changes.
Exercise
A button uses white text at 16px regular weight on --accent-500 (hsl(35 95% 50%)), which gives a ratio of about 2.3:1. Name the WCAG level this fails and give two ways to fix it without changing the hue.
Show answer
It fails AA for normal text, which needs 4.5:1. Large text needs 3:1, and 16px regular is not large, so 3:1 would not apply. Fix 1: change the text to a dark neutral such as --neutral-900, which has a high ratio against the orange. Fix 2: darken the button background by lowering its lightness in HSL (keeping hue 35) until white text reaches at least 4.5:1, then add that step to the accent scale.
Exercise
Your script prints 4.50 for a pair, but the underlying value is 4.499. Does the pair pass AA for normal text, and what should the script compare to the threshold?
Show answer
It does not pass. WCAG requires a ratio of at least 4.5, and rounding up is not allowed. The script must compare the unrounded ratio and use toFixed only for display.
Exercise
A text input has a 1px border in --neutral-300 (about 1.5:1 against the page background) and no other visible edge. Which criterion does it fail, and what is the minimum ratio the border needs?
Show answer
It fails WCAG 1.4.11 Non-text Contrast. The border is the visual cue that identifies the control, so it needs at least 3:1 against the adjacent colours. A darker neutral step, such as one near --neutral-700, would meet it.