Sub topic 12
Testing and documenting the finished palette
This chapter assumes you have worked through the earlier chapters. You should have a palette defined as CSS custom properties with a semantic layer on top (--color-text, --color-bg, and so on), and a dark theme that reassigns only the semantic tokens. You should also be able to run a short Node.js script from a terminal.
A palette is finished when you have checked it against real usage and written down how to use it. Without the checks, a colour that looked fine in your editor can fail on a user's screen. Without the documentation, the next person to touch the stylesheet will add a new blue because they can't find the old one.
What to test
Test every combination of colours that actually appear together, in every theme. A palette with 20 swatches can produce hundreds of pairings, but your components use only a handful. List those pairings first.
| Area | Question | Tool |
|---|---|---|
| Text contrast | Does every text colour reach 4.5:1 (3:1 for large text) on its background? | Contrast script |
| UI contrast | Do input borders, icons, and focus rings reach 3:1? | Contrast script |
| Colour independence | Is anything communicated by colour alone? | Greyscale and vision-deficiency emulation |
| Proportion | Does the page roughly follow 60-30-10? | Visual review, screenshot |
| Themes | Do the checks pass in light and dark? | Run checks per theme |
| States | Do hover, focus, disabled, and visited states stay readable? | Manual pass through components |
The review runs in a fixed order, because automated checks are cheap and visual checks need a stable palette.
Automating the contrast check
Checking contrast by hand in a web tool works for one pair. It does not scale to two themes and a dozen pairings, and it does not catch a regression when someone edits a token six months later. A script solves both problems.
The script below reuses the contrast formula from the contrast chapter. It adds an hslToRgb function so you can keep the palette in HSL, as in your stylesheet. Each theme maps semantic roles to palette steps, and a list of checks says which role must reach which ratio against which background.
// check-contrast.js (run with: node check-contrast.js)
function hslToRgb(h, s, l) {
s /= 100;
l /= 100;
const k = (n) => (n + h / 30) % 12;
const a = s * Math.min(l, 1 - l);
const f = (n) =>
l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
return [f(0), f(8), f(4)].map((v) => Math.round(v * 255));
}
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);
}
// [hue, saturation %, lightness %]
const palette = {
white: [0, 0, 100],
'primary-100': [220, 80, 94],
'primary-500': [220, 80, 50],
'primary-700': [220, 80, 34],
'neutral-50': [220, 15, 97],
'neutral-300': [220, 12, 80],
'neutral-700': [220, 15, 28],
'neutral-900': [220, 20, 12],
};
// Each theme maps semantic roles to palette steps.
const themes = {
light: {
bg: 'neutral-50',
text: 'neutral-900',
link: 'primary-700',
border: 'neutral-300',
buttonBg: 'primary-500',
buttonText: 'white',
},
dark: {
bg: 'neutral-900',
text: 'neutral-50',
link: 'primary-100',
border: 'neutral-700',
buttonBg: 'primary-500',
buttonText: 'white',
},
};
// [foreground role, background role, minimum ratio]
const checks = [
['text', 'bg', 4.5],
['link', 'bg', 4.5],
['border', 'bg', 3],
['buttonText', 'buttonBg', 4.5],
];
let failures = 0;
for (const [themeName, roles] of Object.entries(themes)) {
for (const [fg, bg, min] of checks) {
const ratio = contrastRatio(
hslToRgb(...palette[roles[fg]]),
hslToRgb(...palette[roles[bg]])
);
const pass = ratio >= min;
if (!pass) failures += 1;
console.log(
`${pass ? 'PASS' : 'FAIL'} ${themeName}: ${fg} on ${bg} = ` +
`${ratio.toFixed(2)} (needs ${min})`
);
}
}
process.exitCode = failures > 0 ? 1 : 0;Run it and read the output. The text, link, and button pairs pass. The button label (white on --primary-500) comes out at roughly 5.1, just above the 4.5 line. The border check fails in both themes. In the light theme, --neutral-300 on --neutral-50 gives roughly 1.5, well below 3.
That failure is a real finding, not a bug in the script. A pale border is fine for a card outline, because purely decorative edges are exempt from the WCAG non-text contrast rule. It is not fine for a text input, where the border is how a user finds the field. The fix is a separate role for input borders, backed by a darker neutral step. Keep --color-border for decoration and add --color-border-strong for controls:
:root {
--neutral-500: hsl(220 12% 46%);
--color-border-strong: var(--neutral-500);
}
input,
select,
textarea {
border: 1px solid var(--color-border-strong);
}Add border-strong to each theme and to the checks list, then run the script again. Because the script exits with code 1 on any failure, you can run it in a continuous integration job or a pre-commit hook. A token edit that breaks contrast then stops the build.
Testing for colour-vision differences
Contrast ratios measure lightness difference. They say nothing about whether two colours can be told apart by someone with a colour-vision deficiency. About 1 in 12 men and 1 in 200 women have one, most often a red-green type. Two checks catch most problems.
- Greyscale. Remove all hue and see whether the interface still makes sense. Add this rule temporarily in DevTools or your stylesheet, then remove it:
html {
filter: grayscale(1);
}If a success message and an error message look identical in greyscale, they differ only by hue. Add an icon, a label, or a different weight.
- Vision-deficiency emulation. Chromium-based browsers can emulate protanopia, deuteranopia, tritanopia, and achromatopsia. Open DevTools, open the Rendering panel, and choose a type under "Emulate vision deficiencies". Click through key screens with each type active.
Pay attention to three places. The first is error and success states, since red and green often collapse together. The second is charts and legends, where series are separated only by colour. The third is links inside body text. Links need an underline, or another cue that does not rely on hue, when they sit among paragraphs.
Checking proportion and states
Take a screenshot of a representative page and look at it at a small size, or squint. Your three roles should read in the right order. Neutrals should dominate, the primary should mark the main structure and actions, and the accent should appear in only a few places. If the accent draws the eye away from the primary action, you have used it too often. The 60-30-10 guideline is a starting point, not a rule. A dense dashboard may sit closer to 80-15-5, and a marketing page may spend more on the primary.
Then step through states. For each interactive component, look at default, hover, active, focus, disabled, and visited. Derive these from your scale instead of inventing new values. For example, a button's hover background can be --primary-700 and its pressed state can be one step darker. Check that the focus ring has at least 3:1 against the colours on both sides of it. Disabled controls are exempt from contrast requirements, but they should still be visibly different from enabled ones.
Documenting the palette
Documentation exists so that people choose a token without asking you. Write the minimum that answers three questions: what exists, when to use it, and what is forbidden.
A short token table in your README or style guide covers the first two:
| Token | Role | Use for | Do not use for |
|---|---|---|---|
| `--color-text` | Semantic | Body text and headings | Decorative lines |
| `--color-border` | Semantic | Card and divider outlines | Form-control borders |
| `--color-border-strong` | Semantic | Inputs, selects, checkboxes | Dividers |
| `--color-link` | Semantic | Inline links | Buttons |
| `--color-button-bg` | Semantic | Primary action backgrounds | Text |
| `--color-highlight` | Semantic | Badges, selection, small emphasis | Large surfaces, text on white |
Beyond the table, record these facts:
- Approved pairings. List each foreground and background combination with its measured ratio, such as "
--color-texton--color-bg: 16.4:1". Measured numbers let reviewers see how much margin a pairing has. - The rule for components. State that components reference only semantic tokens. Raw palette steps appear only in the token definition file.
- Theme behaviour. Say which tokens change in dark mode and which stay fixed.
- Known exceptions. If a pairing deliberately falls below a threshold, such as a decorative border, write down why.
Keep this documentation next to the code, in the repository, so it changes in the same pull request as the tokens. A palette page in a separate wiki drifts out of date within weeks.
Finally, keep a short changelog of palette changes. Each entry needs a date, the token changed, the old and new values, and the reason. When a button looks different after a release, the changelog tells you why in a minute instead of an afternoon. If other teams or projects consume your tokens, treat a value change as a minor version and a renamed or removed token as a breaking one.
Common errors
- Checking one theme. A palette that passes in light mode can fail in dark mode, because the same
--color-borderstep no longer has the same relationship to the new background. Run every theme. - Trusting the swatch. Colours over images, gradients, or semi-transparent layers have no single background colour. Test the worst-case region, or add a solid scrim behind the text.
- Checking tokens but not usage. The script confirms that a pairing meets the ratio. It cannot detect a component that uses
--color-highlightfor text. The "do not use for" column and code review cover that gap. - Treating 4.5:1 as the goal. The threshold is a minimum. Body text at exactly 4.6:1 on a bright, glare-prone screen is a weak choice. Aim higher for the text people read most.
- Documenting once. An out-of-date palette page is worse than none, because it gives wrong answers with confidence. Tie updates to the same change as the code.
Exercise
Your script reports PASS for --color-link on --color-bg at 7.3:1, but a teammate says inline links are still hard to spot inside paragraphs. What check did the script not cover, and how do you fix it without changing the palette?
Show answer
The script measures contrast between the link text and the background, not the difference between the link and the surrounding body text, and it cannot test colour-vision differences. Because the link colour (a blue) and the body colour (a dark neutral) differ mainly in hue, some users cannot tell them apart. Fix it with a non-colour cue: keep the underline on inline links (text-decoration: underline), or use a heavier font weight. Then confirm with the greyscale filter or vision-deficiency emulation that the links remain visible.
Exercise
A new token, --color-placeholder, points at --neutral-300. Which entry would you add to the checks list in check-contrast.js, and what result do you expect for it on --neutral-50?
Show answer
Add a role placeholder: 'neutral-300' to each theme and the check ['placeholder', 'bg', 4.5]. Placeholder text is text, so it needs 4.5:1. --neutral-300 on --neutral-50 gives roughly 1.5:1, so the check fails. Choose a darker step such as --neutral-500 and run the script again. Check the dark theme as well.