Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

HSL lies about lightness. Build color palettes in OKLCH instead

When I rebuilt the color palette generator on my tools site, I had to decide which color space to do the maths in. HSL is the obvious choice. It's also wrong in a way that's easy to see. Here are two colors with exactly

When I rebuilt the color palette generator on my tools site, I had to decide which color space to do the maths in. HSL is the obvious choice. It's also wrong in a way that's easy to see.

Here are two colors with exactly the same HSL lightness:

--yellow: hsl(60 100% 50%);   /* #ffff00 */
--blue:   hsl(240 100% 50%);  /* #0000ff */

Put white text on both. On the blue it's perfectly readable. On the yellow you can barely see it. So whatever "50% lightness" means, it isn't what your eyes see.

How far off it is

HSL lightness is just arithmetic on the red, green and blue values. It knows nothing about how bright each color looks to a person. OKLCH does: its L is designed to match perceived lightness.

I ran six colors through both. Every one of them has HSL lightness 50% and full saturation:

Color Hex HSL lightness OKLCH lightness Contrast with white
Yellow #ffff00 50% 0.97 1.07 : 1
Cyan #00ffff 50% 0.91 1.25 : 1
Green #00ff00 50% 0.87 1.37 : 1
Magenta #ff00ff 50% 0.70 3.14 : 1
Red #ff0000 50% 0.63 4.00 : 1
Blue #0000ff 50% 0.45 8.59 : 1

Same number in HSL, but OKLCH puts them anywhere from 0.45 to 0.97. And the contrast ratios side with OKLCH: yellow is almost white, blue is darker than mid-grey.

Why it matters when you build palettes

Shade scales come out uneven. Most generators make a 50–950 scale by stepping HSL lightness. Because that lightness isn't what we see, yellows wash out too early, blues turn muddy, and "500" is a different brightness for every hue. Step OKLCH L instead and the steps look even for any color.

Harmonies feel unbalanced. A complementary palette rotates the hue 180°. In HSL, a calm blue can flip to a glaring yellow at a completely different brightness. In OKLCH you rotate H and keep L, so the new color sits at the same visual weight.

Contrast gets predictable. Colors that share an OKLCH lightness behave alike under the same text color, which makes accessible palettes much easier.

Reading an OKLCH color

color: oklch(0.6 0.2 264);
/*           L   C   H  */
  • L is lightness, from 0 (black) to 1 (white), and 0.6 looks equally light for every hue.
  • C is chroma, how colorful it is. 0 is grey, and most usable colors stay under about 0.32.
  • H is the hue angle, 0 to 360, like HSL, but the positions are a bit different. Red sits around 29°, blue around 264°.

Bugs I hit along the way

A couple of things that bit me, in case you build something like this:

Not every OKLCH color fits on a screen. High chroma at very high or very low lightness just doesn't exist in sRGB. If you let the browser clip it, the hue can shift. It's better to lower the chroma step by step until the color fits.

A monochromatic palette can collapse into duplicates. My first version made monochrome palettes by nudging lightness up and down from the base. For white or bright yellow, "up" has nowhere to go, so I got the same color three times. Picking from fixed lightness slots across the range solved it.

One wrong exponent ruins everything. Converting back from linear light to sRGB is 1.055 × x^(1/2.4) − 0.055. I had a small slip in it, and every color came out slightly off in a way that was hard to spot by eye. Test your conversion against known values before trusting it.

An OKLCH scale in Tailwind v4

Tailwind v4 already defines its default colors in OKLCH. You can add your own scale the same way:

@import "tailwindcss";

@theme {
  --color-brand-50:  #f2f4ff;
  --color-brand-100: #e4e9ff;
  --color-brand-200: #cfd6ff;
  --color-brand-300: #b0bbff;
  --color-brand-400: #8994ff;
  --color-brand-500: #7178ff;
  --color-brand-600: #6366f1;
  --color-brand-700: #4f50cc;
  --color-brand-800: #4242ab;
  --color-brand-900: #37398f;
  --color-brand-950: #1d1d57;
}

I generated this from #6366f1 by stepping OKLCH lightness. The base color ended up at 600, the step closest to its own lightness. After that, bg-brand-500, text-brand-900 and border-brand-200 work like any built-in color.

Writing the values as hex is fine. Every browser and design tool understands hex. OKLCH matters for how you choose the colors, not how you write them down.

Try it

The color palette generator I ended up with does all of the above: six harmonies, a 50–950 scale with gamut clamping, white and black contrast on every swatch, and export to CSS variables, Tailwind v4 @theme, SCSS or JSON. For converting single colors between HEX, RGB, HSL and OKLCH there's also a color converter.

Have you moved your design tokens to OKLCH yet, or is HSL still doing the job for you?

I wrote this with help from an AI assistant for drafting and editing. The color numbers and the bugs come from my own project, AppyGrid.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.