Sub topic 2
Perceptual uniformity: what Oklab and OKLCH fix
This chapter assumes you can read hex, rgb() and hsl() colors and know how CSS custom properties work. The previous chapter showed that those notations describe colors in terms of the screen's red, green and blue lights. Here you will see what goes wrong when you treat those numbers as if they measured how a color looks, and how Oklab and OKLCH address it.
What "perceptually uniform" means
A color space is perceptually uniform when equal numeric changes produce roughly equal visible changes. It also needs the stronger property that a number like "lightness" tracks how light the color looks, whatever its hue.
Three questions test this:
- If two colors have the same lightness value, do they look equally light?
- If you add 0.1 to lightness, does the step look the same size at every hue?
- If you change only lightness, does the hue still look the same?
hsl() fails all three. That is the reason a palette built by changing hsl() numbers often needs hand-tuning.
The problem with HSL lightness
HSL is a rearrangement of sRGB. It takes the red, green and blue channel values and presents them as hue, saturation and lightness. The rearrangement is geometric. It does not model the eye.
The eye is not equally sensitive to the three primaries. Green light looks much brighter than the same amount of blue light. HSL ignores this. Every fully saturated color at L = 50% has the same HSL lightness, but the colors look very different:
| Color | HSL | Looks like | Contrast against white (WCAG) |
|---|---|---|---|
| Yellow | `hsl(60 100% 50%)` | Very bright | about 1.07:1 |
| Green | `hsl(120 100% 50%)` | Bright | about 1.4:1 |
| Red | `hsl(0 100% 50%)` | Medium | about 4:1 |
| Blue | `hsl(240 100% 50%)` | Dark | about 8.6:1 |
All four rows have "50% lightness". White text is unreadable on the yellow and comfortable on the blue. If you write a design rule such as "buttons use hsl(H 100% 50%)", you get buttons that behave very differently depending on H.
The same flaw hurts you when you adjust a color. Subtracting 10 points of HSL lightness from a yellow produces a small visible change. The same subtraction from a blue produces a large one. Hover states built this way look inconsistent across a palette.
Where Oklab comes from
Oklab is a color space published by Björn Ottosson in 2020. It was designed for image processing, with one goal: predict perceived lightness, chroma and hue well, using a model simple enough to compute cheaply. CSS Color Level 4 added it, along with its polar form OKLCH.
Oklab computes a color in three steps:
- Convert from the screen's gamma-encoded sRGB values to linear light, meaning values proportional to physical light intensity.
- Convert to an approximation of how the three cone types in the eye respond, called LMS (long, medium, short wavelength).
- Apply a cube root to compress the response, as the eye does, and rotate into three axes: lightness and two opponent color axes.
The two color axes are called a (roughly green to red) and b (roughly blue to yellow). Oklab values are written as L, a, b.
You rarely call Oklab directly in CSS, because a and b are awkward to reason about. They are Cartesian coordinates, a point on a plane. People think in terms of "how vivid" and "which color family", which are polar coordinates.
OKLCH: Oklab in polar form
OKLCH takes the same lightness and converts the a/b plane into an angle and a distance from the center:
- L is lightness, from 0 (black) to 1 (white). CSS also accepts a percentage, so
0.62and62%are the same. - C is chroma, the distance from gray.
0is a pure gray. Real colors on a display reach about0.3to0.37. A percentage of 100% equals0.4. - h is hue, an angle in degrees from 0 to 360 around the color wheel.
A useful picture is a cylinder. The vertical axis is lightness. The distance from the axis is chroma. The angle around the axis is hue. Moving along one of these dimensions leaves the other two unchanged, at least in the model's own terms.
The next chapter covers the oklch() function's syntax in full. For now, one rule is enough: the arguments appear in the order lightness, chroma, hue, for example oklch(0.62 0.19 255).
Same lightness, same look
Here are the same four colors from the table, written as OKLCH values:
| sRGB color | OKLCH (approximate) |
|---|---|
| Yellow `#ffff00` | `oklch(0.97 0.21 110)` |
| Green `#00ff00` | `oklch(0.87 0.29 143)` |
| Red `#ff0000` | `oklch(0.63 0.26 29)` |
| Blue `#0000ff` | `oklch(0.45 0.31 264)` |
Now the lightness numbers tell the truth. Yellow is near 1 and blue is under 0.5, which matches what you see. HSL called all four "50%".
To get colors that look equally light, hold L constant and change only the hue. The page below renders two rows of six swatches. The top row holds HSL lightness constant. The bottom row holds OKLCH lightness constant.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HSL vs OKLCH lightness</title>
<style>
.row { display: flex; margin-bottom: 1rem; }
.row span { width: 5rem; height: 5rem; }
.hsl span { background: hsl(var(--h) 80% 55%); }
.oklch span { background: oklch(0.72 0.1 var(--h)); }
</style>
</head>
<body>
<div class="row hsl">
<span style="--h: 30"></span>
<span style="--h: 90"></span>
<span style="--h: 150"></span>
<span style="--h: 210"></span>
<span style="--h: 270"></span>
<span style="--h: 330"></span>
</div>
<div class="row oklch">
<span style="--h: 30"></span>
<span style="--h: 90"></span>
<span style="--h: 150"></span>
<span style="--h: 210"></span>
<span style="--h: 270"></span>
<span style="--h: 330"></span>
</div>
</body>
</html>In the top row the yellow-green swatches look bright and the blue-purple swatch looks heavy. In the bottom row, the six swatches look like they belong to the same set. Try converting both rows to grayscale with your browser's rendering tools, or with filter: grayscale(1) on .row. The OKLCH row turns into a nearly flat gray strip. The HSL row turns into a visible ramp.
The "nearly" matters. Oklab is a model, and it is much better than HSL but not perfect. Very saturated blues and purples still show small hue and lightness drift, and viewing conditions affect perception in ways no screen color space accounts for.
Predictable adjustments
Uniform lightness changes how you build interactive states. In HSL you would lower lightness for a hover state and then check each hue by eye. In OKLCH you lower L by a fixed amount and expect a similar-looking step at every hue:
:root {
--blue: oklch(0.62 0.17 255);
--green: oklch(0.62 0.17 150);
--red: oklch(0.62 0.17 25);
}
.btn-blue:hover { background: oklch(0.54 0.17 255); }
.btn-green:hover { background: oklch(0.54 0.17 150); }
.btn-red:hover { background: oklch(0.54 0.17 25); }All three buttons start at the same lightness and drop by the same 0.08. The hover steps look consistent, and so does the contrast between each button and its label. A later chapter turns this pattern into a token system.
Grays are the simplest case. A gray has no chroma, so only L matters:
:root {
--gray-100: oklch(0.96 0 0);
--gray-300: oklch(0.86 0 0);
--gray-500: oklch(0.66 0 0);
--gray-700: oklch(0.44 0 0);
--gray-900: oklch(0.24 0 0);
}Evenly spaced L values give a ramp where each step looks about as far from its neighbor as any other. Hue is meaningless when chroma is 0, so the 0 in the third position is a placeholder.
Hue stability
Lightness is not the only improvement. Older perceptual spaces, notably CIELAB, bend hues as you move lightness or chroma. The standard example is blue: in CIELAB, lightening a saturated blue makes it drift toward purple. Oklab was fitted against better data, and in it a ramp from a blue to white stays blue.
In practice, this means you can derive a tint (higher L, lower C) or a shade (lower L) from a base color without the hue wandering. You still need to look at the result, but you correct fewer surprises.
What OKLCH does not fix
Be clear about the limits before building on it.
- Contrast is not guaranteed. Equal lightness gives similar perceived brightness. It does not give a specific WCAG contrast ratio, because WCAG 2.x uses its own luminance formula. Two colors with an L difference of 0.4 can pass or fail depending on hue. A later chapter covers checking.
- Chroma has no fixed ceiling. The maximum chroma a display can show depends on L and h.
oklch(0.9 0.3 250)is outside the sRGB gamut, and the browser has to map it to something displayable. The gamut chapter covers what happens. - Not every hue behaves alike. Some hues look stronger than others at the same chroma. Equal C does not mean equal vividness for every hue, only closer than in HSL.
- It is a model of an average viewer. Ambient light, display calibration and color vision differences all change what people see.
Choosing between HSL and OKLCH
OKLCH is the better default for deriving related colors: scales, hover states, tints, themes. HSL remains acceptable when you pick one-off colors by eye and never adjust them systematically, or when you must support a browser without oklch(). Fallbacks for that case are covered later in the tutorial.
| Task | HSL | OKLCH |
|---|---|---|
| Same-lightness colors at different hues | Lightness values disagree with appearance | L values match appearance closely |
| Darken by a fixed step | Visible step size varies by hue | Step size is similar across hues |
| Lighten a blue | Hue shifts less in HSL, but lightness is inconsistent | Hue stays stable |
| Gray ramps | Works | Works, with even steps |
| Maximum saturation | `100%` always in gamut | Maximum chroma depends on L and h |
The last row shows the real cost. HSL's saturation always has a valid range. OKLCH's chroma does not, so you sometimes request colors the display cannot show.
Exercise
A teammate writes hsl(60 100% 50%) for a yellow warning badge and hsl(240 100% 50%) for a blue info badge, saying both have "the same lightness", and puts white text on both. Explain what is wrong, and describe how you would pick two badge colors that really are matched.
Show answer
HSL lightness is a geometric rearrangement of RGB values and does not model how bright a color looks. The yellow is perceived as very light (OKLCH L about 0.97) and the blue as dark (L about 0.45). White text on the yellow has a contrast of roughly 1.07:1, far below the 4.5:1 that WCAG requires for normal text, while on the blue it is about 8.6:1. To match the badges, choose the same OKLCH lightness for both, for example oklch(0.55 0.15 95) and oklch(0.55 0.15 255), and vary only the hue. Then measure the actual contrast of the white text on each, because equal lightness still does not guarantee a specific WCAG ratio.
Exercise
You have --brand: oklch(0.62 0.19 255). Write a hover color that is slightly darker with the same hue and chroma, and say why this is more predictable than doing the same in HSL.
Show answer
Use oklch(0.54 0.19 255), which lowers L by 0.08 and leaves C and h alone. It is more predictable because OKLCH lightness tracks perceived brightness, so the same drop in L produces a similar-looking darkening at any hue. In HSL, subtracting a fixed number of points from lightness produces visibly different steps for different hues, so each color in a palette may need its own adjustment.