Skip to content
The Moonbase
DE

Color models and color spaces

Why are there so many ways to write colors in CSS? And what sets them apart?

Contents8 sections

You’re building a row of colored buttons. To keep things consistent, they all get the same lightness: hsl(60 100% 50%) for yellow, hsl(240 100% 50%) for blue, plus a red and a green. The result: the yellow button glows like a safety vest, the blue one looks almost dark. And white text is barely readable on one, but perfectly fine on the other.

But you set 50% lightness everywhere. What’s going on?

The short answer: not every way of turning colors into numbers matches how we see them. And CSS has quite a few of those by now. You can write the same blue as #3366cc, rgb(51 102 204), hsl(220 60% 50%) or oklch(53% 0.17 262). Each notation is based on a different idea of how to describe colors. This article is about what those ideas are and when to use which.

Colors are really just numbers#

Your screen is made of millions of tiny pixels. Simply put, each one consists of three points of light: a red, a green and a blue one. Depending on how bright each of them shines, your eye mixes them into a color.

How pixels are physically built varies from screen to screen. Some have extra white subpixels, others share subpixels with their neighbors.1 For the browser, that doesn’t matter: from its point of view, every pixel is three numbers for red, green and blue. How the screen turns them into light is up to the screen.

That leaves the question of how we write these numbers down. That’s what color models like RGB, HSL or OKLCH are for. They define which values a color consists of and what those values mean.

Color spaces like sRGB or Display P3 are something else. They define which colors can be shown at all. Sounds similar, but it’s an important difference. More on that later.

RGB and hex#

RGB is the color model closest to the hardware: three values between 0 and 255, one each for red, green and blue.

rgb(255 0 0) is bright red, rgb(0 0 0) black and rgb(255 255 255) white.

Hex colors work the same way, just in hexadecimal notation. In #3366cc, 33 stands for 51 red, 66 for 102 green and cc for 204 blue.

That’s ideal for computers, not so much for us humans. Try making #3366cc a bit lighter or warmer in your head. You have to adjust all three values, and at first glance it isn’t clear in which direction or by how much.

HSL#

HSL replaces red, green and blue with three values that are closer to how we think about colors:

  • Hue: an angle on the color wheel. 0° is red, 120° green and 240° blue.
  • Saturation: how vivid the color is, from gray to fully colorful.
  • Lightness: how light or dark the color is. 0% is black, 100% white, and at 50% the color is at its most vivid.

Want a color to be lighter? Just turn up the lightness. No more juggling three RGB values.

Try it out, and feel free to switch between the models:

One color, three notations

  • #33598a
  • rgb(51 89 138)
  • hsl(214 46% 37%)
  • oklch(46% 0.090 255)
The same color in RGB, HSL and OKLCH. When you move a slider, the other notations update along with it.

As handy as HSL is, there’s a catch: at its core, it’s just RGB in different packaging. The conversion is pure geometry and doesn’t take into account how we actually perceive colors.2

Our eyes aren’t equally sensitive to all colors. For example, we perceive yellow and green as much brighter than blue.3 So 50% lightness in HSL doesn’t mean two colors actually look equally bright. That’s exactly what happened with the buttons at the start.

OKLCH#

OKLCH is based on Oklab, a color model Björn Ottosson introduced in 2020.4 Its values are chosen so that colors with the same lightness also look roughly equally bright to our eyes.

The three values:

  • L (lightness): the perceived lightness, from 0% (black) to 100% (white).
  • C (chroma): how colorful it is. At 0, the color is completely gray.
  • H (hue): an angle on the color wheel, like in HSL. The hues sit in slightly different places though, red for example is closer to 30° than 0°.

A direct comparison shows how big the difference to HSL is. Below are six hues that all have the same lightness in HSL. Switch to “Lightness only”: the hue disappears, and what’s left is how bright the colors look to your eye.

Equally bright?

  • 73
  • 89
  • 88
  • 61
  • 53
  • 65

hsl(H 100% 50%)

Number = perceived lightness (OKLCH L)

Six hues with the same lightness, once in HSL and once in OKLCH. The switch shows only the perceived lightness.

You can see the difference with the naked eye: in HSL, the grays range from almost white to quite dark. The yellow-green gets close to 90, the purple only just over 50, even though HSL says both should be equally bright. The OKLCH colors, on the other hand, turn into fairly even grays.

OKLCH isn’t perfect either. L is an approximation, and how bright a color looks also depends on its surroundings and the screen. But the deviations are much smaller than with HSL.

This is especially useful when you put together a color palette. A blue and a red with the same L value look about equally bright on the same background. So you no longer have to tweak each color individually until everything sort of fits together.5

Remember the question of how to make #3366cc lighter? With OKLCH and the relative color syntax, you change only the one value you care about and leave the others as they are:6

css
--brand: #3366cc;
--brand-light: oklch(from var(--brand) calc(l + 0.1) c h);

There is one catch though: not every combination of L, C and H can actually be displayed. How colorful a color can get depends on its lightness and hue. A very dark yellow with high chroma, for example, simply doesn’t exist. More on that in the section on color spaces.

OKLCH has worked in all major browsers since 2023.7 So you can use it in your projects just like anything else.

Gradients#

Color models don’t only matter when defining colors, but also when mixing them.

Take a regular gradient. The browser has to calculate an in-between color at every point between start and end. If you use hex or rgb() colors, this happens with the sRGB values by default: red, green and blue are each averaged separately.8

Mathematically, that’s perfectly correct, but it doesn’t always look good. If you mix blue and yellow, for example, the channels partly cancel each other out and you end up with a dull gray in the middle.

Mixing two colors

  • in srgb
  • in hsl
  • in oklch
The same gradient, mixed in three color models.

In HSL, the gradient stays colorful because it takes the route around the color wheel. But along the way, garish in-between hues often show up and the perceived lightness visibly jumps back and forth.

OKLCH also goes around the color wheel, but keeps the lightness even. The gradient stays vivid and still looks calm.

You barely have to change anything for this. Just add in oklch:

css
background: linear-gradient(to right in oklch, #2453ff, #ffe800);

And the browser calculates the in-between colors in OKLCH.

Keep two things in mind. First, an OKLCH gradient can pass through colors outside of sRGB along the way. The browser then adjusts them, as described below. Second, the route around the color wheel sometimes brings in hues you didn’t want at all. In that case, in oklab helps: the gradient goes straight from one color to the other, without a detour. With complementary colors like blue and yellow, though, the middle gets paler this way too, if not as dull as in sRGB.

sRGB and Display P3#

So far, this was about how we describe and mix colors. Now for a different question: which colors can your screen show at all?

That’s exactly what color spaces define: what “100% red” means in concrete terms and how vivid the primary colors can get.

The standard on the web is sRGB, a color space from 1996.9 Everything you write with hex, rgb() or hsl() is automatically in sRGB.

Many modern screens can do more, though. Macs, iPhones and many current monitors support Display P3, for example. This color space goes back to a standard from cinema technology and covers more colors than sRGB, especially vivid greens, reds and oranges.10

To use these extra colors in CSS, you need a notation like oklch() or color(display-p3 …). With rgb() or hsl(), you can’t go beyond sRGB.11

Wait, wasn’t OKLCH a color model? How can a model go beyond a color space? The trick: in CSS, rgb() and hsl() are tied to sRGB. rgb(255 0 0) always means “the red of sRGB”, and it doesn’t get any more vivid than that. OKLCH, on the other hand, isn’t tied to any screen, but describes colors the way we see them. That way you can write down any visible color, including ones sRGB doesn’t know. Strictly speaking, that makes Oklab a color space itself, just one without the tight limits of sRGB.

How colorful can it get?

sRGB 0.193
Display P3 0.269
  • sRGB
  • Display P3
The most colorful colors possible in sRGB and P3 at this lightness. On an sRGB screen, both swatches look the same.

The lines show, for each hue, how colorful a color can get at the selected lightness. Everything between the two lines can be shown by Display P3, but not by sRGB. How much P3 gains depends heavily on the hue. For green, the difference is huge, for blue much smaller. That’s because both color spaces use practically the same blue as their primary.

You don’t need to worry much about older screens, by the way. If a device can’t display a color, the browser maps it to one it can display.12 The specification describes a gentle method for this that only reduces chroma. How well browsers already implement it varies, though. In the simplest case, the RGB values that stick out are just clipped. For colors just outside of sRGB, that’s barely noticeable. But if a color is far outside, its hue and lightness can shift as well.

If you want to play it safe, set an sRGB color as the default and the P3 variant only for screens that can actually display it:

css
.button { background: oklch(65% 0.19 145); }

@media (color-gamut: p3) {
  .button { background: oklch(65% 0.25 145); }
}

When to use what?#

After RGB, HSL, OKLCH and two color spaces, one question remains: which of these do you use day to day?

  • Hex and RGB when you take a color from Figma or another design tool and need exactly that value.
  • HSL really only in existing code. For new colors, there’s hardly any reason to prefer it over OKLCH.
  • OKLCH for your own color palettes, design tokens and gradients. Lightness is much easier to control, you derive variants with the relative color syntax, and you can reach colors outside of sRGB too.

My tip: try oklch() in your next project, ideally right on your buttons. The values feel unfamiliar at first, but after adjusting a few colors you probably won’t want to give up the benefits.

Footnotes

  1. Wikipedia: PenTile matrix family. ↩

  2. W3C: CSS Color Module Level 4, section “HSL Colors”. The specification defines HSL as a plain conversion from sRGB. ↩

  3. Wikipedia: Luminous efficiency function. The light-adapted eye is most sensitive at around 555 nm (yellow-green). ↩

  4. Björn Ottosson: A perceptual color space for image processing, December 23, 2020. ↩

  5. Andrey Sitnik, Travis Turner: OKLCH in CSS: why we moved from RGB and HSL, Evil Martians. ↩

  6. MDN: Using relative colors. In the relative syntax, l is a number between 0 and 1, hence + 0.1 instead of + 10%. ↩

  7. web.dev: Baseline 2023. ↩

  8. W3C: CSS Color Module Level 4, section “Color Space for Interpolation”. Without one specified, hex, rgb() and hsl() are mixed in sRGB, everything else in Oklab. ↩

  9. Michael Stokes et al.: A Standard Default Color Space for the Internet – sRGB, 1996. ↩

  10. Wikipedia: DCI-P3. The blue primary is in the same place in sRGB and Display P3. ↩

  11. Nikita Vasilyev: Wide Gamut Color in CSS with Display-P3, WebKit Blog. ↩

  12. W3C: CSS Color Module Level 4, section “CSS Gamut Mapping to an RGB Destination”. ↩