HEX vs RGB vs HSL vs OKLCH: CSS Color Formats Explained
HEX, RGB and HSL describe the same sRGB colors; OKLCH describes how bright they look. See how each format works, how to convert, and when to use which.
#3b82f6, rgb(59 130 246), hsl(217 91% 60%) and oklch(0.62 0.19 260) are all the same blue. HEX and RGB spell out how much red, green and blue to mix, HSL rearranges those numbers into hue, saturation and lightness, and OKLCH describes the color by how bright and how colorful it looks to a person. The first three are interchangeable notations. The fourth is the one that behaves predictably when you build palettes and check contrast.
You can paste any of these formats into the picker below and see the others, plus the contrast ratio against black and white. Everything runs in your browser.
Color PickerPick a color and read it in every common format, including OKLCH, with live WCAG contrast ratios against white and black.HEX: RGB written in base 16
A hex color is three bytes, one each for red, green and blue, written as two hex digits from 00 (none) to ff (255).
#3b82f6
3b = 59 red
82 = 130 green
f6 = 246 blue
There are three other lengths:
| Form | Example | Meaning |
|---|---|---|
#rgb | #38f | Each digit doubled: #3388ff |
#rrggbb | #3b82f6 | The standard form |
#rgba | #38f8 | Short form with alpha: #3388ff88 |
#rrggbbaa | #3b82f680 | Alpha as a byte: 80 = 128 / 255 ≈ 0.5 |
The alpha byte trips people up. #3b82f650 is not 50% opacity: 0x50 is 80, so it is about 31%. For 50% use 80, for 25% use 40, for 75% use bf.
RGB: the same numbers in decimal
rgb() takes the same three channels as plain numbers from 0 to 255, and an optional alpha from 0 to 1 (or a percentage).
color: rgb(59 130 246); /* modern space-separated syntax */
color: rgb(59 130 246 / 0.5); /* 50% opacity */
color: rgba(59, 130, 246, 0.5); /* legacy comma syntax, still valid */
HEX and RGB are a direct translation of each other, digit for digit. Converting between them loses nothing. Choose whichever your codebase already uses; HEX is shorter, RGB is easier to read when you need to change one channel or add alpha.
HSL: easier to reason about, but misleading
HSL takes the same color and describes it as a position on a color wheel:
- Hue, 0-360°: red at 0, yellow at 60, green at 120, cyan at 180, blue at 240, magenta at 300.
- Saturation, 0-100%: grey at 0, full color at 100.
- Lightness, 0-100%: black at 0, the pure color at 50, white at 100.
That makes some edits easy. To make a hover state slightly darker, lower the lightness. To make a muted version, lower the saturation. To get a matching color on the other side of the wheel, add 180 to the hue.
The problem is that HSL lightness is arithmetic, not perception. Compare two colors that HSL says are equally light:
hsl(60 100% 50%) → #ffff00 yellow contrast with white 1.07 : 1
hsl(240 100% 50%) → #0000ff blue contrast with white 8.59 : 1
Both have L = 50%, yet one is nearly invisible on a white page and the other passes every contrast test. So if you generate a palette by stepping HSL lightness, some hues end up much brighter than others at the same "step", and text that passes contrast in blue fails in yellow.
OKLCH: lightness that matches what you see
OKLCH is a newer CSS color function (supported in all current major browsers since 2023) built on the Oklab color space, which was designed so that equal numeric steps look like equal visual steps.
- L, 0-1 (or 0-100%): perceived lightness.
- C, chroma, roughly 0-0.37: how colorful. 0 is grey.
- H, 0-360°: hue. The angles do not line up with HSL's wheel; blue is around 260 rather than 240.
The same yellow and blue from before:
#ffff00 → oklch(0.968 0.211 109.8)
#0000ff → oklch(0.452 0.313 264.1)
OKLCH reports what your eyes reported: the yellow is very light and the blue is fairly dark. That is why design systems such as Tailwind CSS v4 now define their palettes in OKLCH. If you keep L fixed and change only H, you get a set of hues that look equally light, and a scale from L 0.95 down to L 0.25 looks evenly spaced no matter which hue it is.
:root {
--brand-100: oklch(0.93 0.04 260);
--brand-500: oklch(0.62 0.19 260);
--brand-900: oklch(0.30 0.10 260);
}
Contrast: why none of these numbers tell you directly
WCAG contrast is calculated from relative luminance, which weights green far more than blue because that is how human eyes respond. You cannot read it from a hex code or from HSL lightness. OKLCH's L comes close, but it is still not the WCAG formula. The thresholds you need to meet are:
- 4.5 : 1 for normal text (AA)
- 3 : 1 for large text (18.66 px bold or 24 px regular) and for UI components (AA)
- 7 : 1 for normal text at AAA
A small change can move you across the line. #767676 on white is 4.54 : 1 and passes AA; #777777, one step lighter on each channel, is 4.48 : 1 and fails. The blue #3b82f6 from the start of this post is only 3.68 : 1 against white, so it is fine for a button background or large heading but too light for body text. Always check the ratio with a tool rather than judging by eye.
Which format should you use?
| Situation | Use |
|---|---|
| Copying a color from a design file or brand guide | HEX, it is what those tools export |
| Adding transparency or changing one channel in code | rgb() with / alpha |
| Quick one-off tweaks (darker hover, muted variant) | HSL is fine |
| Building a palette, theme scale or dark mode | OKLCH |
| Checking that text is readable | The WCAG contrast ratio, whatever the format |
| Ask the printer for CMYK values; a converted CMYK from a screen color is only an estimate |
You can mix formats freely in CSS. The browser converts everything to the same color internally, so #3b82f6 and oklch(0.6231 0.188 259.81) render identically. Pick the one that makes the next edit easiest.
