Sub topic 7
Color Contrast and Legibility
Earlier chapters used contrast as a tool for hierarchy: size, weight, and color make one element stand out from another. This chapter covers a narrower question. Can people actually read the text and see the controls? Legibility is the point where contrast stops being a style choice and becomes a measurable requirement.
This chapter assumes you know basic typography vocabulary (size, weight, line spacing), can read hex colors such as #1a1a1a, and can follow short CSS and Python snippets.
Why "looks fine to me" is not a test
You judge your own layout on a good monitor, at full brightness, in a comfortable room, with healthy eyes. Your readers use phones in sunlight, dim laptop screens, projectors, and older eyes. Many have low vision or color vision deficiency. A light gray caption that looks refined on your display can disappear for them.
Color contrast is the difference in brightness between a foreground color and its background. The key word is brightness, not hue. Red text on a green background can be vivid and still unreadable, because the two colors have nearly the same luminance. Luminance is how light a color is as perceived by the eye, with green counting for the most and blue for the least.
The contrast ratio
The Web Content Accessibility Guidelines (WCAG) define contrast as a ratio between the relative luminance of the lighter color (L1) and the darker color (L2):
ratio = (L1 + 0.05) / (L2 + 0.05)The ratio runs from 1:1 (identical colors) to 21:1 (black on white). Relative luminance comes from the sRGB channels, each converted to a 0–1 value and then linearized, because screen values are not proportional to emitted light:
- Divide each channel (R, G, B) by 255.
- If the value is 0.04045 or less, divide it by 12.92. Otherwise compute
((value + 0.055) / 1.055) ^ 2.4. - Combine:
L = 0.2126 R + 0.7152 G + 0.0722 B.
The weights show why pure blue text on black is hard to read: blue contributes only about 7% of the luminance.
The thresholds
WCAG 2.x sets minimum ratios by what is being displayed.
| Content | Level AA minimum | Level AAA minimum |
|---|---|---|
| Body text (under the large-text size) | 4.5:1 | 7:1 |
| Large text | 3:1 | 4.5:1 |
| UI components and meaningful graphics (borders of inputs, icons, focus rings) | 3:1 | no separate requirement |
| Decorative elements, disabled controls, logos | no requirement | no requirement |
Large text means at least 24px (18pt) at regular weight, or at least about 18.66px (14pt) at bold weight. Level AA is the common legal and procurement target. Treat AAA as a goal for long-form reading rather than a universal rule, since WCAG itself notes that AAA cannot be satisfied for all content.
Computing the ratio yourself
Design tools and browser dev tools report ratios, but computing one once makes the formula concrete and lets you check a whole palette automatically. This script runs as shown with any Python 3.
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_ratio(fg: str, bg: str) -> float:
lighter, darker = sorted((luminance(fg), luminance(bg)), reverse=True)
return (lighter + 0.05) / (darker + 0.05)
pairs = [
("#000000", "#ffffff"),
("#1a1a1a", "#ffffff"),
("#0b5fff", "#ffffff"),
("#767676", "#ffffff"),
("#999999", "#ffffff"),
]
for fg, bg in pairs:
ratio = contrast_ratio(fg, bg)
verdict = "passes AA body text" if ratio >= 4.5 else "fails AA body text"
print(f"{fg} on {bg}: {ratio:5.2f}:1 {verdict}")The output includes 21.00:1 for black on white, about 4.54:1 for #767676 (the lightest neutral gray that still passes AA on white), and about 2.85:1 for #999999, which fails. The accent blue #0b5fff from the repetition chapter gives about 5.1:1 on white, so it works both as link text and as a button background under white text.
Gray #767676 is only just over the line. Rounding matters here: #777777 measures about 4.48:1 and fails. Never round a ratio up to pass. Compare the unrounded value to the threshold.
Building contrast into your tokens
Contrast is easiest to maintain when it is decided once, at the token level, instead of being checked on every component. Name the pairs that have been verified and use only those.
:root {
--color-surface: #ffffff;
--color-text: #1a1a1a; /* about 17:1 on surface */
--color-text-muted: #595959; /* about 7:1 on surface */
--color-accent: #0b5fff; /* about 5.1:1 on surface */
--color-on-accent: #ffffff; /* about 5.1:1 on accent */
--color-border-control: #767676; /* 4.5:1, above the 3:1 needed for UI */
}
body {
background: var(--color-surface);
color: var(--color-text);
font-size: 1rem;
line-height: 1.5;
}
.caption {
color: var(--color-text-muted);
font-size: 0.875rem;
}
.button {
background: var(--color-accent);
color: var(--color-on-accent);
border: 2px solid transparent;
padding: 8px 16px;
}
input {
border: 1px solid var(--color-border-control);
}
:focus-visible {
outline: 3px solid var(--color-accent);
outline-offset: 2px;
}Two points are easy to miss. The muted text token exists so that secondary information can be quieter without falling below 4.5:1. Hierarchy comes from size and weight first, and a restrained color step second. The input border and focus ring are non-text elements, so they need 3:1 against the surface they sit on. A pale gray #d0d0d0 border, common in default-looking forms, is only about 1.5:1 and makes the field hard to find.
What the ratio does not tell you
Passing 4.5:1 is necessary but not sufficient. Check these separately.
Size, weight, and typeface
Thin or light-weight fonts lose strokes at small sizes. A hairline typeface at 12px can be hard to read at a passing ratio, while a sturdy sans serif at the same ratio reads well. Prefer a regular weight (400) or heavier for body text. Keep light weights for large display text, where strokes are wide enough to hold up.
Text over images and gradients
A ratio is defined for two flat colors. Over a photo, the background varies, and the worst spot decides legibility. Fix it by placing a solid or semi-transparent scrim behind the text, or by putting the text in a solid panel. Then measure against the scrim, not the photo. Test the lightest and darkest regions of the image the text crosses.
.hero {
position: relative;
background: url("hero.jpg") center / cover;
}
.hero__title {
display: inline-block;
padding: 16px 24px;
background: rgba(0, 0, 0, 0.7);
color: #ffffff;
}States and themes
Hover, focus, visited, selected, and error states each have their own colors, and each must pass. Disabled controls are exempt from the requirement, but a disabled look should still be distinguishable from an enabled one. Dark mode needs its own pass. Inverting a palette does not preserve the ratios, and pure white text on pure black can cause halation (glare) for some readers, so many designers use an off-white on a very dark gray.
Color as the only signal
About 1 in 12 men and 1 in 200 women have some form of color vision deficiency. Red versus green is the classic confusion. If an error is shown only by turning a field's border red, some users will not see it. Add a second cue: an icon, a text message, a label, or an underline on links.
A workflow for checking a layout
In practice:
- Measure the pairs in your token file first. This covers most of the interface.
- Use a browser's dev tools or a checker to confirm the computed colors, since opacity and overlays change the final color.
- Test on a real phone at low brightness, or simulate color vision deficiency in your design tool.
- Zoom the page to 200% and confirm text still fits and stays readable.
Common failures
- Light gray placeholder and caption text. Often 2.5:1 to 3:1. Darken it to at least 4.5:1.
- White text on a mid-tone brand color. Orange, light green, and sky blue often measure below 3:1 against white. Use dark text on them, or darken the brand color for text use.
- Opacity used to make text "softer".
opacity: 0.5on dark text lowers its contrast in ways that are easy to miss. Define a muted color token instead. - Contrast checked only in the light theme. The dark theme ships with unverified pairs.
- Borderless inputs. A field with a white fill on a white page has no visible boundary. Add a border at 3:1 or higher.
Connection to the four principles
Contrast as a principle asks you to make differences obvious. Accessible contrast sets a floor: the difference must be large enough to perceive for everyone. The repetition principle supports it, because tokens let one verified color pair be reused across the whole interface instead of rechecked for each component.
Exercise
A caption uses #777777 text on a white background at 14px regular weight. The measured ratio is about 4.48:1. Does it meet WCAG AA, and what is the smallest change that fixes it?
Show answer
It fails. Body-size text needs 4.5:1 for AA, and 14px regular weight does not count as large text. Darken the text slightly, for example to #767676 (about 4.54:1) or better, to a darker gray such as #595959 (about 7:1), which also gives comfortable margin on lower-quality screens.
Exercise
A form marks invalid fields only by changing the border from gray to red. Name two problems with this and one fix for each.
Show answer
First, some users cannot distinguish the red from the gray, so color alone carries the meaning. Fix it by adding an icon and an error message next to the field. Second, the new border color may not reach 3:1 against the background. Fix it by measuring the red border against the page color and darkening it until it reaches at least 3:1.
Exercise
White headline text sits on a busy photograph, and the designer measured contrast against the photo's average color. Why is this unreliable, and what should be done?
Show answer
Legibility depends on the worst region the text crosses, not the average. A bright patch can drop the local ratio far below the target. Place a solid or semi-transparent dark panel behind the text, then measure white against the panel's resulting color.