Color Formats Explained
Contrast ratio compares the relative luminance of two colors: (L1 + 0.05) divided by (L2 + 0.05), where L1 is the lighter color. WCAG AA requires at least 4.5:1 for normal text and 3:1 for large text. Pick a color above to get its HEX, RGB, HSL, HSV and CMYK values, tints and shades, and its contrast ratio against white and black.
Every color on screen can be represented in multiple formats. HEX (#RRGGBB) is a base-16 encoding of RGB values: the standard for web development and CSS. RGB (Red, Green, Blue) represents colors as three integers from 0 to 255, matching how screens physically display color. HSL (Hue, Saturation, Lightness) is more intuitive for humans: hue is the color angle (0 to 360°), saturation is vividness, lightness is brightness. HSV/HSB uses brightness instead of lightness.
HEX Color Codes
WCAG Contrast
Tints and Shades
CSS Custom Properties
How the WCAG Contrast Ratio Is Calculated
- Scale each channel to 0 to 1. Divide R, G and B by 255.
- Linearize. If a value c is 0.04045 or less, use c ÷ 12.92; otherwise use ((c + 0.055) ÷ 1.055)2.4. Older WCAG text says 0.03928 instead of 0.04045; for 8-bit colors both give identical results.
- Relative luminance. L = 0.2126 R + 0.7152 G + 0.0722 B, using the linearized values. Green weighs most because the eye is most sensitive to it. Black is 0, white is 1.
- Contrast ratio. (L1 + 0.05) ÷ (L2 + 0.05), with L1 the lighter color. The result runs from 1:1 (same color) to 21:1 (black on white).
Worked example: #7C3AED on white. The channels are 124, 58 and 237, which scale to 0.4863, 0.2275 and 0.9294 and linearize to 0.2016, 0.0423 and 0.8469. Luminance is 0.2126 × 0.2016 + 0.7152 × 0.0423 + 0.0722 × 0.8469 = 0.1343. Against white: (1 + 0.05) ÷ (0.1343 + 0.05) = 5.70:1, which passes AA for all text but not AAA (7:1).
WCAG Contrast Thresholds
| What | Level AA | Level AAA | WCAG rule |
|---|---|---|---|
| Normal text | 4.5:1 | 7:1 | 1.4.3 and 1.4.6 |
| Large text (24 px, or 18.66 px bold) | 3:1 | 4.5:1 | 1.4.3 and 1.4.6 |
| Icons, input borders, focus rings, chart lines | 3:1 | no higher level | 1.4.11 |
| Logos and disabled controls | no requirement | no requirement | exempt |
Large text means at least 18 point (24 CSS pixels) regular, or 14 point (about 18.66 CSS pixels) bold. The ratio is not rounded up: 4.49:1 fails a 4.5:1 requirement.
Contrast of Common Colors on White and Black
Computed with the formula above. "Best text" is whichever of white or black gives the higher ratio, which is what the preview swatch on this page now uses.
| Color | On white | On black | Best text | Normal text AA |
|---|---|---|---|---|
| #333333 dark gray | 12.63:1 | 1.66:1 | White | Passes (with white) |
| #595959 gray | 7.00:1 | 3.00:1 | White | Passes, also AAA |
| #767676 gray | 4.54:1 | 4.62:1 | Black | Passes with either |
| #777777 gray | 4.48:1 | 4.69:1 | Black | Fails on white |
| #6B7280 slate gray | 4.83:1 | 4.34:1 | White | Passes on white only |
| #7C3AED violet | 5.70:1 | 3.69:1 | White | Passes on white |
| #2563EB blue | 5.17:1 | 4.06:1 | White | Passes on white |
| #3B82F6 light blue | 3.68:1 | 5.71:1 | Black | Large text only on white |
| #DC2626 red | 4.83:1 | 4.35:1 | White | Passes on white |
| #EF4444 light red | 3.76:1 | 5.58:1 | Black | Large text only on white |
| #16A34A green | 3.30:1 | 6.37:1 | Black | Large text only on white |
| #F97316 orange | 2.80:1 | 7.49:1 | Black | Fails on white |
| #EAB308 yellow | 1.92:1 | 10.95:1 | Black | Fails on white |
The pattern to remember: bright greens, oranges and yellows almost never work as text on white. Use a darker shade from the Shades row above, or put black text on them instead.
Converting Between Color Formats
All formats describe the same sRGB color. Taking #7C3AED as the example:
- HEX to RGB: 7C = 7 × 16 + 12 = 124, 3A = 58, ED = 237, so rgb(124, 58, 237).
- RGB to HSL: max 237, min 58. Lightness is (max + min) ÷ 2 ÷ 255 = 57.8%. Saturation is (max − min) ÷ (510 − max − min) = 179 ÷ 215 = 83.3%. Blue is the largest channel, so hue = 60 × ((R − G) ÷ (max − min) + 4) = 262.1°. Result: hsl(262, 83%, 58%).
- RGB to HSV: value is max ÷ 255 = 92.9%, saturation (max − min) ÷ max = 75.5%, same hue: hsv(262, 76%, 93%).
- RGB to CMYK: K = 1 − max ÷ 255 = 7.1%, then C = (1 − R/255 − K) ÷ (1 − K) = 47.7%, M = 75.5%, Y = 0%: cmyk(48, 76, 0, 7). This is the simple formula; print shops convert with an ICC profile for their press, so expect their numbers to differ.