Color Formats and Contrast: HEX, RGB, HSL and WCAG Explained
Designers describe colors in several formats and need to know whether text stays readable on top of them. Here is how it works.
HEX, RGB and HSL
- RGB gives the amount of red, green and blue light, each from 0 to 255. For example, rgb(11, 124, 136) is a teal.
- HEX is the same information in base 16. #0B7C88 means red 0B, green 7C and blue 88. A three-digit code such as #FA0 is shorthand for #FFAA00.
- HSL describes hue (the color wheel angle from 0 to 360), saturation and lightness. It is the easiest format for making a color lighter, darker or less intense.
What contrast ratio means
The contrast ratio compares the brightness of two colors, from 1:1 (identical) to 21:1 (black on white). The Web Content Accessibility Guidelines (WCAG) set minimums so that people with low vision can read text.
- AA: 4.5:1 for normal text and 3:1 for large text and interface elements.
- AAA: 7:1 for normal text and 4.5:1 for large text.
Large text is generally 18 point, or 14 point bold, and above.
Examples
| Text | Background | Contrast ratio | AA normal text (4.5) | AA large text (3) | AAA normal text (7) |
|---|---|---|---|---|---|
| #000000 | #FFFFFF | 21:1 | Pass | Pass | Pass |
| #1C2024 | #FBFCFA | 15.92:1 | Pass | Pass | Pass |
| #767676 | #FFFFFF | 4.54:1 | Pass | Pass | Fail |
| #999999 | #FFFFFF | 2.85:1 | Fail | Fail | Fail |
| #0B7C88 | #FFFFFF | 4.94:1 | Pass | Pass | Fail |
| #FFFFFF | #B5195C | 6.44:1 | Pass | Pass | Fail |
| #FFFFFF | #B9770E | 3.68:1 | Fail | Pass | Fail |
Practical tips
- Gray text is the most common failure. Light gray on white often looks elegant and is hard to read.
- Text over photos needs a darker overlay or a solid panel behind it.
- Do not rely on color alone to convey meaning. Add icons, labels or patterns.
- Check dark mode separately. A color that works on white may fail on black.
- Use our color converter and contrast checker to test your own pairs.
Why contrast matters
Contrast is not only for people with a diagnosed visual impairment. Text that is easy to read on a laptop in an office becomes hard to read on a phone in bright sunlight, on a dim screen, or for someone who is tired or older. Color vision differences are also common: roughly one in twelve men and about one in two hundred women have some form of color vision deficiency. A design that depends on telling certain colors apart can fail for a noticeable share of visitors.
Worked examples with a teal and a yellow
Using the teal #0B7C88 from the example above, and a bright yellow, here is how different text colors perform:
| Text color | Background | Contrast ratio | Normal text (4.5:1) | Large text (3:1) |
|---|---|---|---|---|
| White | #0B7C88 | 4.94:1 | Pass | Pass |
| Black | #0B7C88 | 4.25:1 | Fail | Pass |
| White | #FFAA00 | 1.91:1 | Fail | Fail |
| Black | #FFAA00 | 11.00:1 | Pass | Pass |
The lesson is that white text is not automatically the safe choice on a colored button. A bright yellow needs dark text, while a dark teal can take white. The tool above computes the exact figure for any pair, so check before you commit to a brand color.
Beyond the ratio
- Do not rely on color alone. Pair color with an icon, a label or a pattern, for example an error icon next to a red message.
- Check interface parts. Buttons, form borders, icons and focus outlines need at least 3:1 against their surroundings.
- Test text over images. A caption on a photograph can pass in one corner and fail in another. Test the worst area, or add a solid or semi-transparent panel behind the text.
- Mind light gray text. The lightest gray that passes 4.5:1 on white is #767676, at 4.54:1. Placeholder text and captions are often lighter than that.
- Check dark mode separately. A palette that passes in light mode can fail when the colors are inverted.
A simple testing routine
- List the text and background pairs that appear in your design, including hover and disabled states.
- Check each pair, and note the ones that fail.
- Adjust the lightness of the text or the background, which is easier to do in HSL than in HEX, until the pair passes.
- View the page on a phone outdoors, or with the screen brightness lowered, as a reality check.
Try the tool
Color Converter and Contrast CheckerConvert HEX to RGB and HSL and check text contrast against WCAG accessibility guidelines.
About the author. Darkroom Bench is created and maintained by Andrés, a nature photographer from Argentina with two decades in southern Patagonia and more than 50 trips to Antarctica, whose work has received international awards. Read more or follow the photography on Instagram: @yosoytato.