Sub topic 13
Checking contrast and accessibility
A palette can follow a harmony rule exactly and still be hard to read. Harmony describes how hues relate to each other. Contrast describes how well two colours separate when one is text or an icon and the other is its background. This chapter covers how to measure that separation and how to adjust a palette that fails.
This chapter assumes you can read HSL values and have seen complementary, analogous, and triadic palettes built from a base hue. The code examples use JavaScript and run in Node.js.
Why harmony does not guarantee legibility
Hue is the angle on the wheel, and the harmony rules operate on it. Readability depends mostly on lightness, because the eye detects edges and letter shapes through differences in brightness more than through differences in hue.
Two colours can sit exactly 180 degrees apart and have nearly the same lightness. A saturated red and a saturated green at equal lightness form a perfect complementary pair. Set as text on background, they blur together, and the edge between them can appear to shimmer. A palette can therefore be harmonious and unusable, and checking it takes a separate step.
Relative luminance and the contrast ratio
The Web Content Accessibility Guidelines (WCAG) define contrast as a ratio between the relative luminance of two colours. Relative luminance is a number from 0 (black) to 1 (white) that estimates how bright a colour appears to a typical viewer.
The calculation has three steps:
- Convert each sRGB channel from 0–255 to 0–1, then linearise it. Screen values are gamma-encoded, so this step undoes the encoding.
- Combine the channels with weights that reflect how sensitive the eye is to each: 0.2126 for red, 0.7152 for green, and 0.0722 for blue.
- Compute the ratio: (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 ratio ranges from 1:1 (identical colours) to 21:1 (black on white).
The weights explain a common surprise. Pure green is far brighter than pure blue in luminance terms, so saturated blue text on black is hard to read even though both colours look vivid.
The thresholds
WCAG 2.x sets minimum ratios for conformance levels AA and AAA.
| Content | AA minimum | AAA minimum |
|---|---|---|
| Normal body text | 4.5:1 | 7:1 |
| Large text (about 24 px, or 19 px bold, and up) | 3:1 | 4.5:1 |
| Interface components and meaningful graphics (borders of inputs, icons, chart marks) | 3:1 | No separate level |
| Decorative elements and disabled controls | No requirement | No requirement |
AA is the usual target for products. Many accessibility laws and procurement rules refer to it.
Measuring contrast in code
The script below converts HSL colours to RGB, computes the contrast ratio, and checks the thresholds. It reuses the idea of storing colours as HSL so you can adjust lightness directly.
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, 9 - k(n), 1));
return [f(0), f(8), f(4)].map((v) => Math.round(v * 255));
}
function channelToLinear(value) {
const c = value / 255;
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
}
function luminance([r, g, b]) {
return (
0.2126 * channelToLinear(r) +
0.7152 * channelToLinear(g) +
0.0722 * channelToLinear(b)
);
}
function contrastRatio(rgbA, rgbB) {
const a = luminance(rgbA);
const b = luminance(rgbB);
const lighter = Math.max(a, b);
const darker = Math.min(a, b);
return (lighter + 0.05) / (darker + 0.05);
}
function report(label, rgbA, rgbB) {
const ratio = contrastRatio(rgbA, rgbB);
const text = ratio >= 4.5 ? "passes AA text" : "fails AA text";
const large = ratio >= 3 ? "passes large text/UI" : "fails large text/UI";
console.log(`${label}: ${ratio.toFixed(2)}:1 (${text}, ${large})`);
}
const white = [255, 255, 255];
const blue = hslToRgb(210, 65, 45);
const orange = hslToRgb(30, 65, 45);
const darkOrange = hslToRgb(30, 65, 35);
report("white on blue", white, blue);
report("white on orange", white, orange);
report("blue vs orange", blue, orange);
report("white on dark orange", white, darkOrange);Running it for the complementary pair from earlier chapters (hues 210 and 30, both at 65% saturation and 45% lightness) gives results close to these:
- White on the blue: about 4.9:1. This passes AA for body text.
- White on the orange: about 3.7:1. This passes for large text and icons but fails for body text.
- Blue against orange: about 1.3:1. The two colours have almost the same lightness, so orange text on a blue background is nearly unreadable.
- White on the orange at 35% lightness: about 5.7:1. Lowering lightness fixed the failure and kept the hue.
The last result shows the general repair. You can keep the hue relationships and change only lightness.
A workflow for testing a palette
Apply the flow to each pair that appears in the design, not to the palette as a whole. A palette of five colours has up to ten pairs, but only the pairs that actually touch need checking: text on each surface, borders on each surface, and adjacent chart series.
Adjusting a failing pair
When a pair fails, change lightness first.
- Darken the darker colour or lighten the lighter one. Moving both apart works faster than moving one far.
- Reduce saturation if a vivid colour glares at high lightness. Tones (colour mixed with grey) often read better as backgrounds.
- Keep hue unchanged where you can, so the harmony survives.
- Accept that some hues have a limited usable range. Yellow is very light even at full saturation, so it cannot carry white text at any practical lightness. It needs a dark foreground or a darkened, olive-leaning variant.
Dominance and proportion help here too. Under the 60-30-10 guideline, the dominant colour is usually a large neutral or low-saturation surface. Placing text colours against that surface is easier to verify than checking accent colours against each other.
Colour is not the only signal
Contrast ratio addresses low vision and poor screens. A separate failure happens when colour is the only carrier of meaning. About 1 in 12 men have some form of red-green colour vision deficiency. A red error and a green success state at the same lightness can look identical to them.
Complementary red and green pairs are the classic trap. Follow these rules:
- Add a second cue to any status colour: an icon, a text label, or a pattern.
- Give chart series different lightness as well as different hue, and use direct labels or different line styles where possible.
- Underline links inside body text, or keep a 3:1 contrast between link colour and surrounding text.
- Test with a colour-vision simulator or the greyscale mode in your design tool. If two colours turn into the same grey, they depend on hue alone.
Common errors
- Checking the swatch, not the usage. A colour has no contrast value by itself. The ratio exists only for a pair, so a brand colour can pass on white and fail on a tinted card.
- Ignoring state colours. Hover, focus, visited, and disabled variants need their own checks. Focus indicators need 3:1 against the surrounding colours.
- Testing text over images or gradients at one point. Measure the worst case, or place a solid or semi-opaque layer behind the text.
- Rounding up. A ratio of 4.49:1 fails the 4.5:1 threshold. Do not round for the report.
- Relying on HSL lightness as a proxy. Two colours with equal HSL lightness can have very different luminance. Yellow at 50% lightness is much brighter than blue at 50% lightness. Compute the ratio instead of comparing L values.
- Treating AAA as the only goal. AAA at 7:1 is hard to reach with mid-tone brand colours. Aim for AA everywhere and reach higher on long-form reading text.
Summary of the method
- Build the harmony from hue angles.
- Set the lightness and saturation for each role: surface, text, accent, border.
- Compute the ratio for every pair that touches.
- Adjust lightness until each pair meets its threshold.
- Check that no meaning depends on hue alone.
Exercise
A designer builds a complementary palette with hsl(120 70% 45%) as a green button background and hsl(300 70% 45%) as a magenta label placed on top. Both have the same saturation and lightness. Will this pair pass WCAG AA for text, and what is the simplest fix that keeps the complementary relationship?
Show answer
It will almost certainly fail. The two colours are exactly 180 degrees apart, but equal HSL lightness does not mean equal luminance. Green has a much larger luminance weight (0.7152) than magenta's red and blue parts combined, so the two do differ in luminance, yet the difference is far too small to reach 4.5:1. Measure it with the contrastRatio function to confirm. The simplest fix is to change lightness only: darken the button background or use a very light or very dark label colour so the ratio reaches at least 4.5:1 for body text, or 3:1 if the label is large text. The hues stay at 120 and 300, so the complementary relationship is preserved.
Exercise
A chart uses a red line for "failed" and a green line for "passed". Both lines have the same lightness. List two changes that make the chart accessible without changing either hue.
Show answer
First, give the lines different lightness so they also differ in greyscale, and check each line against the chart background for at least 3:1. Second, add a non-colour cue, such as a different line style (solid versus dashed), distinct point markers, or a direct text label at the end of each line. Either of these lets a viewer with red-green colour vision deficiency tell the series apart.