Sub topic 12
Testing and documenting the finished palette
This chapter assumes you can write CSS custom properties, read HSL values, and know the WCAG contrast thresholds from the earlier chapters. You will review a finished palette, automate the contrast check, and write down the decisions so others can keep the palette consistent.
What a palette review covers
A palette can look right on your screen and still fail for other users. A review checks three things:
- Contrast: every text and UI pairing meets the WCAG 2 thresholds.
- Consistency: components use semantic tokens such as
--color-text, not raw shades. - Colour-vision accessibility: meaning never depends on colour alone.
Test contrast with a script
Checking pairs by hand in a web tool does not scale. List every foreground and background pairing in a small script and run it whenever the palette changes. This script reuses the contrast formula from the contrast chapter and reports a pass or fail for each pair.
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);
return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
}
const pairs = [
{ name: "text on background", fg: [0, 0, 0], bg: [255, 255, 255], min: 4.5 },
{ name: "large heading on background", fg: [90, 90, 90], bg: [255, 255, 255], min: 3 },
{ name: "light grey border on white", fg: [200, 200, 200], bg: [255, 255, 255], min: 3 },
];
for (const { name, fg, bg, min } of pairs) {
const ratio = contrastRatio(fg, bg);
const result = ratio >= min ? "PASS" : "FAIL";
console.log(`${result} ${name}: ${ratio.toFixed(2)} (needs ${min})`);
}Run it with Node.js. The first two pairs pass. The third fails, because a light grey border on white is well under 3:1. That is a typical finding: borders around form fields need 3:1, so a decorative-looking neutral step may be too pale.
Use the right threshold for each pair:
| Pair | Minimum ratio |
|---|---|
| Normal body text | 4.5:1 |
| Large text (about 24px, or 19px bold) | 3:1 |
| UI components such as input borders and icons | 3:1 |
| Purely decorative dividers | No requirement |
Test the pairs in both themes. If you adapted the palette for dark mode, run the script again with the dark values, since a pair that passes in light mode can fail after the tokens are reassigned.
Check consistency in the stylesheet
Raw palette values in component CSS defeat the semantic layer, because changing a token no longer updates every use. Search the stylesheet for hard-coded colours. This command lists every line that uses a hex value, rgb() or hsl() outside the files that define tokens:
grep -rnE "#[0-9a-fA-F]{3,8}\b|rgb\(|hsl\(" src/css --include=*.css --exclude=tokens.cssAnything it prints should either become a var(--color-...) reference or be added to the palette on purpose.
Check colour-vision accessibility
About 1 in 12 men has some form of colour-vision deficiency, most often red-green. Success and error colours are the usual problem, since green and red can look alike. Check these points:
- Every status message includes an icon or text label, such as "Error:", beside its colour.
- Links inside body text are underlined or differ in more than hue.
- Charts use labels or patterns as well as colour.
Browser developer tools can emulate colour-vision deficiencies. In Chrome, open the Rendering panel and choose a vision deficiency under "Emulate vision deficiencies". Firefox offers similar simulations in its Accessibility panel.
Document the palette
A palette that exists only in CSS gets broken over time. Keep a short reference next to the code, either in the repository or in your design documentation. Include:
- The roles: which colour is primary, which are neutral, and which is the accent.
- The tokens: each semantic token, the palette step it points to, and its intended use.
- The tested pairs: the contrast ratio for each approved text and background pairing.
- The rules: for example, "the accent is used only for highlights, about 10% of the page".
A table works well for this:
| Token | Points to | Use | Contrast on `--color-bg` |
|---|---|---|---|
| `--color-text` | `--neutral-900` | Body text | Passes 4.5:1 |
| `--color-link` | `--primary-700` | Links in text | Passes 4.5:1 |
| `--color-border` | `--neutral-300` | Decorative dividers | Not required |
Record the actual ratios your script reports, and update the table whenever a token changes. Re-run the script in your build or review process so a failing pair is caught before release.
Exercise
Your script reports that --color-border (a light grey) has a ratio of 1.6:1 against the page background, and that border surrounds a text input. Does this pass, and what should you do?
Show answer
It fails. Borders that identify UI components such as input fields need at least 3:1. Choose a darker neutral step, such as one near --neutral-700 or a mid step that reaches 3:1, and point a separate token such as --color-border-input at it. Keep the lighter step for decorative dividers. Then re-run the script in both light and dark themes and update the documentation table.
Exercise
Name two checks beyond contrast that a finished palette review should include, and give one fix for each problem.
Show answer
First, consistency: search the stylesheets for hard-coded hex, rgb() or hsl() values outside the token file, and replace them with semantic tokens. Second, colour-vision accessibility: confirm that status colours are paired with an icon or text label, and add one if an error message relies on red alone.