Language: English

tools / color

color/

Convert between HEX, RGB, HSL and OKLCH, pick colors, and check WCAG contrast against black and white.

Preview

Formats

HEX
RGB
HSL
OKLCH

CSS variables


      

Contrast (WCAG 2.x)

Normal text AA ≥ 4.5 · AAA ≥ 7; large text AA ≥ 3 · AAA ≥ 4.5
On white Aa Sample text
On black Aa Sample text

What are HEX, RGB, HSL and OKLCH

HEX, RGB, HSL and OKLCH are four CSS ways of writing the same color: the first three describe sRGB red, green and blue or hue, and OKLCH has a lightness closer to how the eye perceives it. This page converts between the four and computes the WCAG 2.x contrast of the color against pure white and pure black, which ranges from 1 to 21.

How it works: convert to sRGB, then compute relative luminance

Conversion first brings the input to the three sRGB channels red, green and blue plus alpha: HSL computes the three channels from where the hue falls, and OKLCH is turned into OKLab, then into linear sRGB, and finally passed through the sRGB transfer function; output runs the same steps in reverse.

Contrast follows WCAG 2.x: each channel is first made linear (divided by 12.92 when it is at most 0.04045, otherwise ((c + 0.055) ÷ 1.055) raised to the power 2.4), then weighted by 0.2126, 0.7152 and 0.0722 to get the relative luminance L, and contrast = (lighter L + 0.05) ÷ (darker L + 0.05). Pure white has L = 1 and pure black 0, so white against black is 21.

Example

Pure red in HSL is hue 0, saturation 100% and lightness 50%; its HEX form is below. Its contrast is 4 on white and 5.25 on black.

Input
hsl(0 100% 50%)
Output
#ff0000

Sources

FAQ

Which color formats can I type, and can I use a name like red?

Color names are not supported; you will get an "Unrecognized format" message. You can enter HEX (3, 4, 6 or 8 digits, # optional), rgb() / rgba(), hsl() / hsla() and oklch(), with either comma-separated arguments or spaces plus a slash for opacity (for example rgb(255 0 0 / 50%)). The hue in HSL may carry a deg, rad, grad or turn unit. The output is always HEX, RGB, HSL and OKLCH; there is no CMYK or HSV.

Is the alpha in an 8-digit HEX first or last, and why does 80 become 0.502?

It is the last two digits, written #RRGGBBAA, the same as in CSS. The #AARRGGBB form commonly used on Android puts alpha first, so move the first two digits to the end before pasting. Two hex digits give only 256 steps, so 80 (128 in decimal) converts to 0.502 rather than a round 0.5; going the other way, an alpha of 0.5 produces 80 at the end of the HEX.

What happens to out-of-range values, and what does "Outside the sRGB gamut" mean for OKLCH?

When a color falls outside what sRGB can show, the tool clips the red, green and blue channels to the valid range separately and outputs the clipped color. Values such as rgb() above 255 or an HSL saturation or lightness above 100% are clipped silently; only OKLCH shows the "Outside the sRGB gamut" notice, because it can describe colors more vivid than sRGB. The OKLCH in the output is then recalculated from the clipped color, so it differs from what you entered. On a wide-gamut display, writing oklch() directly in CSS can look more vivid than the preview here.

Can it compute the contrast between any two colors?

No. The tool only compares your color with pure white and pure black, treating it as the text color on a white and on a black background; you cannot pick another background. For any two colors, compute the relative luminance of each with the formula under "How it works" above and put them into the contrast formula.

What do AA, AAA and "Large" mean?

AA and AAA are WCAG's two conformance levels for text contrast, and AAA is stricter. For normal text, AA needs 4.5 : 1 and AAA needs 7 : 1; for large text, AA needs 3 : 1 and AAA needs 4.5 : 1. The tool marks each one as pass or fail. "Large" text means 18pt (about 24px) and up, or bold text at 14pt (about 18.7px) and up. For websites, AA is usually the target.

How is contrast calculated for a semi-transparent color?

The color is first blended onto the background according to its opacity, and the contrast is calculated on the result. The "On white" row blends it over pure white and the "On black" row over pure black, so the same semi-transparent color is actually a different color in each row. If the real background on your page is not pure white or black, the result will differ, so check it yourself.

Can the color picker choose a semi-transparent color?

No. The picker is a built-in browser control and can only choose opaque 6-digit HEX colors. After you pick a color with it, the input box is replaced with #rrggbb and any earlier opacity is lost; for a semi-transparent color, type an 8-digit HEX or rgb(255 0 0 / 50%) directly in the input box. When the input box holds a semi-transparent color, the picker shows only its opaque part.

How do I use the "CSS variables" lines?

Copy them into a :root rule and they are ready to use. The four lines are the HEX, the three R G B numbers separated by spaces, the three H S L values, and the full oklch(). --color-rgb and --color-hsl carry no opacity, so you can adjust it on the spot, as in rgb(var(--color-rgb) / 50%); this space-and-slash syntax needs a reasonably recent browser. When the color itself is semi-transparent, only --color and --color-oklch include the opacity.