Color Converter and Contrast Checker: HEX, RGB, HSL, CMYK and WCAG
By FindMySum · Last reviewed
A color converter translates one colour between the notations designers and developers use: HEX codes in CSS, RGB triples, HSL and HSV values from colour pickers and CMYK percentages for print. This one also checks the colour against a background using the contrast formula from the Web Content Accessibility Guidelines (WCAG) 2.2, and suggests the nearest colour that passes when yours does not. This article explains how each format works, how the conversions are done, how the contrast ratio is calculated and how to read the pass and fail results.
RGB and HEX: the same numbers in two bases
Screens make colour by mixing red, green and blue light. In the sRGB colour space used on the web, each channel is a whole number from 0 (off) to 255 (full), so rgb(29, 78, 216) is a little red, some green and a lot of blue.
A HEX code writes the same three numbers in base 16, two digits each. In #1d4ed8, 1d is 1 × 16 + 13 = 29, 4e is 4 × 16 + 14 = 78 and d8 is 13 × 16 + 8 = 216. Converting back is the reverse. A three-digit shorthand repeats each digit, so #abc is #aabbcc. Because the conversion is exact in both directions, HEX and RGB never lose information.
HSL and HSV: colours by hue
HSL describes the same colour as a hue angle on the colour wheel (0° red, 120° green, 240° blue), a saturation and a lightness. It is easier to reason about: keep the hue and lower the lightness for a darker shade, or reduce the saturation for a muted one. CSS supports it directly with hsl(). The calculator follows the CSS Color Module Level 4 algorithm: lightness is the average of the largest and smallest channel, and saturation is the spread between them relative to how far the lightness sits from black or white.
HSV (also called HSB) uses the same hue but a different second and third value. Value is simply the largest channel, so every pure colour has a value of 100%, which matches the square-and-slider pickers in many design tools. Converting to HSL or HSV involves rounding, so a round trip back to RGB can be off by one in a channel.
CMYK: a rough guide for print
Printers subtract light with cyan, magenta, yellow and black ink. The simple conversion sets black first, K = 1 − (largest channel ÷ 255), then works out how much of each coloured ink is needed for the rest: C = (1 − R − K) ÷ (1 − K), and the same for M and Y. That gives a reasonable starting point, but real printed colour depends on the press, ink and paper, described by an ICC colour profile. Bright screen colours such as vivid blues and greens often fall outside what ink can reproduce, so use your design software's profile-based conversion for anything going to a printer.
How contrast is measured
WCAG compares colours by relative luminance, a measure of how bright a colour looks to the eye, from 0 for black to 1 for white. First, each channel is divided by 255 and converted from the sRGB curve to linear light: values up to 0.04045 are divided by 12.92, and higher values become ((c + 0.055) ÷ 1.055) raised to the power 2.4. Then the channels are weighted by how sensitive the eye is to each: L = 0.2126 R + 0.7152 G + 0.0722 B. Green counts almost ten times as much as blue, which is why pure blue text looks dark even though one channel is at full strength.
The contrast ratio puts the lighter luminance over the darker, each with 0.05 added to model ambient light reflecting off the screen:
contrast ratio = (L lighter + 0.05) ÷ (L darker + 0.05)
It ranges from 1:1 for identical colours to 21:1 for black on white.
Worked example with the default colours
The calculator opens with #1d4ed8, a strong blue, on white.
- RGB: 29, 78 and 216, which as fractions are 0.1137, 0.3059 and 0.8471.
- Linearised: 0.0123, 0.0762 and 0.6867.
- Relative luminance: 0.2126 × 0.0123 + 0.7152 × 0.0762 + 0.0722 × 0.6867 = 0.1067.
- White has a luminance of 1, so the ratio is (1 + 0.05) ÷ (0.1067 + 0.05) = 6.70:1.
That passes Level AA for normal text (4.5:1) and large text (3:1) and Level AAA for large text (4.5:1), but not AAA for normal text, which needs 7:1. The same colour converts to hsl(224, 76.3%, 48%), hsv(224, 86.6%, 84.7%) and roughly cmyk(87%, 64%, 0%, 15%).
Reading the results
WCAG 2.2 sets several thresholds. Success Criterion 1.4.3 (Level AA) needs 4.5:1 for normal text and 3:1 for large text, meaning at least 24 CSS pixels regular or about 18.66 pixels bold. Criterion 1.4.6 (AAA) raises those to 7:1 and 4.5:1. Criterion 1.4.11 needs 3:1 for the visual parts of controls, such as input borders, focus rings and icons that carry meaning.
WCAG is explicit that ratios are not rounded when testing: 4.499:1 fails 4.5:1. The calculator decides pass and fail on the exact value and displays the ratio rounded down, so the familiar grey #777777 on white shows 4.47:1 and fails, while #767676 shows 4.54:1 and passes.
When a colour fails normal-text AA, the calculator keeps its hue and saturation and nudges the HSL lightness in small steps until the contrast reaches 4.5:1, then shows the closest result. That keeps a brand colour recognisable while making it readable. If no lightness works against the chosen background, change the background instead.
More questions
Does dark mode need different colours for good contrast?
Usually, yes. A brand blue that passes 4.5:1 on white often fails on a near-black background, because contrast depends on both colours. Check each text colour against each background it will appear on, in both themes. Pure white text on pure black (21:1) can feel harsh for long reading, so many dark themes use an off-white on a dark grey, which still passes AAA comfortably.
Can colour alone be used to show an error or a required field?
No. WCAG Success Criterion 1.4.1 says colour must not be the only way information is conveyed, because about 1 in 12 men and 1 in 200 women have some form of colour vision deficiency. Pair a red border with an icon or text such as 'Required' or 'Error: enter a valid email'. Contrast between the red and its surroundings still needs to meet 3:1 for the border to count as a visible component.
Why do the same HEX colours look different on different screens?
HEX and RGB values are instructions, not measurements of light. A screen's brightness, colour temperature, panel type and calibration all change how a value appears, and wide-gamut displays may stretch sRGB colours unless the browser colour-manages them. The contrast ratio is calculated from the sRGB definition, so it stays the same even though the perceived colours shift slightly between devices.
What is APCA and will it replace the WCAG contrast ratio?
APCA (the Accessible Perceptual Contrast Algorithm) is a newer method being developed for the future WCAG 3. It accounts for font size and weight and for the fact that light text on dark backgrounds behaves differently from dark on light. It is still a draft and is not part of any published standard, so WCAG 2.2's contrast ratio remains the measure used for conformance and in accessibility laws.
Which web-safe or named colours pass contrast on white?
Among the CSS named colours, black, navy, maroon, purple, green, teal and darkred all pass 4.5:1 on white, while gray (#808080, 3.95:1), red (#ff0000, 4.0:1) and orange fail for normal text. The lightest neutral grey that passes AA on white is #767676 at 4.54:1. Type any of them into the calculator to see the exact ratio and the levels it meets.
Sources
This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.