Sub topic 13
Checking contrast and accessibility
This chapter assumes you can read HSL values and know the 60-30-10 proportion guideline from the previous chapter. A palette can be harmonious and still be hard to read. Harmony describes how hues relate. Contrast describes how different two colours are in brightness. You need to check both.
Why harmony does not guarantee legibility
Hue relationships say nothing about lightness. A complementary pair such as a blue and an orange can sit at the same lightness. Placed together as text on a background, they look vivid but blend into each other, and the edges seem to vibrate. Analogous palettes have a related problem: neighbouring hues often share similar lightness, so text and background can be too close in value.
Readability depends mostly on the lightness difference between foreground and background. Hue and saturation matter much less.
The WCAG contrast ratio
The Web Content Accessibility Guidelines (WCAG) define contrast as a ratio from 1:1 (identical colours) to 21:1 (black on white). The common targets are:
| Content | Minimum (AA) | Enhanced (AAA) |
|---|---|---|
| Normal body text | 4.5:1 | 7:1 |
| Large text (about 24px, or 19px bold) | 3:1 | 4.5:1 |
| Icons, input borders, chart lines | 3:1 | not defined |
The ratio uses relative luminance, which weights green most heavily because the eye is most sensitive to it. Each sRGB channel is first linearised, then combined.
function channel(value) {
const c = value / 255;
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
}
function luminance(hex) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
function contrastRatio(hexA, hexB) {
const a = luminance(hexA);
const b = luminance(hexB);
const lighter = Math.max(a, b);
const darker = Math.min(a, b);
return (lighter + 0.05) / (darker + 0.05);
}
console.log(contrastRatio("#000000", "#ffffff").toFixed(2)); // 21.00
console.log(contrastRatio("#767676", "#ffffff").toFixed(2)); // 4.54The grey #767676 is the lightest grey that passes 4.5:1 on white, so it is a useful benchmark.
Fixing a failing pair
When a pair fails, keep the hue and change lightness. This keeps the palette's harmony intact. In HSL, lower the text's lightness, or raise the background's, until the ratio passes. Changing the hue would break the relationship you chose.
.card {
background: hsl(210 65% 95%);
color: hsl(210 65% 20%);
}
.card .accent {
color: hsl(30 80% 30%); /* complement, darkened to stay readable */
}Here the complement keeps its hue (30) but drops to 30% lightness so it works as text on a pale background.
Do not rely on hue alone
About 1 in 12 men has some form of colour vision deficiency, most often red-green. A red error and a green success that differ only in hue can look identical to them. Add a second cue such as an icon, a text label, an underline, or a pattern. Also test with a colour vision simulator, which most browser developer tools include.
A practical checking routine
Exercise
A designer picks a triadic palette and sets pale yellow text (hsl(55 90% 85%)) on a white background. The hues are harmonious, but the text is hard to read. Why, and what is the smallest change that keeps the harmony?
Show answer
The text and background are both very light, so their luminance values are close and the ratio is far below 4.5:1. Hue is not the problem. Keep the hue at 55 and lower its lightness (for example to about 25%), or use the yellow as a background and put dark text on it. Then recompute the ratio to confirm it reaches at least 4.5:1.