How W3C WCAG 2.1 Computes Relative Luminance and Contrast
Human vision is non-linear and far more sensitive to green wavelengths than blue wavelengths. Because of that, simple RGB averages fail miserably at predicting legibility: pure blue (#0000FF) and pure green (#00FF00) have identical RGB channel sums, yet pure green appears nearly ten times brighter to the human eye!
W3C's WCAG 2.1 specification first linearizes each gamma-encoded 8-bit sRGB channel (dividing by 255 and applying the 2.4 gamma transfer curve above 0.03928), then weights the linear channels according to human photopic sensitivity: L = 0.2126 × R + 0.7152 × G + 0.0722 × B.
Finally, the contrast ratio between the lighter luminance L1 and darker luminance L2 adds a 0.05 ambient flare offset: Ratio = (L1 + 0.05) / (L2 + 0.05), yielding values from 1.00:1 (identical colors) up to 21.00:1 (pure white #FFFFFF on pure black #000000).
function channelToLinear(c8bit: number): number {
const s = c8bit / 255;
return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
}
export function wcagContrast(lumA: number, lumB: number): number {
const lighter = Math.max(lumA, lumB);
const darker = Math.min(lumA, lumB);
return (lighter + 0.05) / (darker + 0.05);
}Understanding WCAG AA and AAA Compliance Thresholds
Under WCAG 2.1 Success Criterion 1.4.3 (Level AA—the baseline legal standard referenced by ADA, Section 508, and the European Accessibility Act), normal body text must achieve at least a 4.5:1 contrast ratio against its background.
Large-scale text—defined strictly as at least 18pt (24px) at regular weight or 14pt (~18.66px) at bold weight (font-weight >= 700)—requires at least 3.0:1. Non-text interactive UI boundaries (input borders, focus rings, icons conveying state) also require 3.0:1 under SC 1.4.11.
Enhanced Level AAA (SC 1.4.6), recommended for long-form documentation, code editors, and clinical/financial dashboards, raises requirements to 7.0:1 for normal text and 4.5:1 for large text.
The Two Classic SaaS Failures: White-on-Cyan Buttons and Slate-400 Text
Bright cyan (#06B6D4), emerald (#10B981), and amber (#F59E0B) look vibrant as brand accents on dark #0F172A surfaces. However, placing pure white (#FFFFFF) button labels on #06B6D4 yields a dismal 2.43:1 contrast ratio because cyan contains high green luminance!
To make vibrant brand buttons accessible in both themes, either darken the button fill to the 600/700 shade (#0891B2 / #0E7490) for white text, or pair bright 400/500 fills with dark #020617 text (which achieves > 10:1 contrast).
Similarly, Tailwind's slate-400 (#94A3B8) achieves 7.05:1 on slate-950 (#020617) in dark mode, but drops to 2.56:1 on white (#FFFFFF) in light mode—always map muted text tokens semantically (slate-600 in light mode, slate-400 in dark mode).
Auditing Gradients and Glassmorphism Overlays
When text sits atop a CSS linear-gradient() or a translucent backdrop-filter card, audit the contrast against the worst-case (lowest-contrast) color stop behind the text rather than the average color, and include a solid fallback background-color.
WCAG 2.1 Contrast Requirements by Element Type
| UI Element Category | CSS Size / Weight | WCAG AA Minimum | WCAG AAA Target |
|---|---|---|---|
| Body copy, labels, code | < 24px regular (or < 18.66px bold) | 4.5 : 1 | 7.0 : 1 |
| Large headings | >= 24px regular (or >= 18.66px bold) | 3.0 : 1 | 4.5 : 1 |
| Form borders, icons, focus rings | Essential interactive graphics | 3.0 : 1 | 4.5 : 1 |
| Disabled controls / decorative | Inactive / purely ornamental | Exempt | Exempt |
Production Engineering Checklist
- Audit both dark mode and light mode token pairs separately—a shade that passes AAA on dark slate often fails AA on white.
- Avoid pure white (#FFFFFF) text on bright cyan, green, or yellow button backgrounds; use 700-weight dark fills or dark text.
- Ensure form input borders and keyboard focus outlines meet the 3.0:1 non-text contrast rule.
- Avoid pure #000000 + #FFFFFF halation strain in dark mode by using rich dark surfaces (#0B1222 / #0F172A) with #F8FAFC text (~17:1).
Frequently Asked Questions
Why do my eyes hurt reading pure white text on pure black (#000000)?
Maximum 21:1 contrast on emissive OLED screens causes halation (chromatic/astigmatic blur) for many readers. Using #F1F5F9 or #F8FAFC on #0F172A delivers ~16–17:1 contrast—comfortably exceeding AAA (7:1) without harsh glare.
Do placeholder attributes in inputs need 4.5:1 contrast?
If placeholder text communicates essential format instructions not shown in a visible label, yes. Best practice is always pairing inputs with persistent, high-contrast <label> elements.