Sub topic 12
Color and Contrast for Legibility
Earlier chapters built hierarchy from size, weight, and spacing. Color is the fourth lever, and it is the easiest to misuse. A pale gray subhead can look elegant on a designer's monitor and be unreadable on a phone in sunlight. This chapter covers how to measure contrast, which thresholds to meet, and how to use color to separate levels without hurting legibility.
This chapter assumes you know the terms typeface, size, weight, and italic. It also assumes you can read basic CSS and a short Python function.
Color as a hierarchy cue
Color works best as a supporting signal. A reader who can't see your color choices, because of low vision, color blindness, glare, or a grayscale print, should still be able to tell a headline from body text. Size, weight, and spacing carry the structure. Color refines it.
Color can do three jobs in a text hierarchy:
- Rank levels. Headlines take the darkest tone. Body text takes a slightly softer one. Captions and metadata take the softest tone that still passes the contrast thresholds.
- Mark interaction. Links and buttons get an accent color, so the reader sees what is clickable.
- Signal state. Errors, warnings, and success messages get distinct hues, paired with an icon or a word.
Don't use hue alone for any of these jobs. About 1 in 12 men has some form of color vision deficiency, so a red-versus-green distinction needs a second cue such as an underline, a label, or a weight change.
What contrast means
Contrast is the difference in brightness between text and its background. It is not the same as difference in hue. Red text on a green background can have almost no brightness difference, so it vibrates and is hard to read, even though the two hues look very different.
WCAG (the Web Content Accessibility Guidelines) defines a contrast ratio between two colors. The ratio runs from 1:1 (identical colors) to 21:1 (black on white). It is computed from each color's relative luminance, a number from 0 to 1 that estimates how bright the color appears to a human eye.
The steps are:
- Convert each sRGB channel (red, green, blue) from 0–255 to 0–1.
- Linearize each channel. If the value is 0.04045 or less, divide it by 12.92. Otherwise compute ((value + 0.055) / 1.055) raised to the power 2.4.
- Compute luminance as
0.2126 × R + 0.7152 × G + 0.0722 × B. Green weighs most because the eye is most sensitive to it. - Compute the ratio as
(L_lighter + 0.05) / (L_darker + 0.05).
The 0.05 term models ambient light, so even pure black has a small effective brightness.
The WCAG thresholds
WCAG 2.x sets minimum ratios by text size and conformance level:
| Text type | Level AA | Level AAA |
|---|---|---|
| Normal text | 4.5:1 | 7:1 |
| Large text (24px regular, or 18.66px bold, and up) | 3:1 | 4.5:1 |
| UI components and graphical objects (borders, icons) | 3:1 | not defined |
Large text gets a lower threshold because bigger glyphs stay readable at lower contrast. This ties directly to your type scale. In the scale from the earlier chapter, --step-2 (1.563rem, about 25px) and above counts as large even at regular weight. Body text at --step-0 and small captions must meet 4.5:1.
AA is the practical target for most products and is the level most accessibility regulations reference. Aim for AAA on long-form reading text where you can.
Computing contrast yourself
Browser developer tools and design tools report ratios, but it helps to know what they calculate. This script implements the formula above and checks three grays on white.
def channel(value: int) -> float:
c = value / 255
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
def luminance(hex_color: str) -> float:
h = hex_color.lstrip("#")
r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b)
def contrast(fg: str, bg: str) -> float:
l1, l2 = luminance(fg), luminance(bg)
lighter, darker = max(l1, l2), min(l1, l2)
return (lighter + 0.05) / (darker + 0.05)
for color in ("#595959", "#767676", "#949494"):
print(color, "on #ffffff:", round(contrast(color, "#ffffff"), 2))The output is about 7.0 for #595959, 4.54 for #767676, and 3.03 for #949494. So #767676 is the lightest gray that passes AA for normal text on white. #949494 passes only for large text and UI components. Light gray body text is a common failure, and the cause is rarely the designer's intent. It usually comes from "softening" a color by eye.
A color system for the three text levels
Define text colors as named tokens, so every level is chosen once and checked once. The tokens below use dark grays on a white page, and each passes AA.
:root {
--bg: #ffffff;
--text-strong: #111111; /* headlines, high emphasis */
--text-body: #333333; /* body text */
--text-muted: #595959; /* captions, metadata */
--link: #0b57d0;
}
body {
background: var(--bg);
color: var(--text-body);
}
h1, h2, h3 {
color: var(--text-strong);
}
.caption {
color: var(--text-muted);
font-size: 0.875rem;
}
a {
color: var(--link);
text-decoration: underline;
}The steps between tokens are small on purpose. The three levels already differ in size, weight, and space, so color only needs a light nudge. The muted token is #595959, which gives 7:1 on white, comfortably above the 4.5:1 minimum for small caption text. Links keep their underline, so link identity doesn't depend on hue.
Dark mode and halation
Dark backgrounds change the rules. Pure white (#ffffff) text on pure black (#000000) gives the maximum 21:1 ratio, but it can be tiring to read. Light text on a dark ground appears to bleed outward, an effect called halation, and it is worse for readers with astigmatism.
A common fix is to soften both ends: an off-black background and an off-white text color. The text pair below measures about 15:1, which is far above AAA.
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text-strong: #f2f2f2;
--text-body: #e6e6e6;
--text-muted: #b3b3b3;
--link: #8ab4f8;
}
}Two further points apply in dark mode. Thin weights look thinner on dark backgrounds, so consider raising body weight slightly or avoiding very light weights for headlines. Also, saturated colors that work on white often vibrate on dark, so lighten and desaturate accent colors, as the link color above does.
A decision flow for each text color
Run each text-and-background pair through the same check. The diagram shows the order.
When adjusting, change lightness, not hue. Darkening a color keeps its identity, so a brand blue can stay recognizably blue and still pass.
Common failures
- Text over images. The ratio varies across the photo, so a single measurement means little. Add a solid or gradient overlay behind the text, and measure against the lightest area the text crosses.
- Placeholder and disabled text. Placeholder text inside form fields still needs 4.5:1 if it carries meaning. Disabled controls are exempt from WCAG, but making them unreadable hides information.
- Colored text on colored panels. A blue headline on a tinted panel needs measuring against the panel, not the page.
- Anti-aliasing and thin weights. The formula treats text as solid color. A 300-weight font renders with lighter edge pixels, so it reads as lower contrast than the ratio suggests. Give thin weights extra margin.
- Relying on color alone. A red error message with no icon or text label fails readers who can't distinguish red.
Checklist
- List every text color in the design and its background.
- Classify each as normal or large text.
- Measure each pair and record the ratio.
- Fix failures by adjusting lightness.
- Confirm links, errors, and states have a second cue besides hue.
- Repeat for dark mode if you ship one.
Exercise
A page uses #8a8a8a body text (16px, regular weight) on a white background. The ratio is about 3.4:1. Does it meet WCAG AA? What are two ways to fix it without changing the type scale?
Show answer
It fails. Normal text needs 4.5:1 for AA, and 3.4:1 only meets the 3:1 threshold for large text. The text is 16px regular, so it isn't large. Fix 1: darken the text to #767676 or darker, which gives at least 4.54:1. A value like #595959 (7:1) also meets AAA. Fix 2: keep the gray and change the background to something darker only if the text color is also adjusted so the ratio reaches 4.5:1. Making the text large (24px regular or 18.66px bold) would also lower the requirement to 3:1, but that changes the scale, so darkening the text is the right fix.
Exercise
Why is pure #ffffff text on a #000000 background not always the best choice for a dark theme, even though it has the highest possible contrast ratio?
Show answer
The 21:1 ratio is very high, but the glare makes light text appear to bleed into the dark background (halation). This is tiring over long reading and is worse for readers with astigmatism. An off-white on an off-black, such as #e6e6e6 on #121212, still gives about 15:1 and is more comfortable.