Color Theory and Accessibility for Developers
A color that looks fine on your monitor can be unreadable for someone with low vision, a glare-covered phone screen, or a color vision deficiency. Color is a functional part of an interface, and there are concrete numbers for whether it works.
This article covers the color models developers actually use, the WCAG contrast requirements, and a repeatable way to check your colors.
Color Models: RGB, HSL, and HSV
Knowing the three common color models helps you reason about color in code.
RGB (Red, Green, Blue)
RGB is the native color model of screens. Every pixel emits red, green, and blue light at intensities from 0 to 255. When all three channels are at 255, you get white. When all are at 0, you get black. This is additive color mixing, and it maps directly to how hardware works.
/* RGB in CSS */
color: rgb(59, 130, 246); /* A nice blue */
color: rgb(239, 68, 68); /* A warning red */
color: rgba(59, 130, 246, 0.5); /* Same blue, 50% transparent */
RGB suits computation and is hard to read as a person. It is not obvious what rgb(180, 83, 210) looks like, which is where HSL helps.
HSL (Hue, Saturation, Lightness)
HSL describes a color by hue on a 360-degree wheel (0 is red, 120 is green, 240 is blue), saturation (0% is gray, 100% is fully saturated), and lightness (0% is black, 100% is white, 50% is the pure color). Its lightness is not perceptual: hsl(60, 100%, 50%) (yellow) looks far brighter than hsl(240, 100%, 50%) (blue). See the CSS color post for OKLCH, which addresses this.
/* HSL in CSS */
color: hsl(217, 91%, 60%); /* That same blue */
color: hsl(0, 84%, 60%); /* That same red */
/* Want a darker version? Just lower lightness */
color: hsl(217, 91%, 40%); /* Darker blue */
/* Want a muted version? Lower saturation */
color: hsl(217, 30%, 60%); /* Desaturated blue */
HSL is convenient for generating variations in code. For a hover state, subtract 10 from the lightness. For a muted state, reduce the saturation. You can try these changes with the Color Picker.
HSV/HSB (Hue, Saturation, Value/Brightness)
HSV is similar to HSL but uses "value" instead of "lightness." The difference: in HSV, a value of 100% gives you the fully bright version of a color, while in HSL, lightness of 100% always gives white regardless of hue. HSV is common in color picker UIs (like the square picker in design tools), while HSL tends to be more useful in CSS.
Color Harmony: Building Palettes That Work
Color harmony describes combinations based on positions on the color wheel. Screens use an RGB wheel, while the pairings taught in art class (red and green, yellow and purple) come from the older red-yellow-blue wheel, so the same "complement" differs between the two. The examples below use HSL, which is the RGB wheel.
Complementary Colors
Two colors 180 degrees apart on the wheel. Blue and orange is a close example on the RGB wheel. Complementary pairs give strong contrast of hue, so they work better as an accent than in equal amounts.
/* Complementary pair */
--primary: hsl(220, 80%, 50%); /* Blue */
--complement: hsl(40, 80%, 50%); /* Orange */
Analogous Colors
Colors next to each other on the wheel, usually within about 30 degrees. They produce low-contrast, closely related palettes that suit backgrounds and gradients. The Color Palette Generator can produce these from a base color.
Triadic Colors
Three colors 120 degrees apart. On the HSL wheel, red, green, and blue form a triad. Usually one color dominates and the other two act as accents.
Split-Complementary
Instead of the direct complement, take the two colors 30 degrees either side of it. This keeps most of the contrast of a complementary pair with less tension between the two sides.
/* Split-complementary from blue */
--primary: hsl(220, 80%, 50%); /* Blue */
--split-1: hsl(10, 80%, 50%); /* Red-orange */
--split-2: hsl(70, 80%, 50%); /* Yellow-green */
The Gradient Generator shows how two of these colors blend into each other.
WCAG 2.1 Contrast Requirements
The Web Content Accessibility Guidelines (WCAG) set contrast ratio thresholds for text against its background. WCAG 2.2, the current version, keeps the same contrast requirements as 2.1. Many legal standards point to WCAG: the US Department of Justice's 2024 rule for state and local governments adopts WCAG 2.1 AA, and the European standard EN 301 549 incorporates it too.
The Two Levels
- AA (minimum): 4.5:1 for normal text, 3:1 for large text (18pt or 14pt bold), 3:1 for UI components and graphical objects
- AAA (enhanced): 7:1 for normal text, 4.5:1 for large text
Most organizations target AA. AAA is harder to reach, especially with brand colors, but it is a reasonable goal for long-form body text.
How Contrast Ratio Works
The contrast ratio comes from the relative luminance of the two colors, which weights each channel by how bright it looks to the human eye.
// Relative luminance calculation
function relativeLuminance(r, g, b) {
// Linearize each channel (remove gamma)
const rsRGB = r / 255;
const gsRGB = g / 255;
const bsRGB = b / 255;
const rLinear = rsRGB <= 0.04045
? rsRGB / 12.92
: Math.pow((rsRGB + 0.055) / 1.055, 2.4);
const gLinear = gsRGB <= 0.04045
? gsRGB / 12.92
: Math.pow((gsRGB + 0.055) / 1.055, 2.4);
const bLinear = bsRGB <= 0.04045
? bsRGB / 12.92
: Math.pow((bsRGB + 0.055) / 1.055, 2.4);
// Weighted sum: green contributes most
return 0.2126 * rLinear + 0.7152 * gLinear + 0.0722 * bLinear;
}
// Contrast ratio
function contrastRatio(lum1, lum2) {
const lighter = Math.max(lum1, lum2);
const darker = Math.min(lum1, lum2);
return (lighter + 0.05) / (darker + 0.05);
}
The result ranges from 1:1 (identical colors) to 21:1 (black on white). The green channel has a weight of 0.7152 and blue only 0.0722, which is why pure blue on a dark background is much harder to read than pure green. You can test any pair with the Contrast Checker.
Common Accessibility Mistakes
Light Gray Text on White
Subtle gray text for secondary content is a frequent failure. #999999 on #FFFFFF has a contrast ratio of just 2.85:1, which fails WCAG AA at every text size. The lightest gray that passes AA for normal text on white is about #767676 (4.54:1).
Color-Only Indicators
Using red for errors and green for success looks obvious, but about 8% of men have some form of color vision deficiency. If the only difference between a valid and an invalid field is a red or green border, those users may not be able to tell which is which. Pair color with an icon or text.
/* Bad: color only */
.input-error { border-color: red; }
.input-valid { border-color: green; }
/* Better: border color plus a visible message */
.field-error-message {
color: #b91c1c; /* 6.47:1 on white */
}
<!-- ::after does not work on <input>, so use a real element -->
<input id="email" class="input-error" aria-describedby="email-error">
<p id="email-error" class="field-error-message">
Enter a valid email address.
</p>
Placeholder Text as Labels
Placeholder text disappears as soon as someone starts typing, and browsers style it in a muted color that you should check against your own background. Never use it as the only label for a field. Use a real <label> element.
Focus Indicators Removed
Keyboard users rely on focus indicators to know where they are on the page. Removing them with outline: none makes the page very hard to use without a mouse. If you want a custom look, replace the outline with something equally visible, such as a :focus-visible style.
Dark Mode Considerations
Dark mode introduces its own set of contrast challenges. Colors that pass on a white background often fail on dark backgrounds, and vice versa.
:root {
--bg: #ffffff;
--text: #1a1a1a; /* Contrast: 17.4:1 */
--text-secondary: #6b7280; /* Contrast: 4.83:1 */
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0f0f0f;
--text: #f5f5f5; /* Contrast: 17.58:1 */
--text-secondary: #a0a0a0; /* Contrast: 7.33:1 */
}
}
Many designers avoid pure white text on pure black, because the extreme contrast can cause a halo effect for some readers, including people with astigmatism. Use a slightly off-white and off-black, and re-check every contrast ratio for both themes. Saturated blue or red that works on white can be unreadable on dark gray.
Colorblindness: Types and Design Strategies
Color vision deficiency affects roughly 1 in 12 men and 1 in 200 women, based on commonly cited figures for people of Northern European descent. There are three main types of partial color loss:
- Deuteranopia / Deuteranomaly (green-weak): The most common type, affecting about 6% of men. Red and green look very similar.
- Protanopia / Protanomaly (red-weak): About 2% of men. Red appears darker, and red-green distinction is poor.
- Tritanopia / Tritanomaly (blue-weak): Very rare (less than 0.01%). Blue and yellow are confused.
Designing Inclusively
The practical strategies are straightforward:
- Never rely on color alone to convey information. Add icons, patterns, labels, or text.
- Use blue and orange instead of red and green when you need a two-color distinction. This pair is distinguishable by virtually all types of color vision deficiency.
- Test with simulation. Tools that simulate different types of colorblindness show you exactly what your interface looks like to affected users.
- Maintain sufficient contrast. Text that passes WCAG AA is usually still readable with color vision deficiency, because it relies on lightness differences. Reds can look noticeably darker with protanopia, so check them in a simulator.
/* Status colors checked against a white background */
:root {
--status-error: #b91c1c; /* 6.47:1 */
--status-success: #15803d; /* 5.02:1 */
--status-warning: #b45309; /* 5.02:1 */
--status-info: #1d4ed8; /* 6.70:1 */
}
/* Red and green here are only 1.29:1 against each other,
so the text label has to carry the meaning. */
.alert-error::before { content: "Error: "; }
.alert-success::before { content: "Success: "; }
Practical Workflow: Checking Your Colors
A repeatable order of checks:
- Start with a palette. Use the Color Palette Generator to create a harmonious set of base colors.
- Check every text/background pair. Plug your foreground and background colors into the Contrast Checker. Fix any pair that falls below 4.5:1 for normal text.
- Generate accessible variants. If a color fails, the Contrast Checker suggests lighter or darker versions with the same hue that meet the threshold.
- Test interactive states. Check hover, focus, and active states. WCAG exempts disabled controls from its contrast requirements, but they should still be recognizable.
- Verify in both themes. If you support dark mode, run the same checks on your dark palette.
- Simulate color vision deficiency. Load a screenshot into the Color Blindness Simulator, or use the Rendering panel in Chrome DevTools.
The Color Picker is useful throughout this process for quickly converting between RGB, HSL, and hex as you adjust values.
Quick Reference: Minimum Contrast Values
| Element | WCAG AA | WCAG AAA |
|--------------------------|----------|----------|
| Normal text (<18pt) | 4.5:1 | 7:1 |
| Large text (>=18pt) | 3:1 | 4.5:1 |
| Bold text (>=14pt bold) | 3:1 | 4.5:1 |
| UI components | 3:1 | None set |
| Non-text graphics | 3:1 | None set |
| Decorative elements | None | None |
A good first step is to run the main text and background pairs of your current project through the Contrast Checker.