Build a Palette in oklch()

Bea’s catalogue site is open in your live preview, and its colors come from her printed seed packets: a leaf green, a terracotta, and a sunflower yellow. Right now they’re hex values picked by eye. That works until Bea asks for a paler green behind the seed cards or a darker one for small print, and you have to guess six more hex digits that still look like the same green.

In this lesson, you’ll rewrite Bea’s three brand colors in oklch(), a color function whose numbers describe how a color looks. You’ll build a green scale by changing one number at a time, and you’ll be able to predict what a change to each channel does to a color. Start with what those numbers mean.

Read a Color as Lightness, Chroma, and Hue

A hex value doesn’t tell you how light or how colorful a color is. Open the site’s styles.css and find the :root rule in the base layer. The file’s first line, @layer base, components;, declares its two cascade layers: a rule in a later layer beats one in an earlier layer, and within a layer the usual cascade order applies.

As in any token-based stylesheet, primitives hold the raw values, semantic roles such as --color-text point at primitives, and components read only the roles. These are the color tokens Bea’s site starts with:

Language: CSS
  :root {
    /* Primitives */
    --green-95: #eef5e9;
    --green-50: #3a732c;
    --green-20: #1f3a17;
    --terracotta-50: #c06240;
    --sun-50: #f0bb3b;

    /* Semantic tokens */
    --color-surface: var(--green-95);
    --color-text: var(--green-20);
    --color-accent: var(--terracotta-50);
    --color-border: var(--green-50);
    --color-focus: var(--sun-50);
    --color-on-photo: white;
  }

In the preview, the surface is the pale green page, the border is the green around each seed card, and the accent is the terracotta line under the header:

The catalogue with hex colors. A pale green page, dark green text, a terracotta line under the header, three seed cards with strong green borders and terracotta buttons.
The Catalogue With Its Hand-Picked Hex Colors

How light is each green? You can tell that #eef5e9 is pale and #1f3a17 is dark, but not by how much or whether the steps between them are even.

The oklch() function writes a color as three channels that you can read off directly:

  • Lightness runs from 0%, black, to 100%, white, and it’s measured the way people see lightness.
  • Chroma is how far the color is from gray. At 0, it’s a gray. Bea’s colors sit between 0.1 and 0.15, and few screen colors go past 0.3.
  • Hue is an angle in degrees around a color wheel: about 40 is a terracotta orange, 85 a sunflower yellow, 140 a leaf green, and 260 a blue.

Picture every color arranged around a gray pole, black at its foot and white at its top. Lightness is how high up a color sits, chroma how far out from the pole, and hue the direction you go out in. Bea’s leaf green is halfway up, 0.12 out, in direction 140. Here’s that model cut open at hue 140, with the color wheel laid out as a strip below it:

A grid of greens at hue 140, lightness in rows and chroma in columns, Bea's green outlined at 50% and 0.12, striped off-screen cells at the top and bottom right, and a hue strip below.
Bea's Green Among the Greens at Hue 140

Each row has one lightness value and each column one chroma value, starting at the gray pole on the left. Bea’s green is the outlined cell. Read down a column, and you pass through lighter and darker greens. The striped cells are colors a standard screen can’t show. How far out you can go depends on the direction and, as you’ll see when you build the scale, the height. The strip under the grid is the hue, from 0 round to 360.

Here are Bea’s three brand colors converted to oklch(). Replace the three primitives they came from:

Language: CSS
    --green-50: oklch(50% 0.12 140);
    --terracotta-60: oklch(60% 0.13 40);
    --sun-82: oklch(82% 0.15 85);

These converted values are rounded, but the difference is too small to see. Each name’s number is now the color’s lightness. Yellow is a light color by nature, so Bea’s sunflower sits much higher than her green or terracotta. At 50%, it would be a dark mustard. Point the roles at the two renamed tokens, or the accent and focus ring lose their colors:

Language: CSS
    --color-accent: var(--terracotta-60);
    --color-focus: var(--sun-82);

Save, and the catalogue looks exactly as before. No component changed, because components read the roles, and the roles still name the same colors.

Before you rely on a newer CSS feature such as oklch(), check its browser support. MDN’s oklch() page shows a Baseline badge at the top of the page, under the title. For oklch(), it says Baseline Widely available. All major browsers have supported oklch() for over two years, so Bea’s site needs no fallback. A feature marked Baseline Newly available works only in recent browsers and needs one.

Now change one channel to see what it does. Before you try it, predict what happens to the site’s terracotta accents if you set the chroma of --terracotta-60 to 0, keeping its lightness and hue as they are.

The accents turn a mid gray, oklch(60% 0 40). With no chroma, the color sits on the gray pole, so the hue no longer matters: oklch(60% 0 140) is the same gray. The gray is as light as the terracotta was, because the lightness didn’t change. Set the chroma back to 0.13 before you go on.

Try the other two channels too. Raise the lightness of --terracotta-60, and the accent gets paler but stays terracotta. Change its hue to 140, and it turns a green that’s about as light. Then put the values back.

Reading colors from numbers takes practice. The button below opens your mentor with this request, so it quizzes you with oklch() values, one at a time, and tells you how close each guess was:

Step Lightness to Build a Scale

Three greens aren’t enough for a whole site. Bea’s catalogue needs a near-white surface, tints for cards and banners, a strong border green, dark text shades, and, later in the course, a dark theme. In oklch(), you build most of the scale by keeping the hue and chroma and changing only the lightness.

Replace the three green primitives, --green-95, --green-50, and --green-20, with this scale:

Language: CSS
    --green-98: oklch(98% 0.03 140);
    --green-90: oklch(90% 0.12 140);
    --green-80: oklch(80% 0.12 140);
    --green-70: oklch(70% 0.12 140);
    --green-60: oklch(60% 0.12 140);
    --green-50: oklch(50% 0.12 140);
    --green-40: oklch(40% 0.12 140);
    --green-30: oklch(30% 0.09 140);
    --green-20: oklch(20% 0.06 140);
    --green-15: oklch(15% 0.04 140);

This scale is the 0.12 column of the picture above, with its ends moved in toward the pole. It keeps one hue and the same chroma through the middle, and it steps lightness by ten points, apart from the ends. To see it, make a scratch page, swatches.html, in the site’s folder. It’s a test strip for you, not part of the catalogue, and it lays the column on its side:

Language: HTML Filename: swatches.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Palette Test Strip</title>
  <style>
    .strip {
      display: flex;
      gap: 0.5rem;
    }

    .swatch {
      inline-size: 4rem;
      block-size: 4rem;
      background: var(--swatch);
    }
  </style>
</head>
<body>
  <h1>Palette Test Strip</h1>
  <h2>Green Scale</h2>
  <div class="strip">
    <div class="swatch" style="--swatch: oklch(98% 0.03 140)"></div>
    <div class="swatch" style="--swatch: oklch(90% 0.12 140)"></div>
    <div class="swatch" style="--swatch: oklch(80% 0.12 140)"></div>
    <div class="swatch" style="--swatch: oklch(70% 0.12 140)"></div>
    <div class="swatch" style="--swatch: oklch(60% 0.12 140)"></div>
    <div class="swatch" style="--swatch: oklch(50% 0.12 140)"></div>
    <div class="swatch" style="--swatch: oklch(40% 0.12 140)"></div>
    <div class="swatch" style="--swatch: oklch(30% 0.09 140)"></div>
    <div class="swatch" style="--swatch: oklch(20% 0.06 140)"></div>
    <div class="swatch" style="--swatch: oklch(15% 0.04 140)"></div>
  </div>
</body>
</html>

Open swatches.html in your live preview. Through the middle, each patch is the same green, one even step darker than the one before. The ends are a little grayer, for a reason you’ll see in a moment:

Ten green patches in a row, from near white on the left to near black on the right, in even steps through the middle and a little grayer at the ends.
The Green Scale, One Lightness Step at a Time

Because lightness in oklch() follows what people see, steps of equal size look evenly spaced. Watch out if you’ve picked colors by their hsl() numbers. Its lightness is plain arithmetic on the red, green, and blue values, so it doesn’t track what people see. At the same hsl() lightness, 50%, a yellow is almost as light as white and a blue is very dark:

Two patches that both have hsl() lightness 50%. The yellow one is nearly as light as the white page; the blue one is very dark.
Same hsl() Lightness, Very Different Lightness on Screen

Equal lightness doesn’t tell you whether text on a color is readable. That’s contrast, and you’ll measure it later in the course. That’s all you need to know about contrast for now.

So why do the ends of the scale have less chroma? The range of colors a screen can show is its gamut, and near white or black it narrows. A color that light or dark can’t be very colorful.

The browser maps a color outside the gamut back inside. In Chrome on a standard screen, oklch(98% 0.12 140) comes out a stronger green at about 95% lightness, breaking the even steps. So the scale’s ends take the most chroma that fits, rounded down to two decimals.

You’ll also meet colors written as color(display-p3 ...), for the wider gamut of many newer screens, but this course keeps to colors every screen can show.

Gamut is an idea you can nod along to without quite getting it. To test yourself, press the button below and explain it in your own words. Your mentor then asks questions about your explanation:

Finally, point the green roles at the new scale. In the semantic tokens, change --color-surface and keep the other two lines as shown:

Language: CSS
    --color-surface: var(--green-98);
    --color-text: var(--green-20);
    --color-border: var(--green-50);

Save, and compare the catalogue with how it started:

The catalogue after the change. The page behind the cards is a paler green, the text a darker green, and the card borders, buttons and terracotta line look as before.
The Same Catalogue, Now Built on the oklch() Scale

The page is a touch paler and the text a touch darker, and the borders and accent are unchanged. No rule outside :root changed, because the roles carried the new colors to every component.

Check Your Understanding

Try these before you open the answers:

  1. For small print, Diego wants a darker terracotta that still looks like Bea’s terracotta. Which channel of --terracotta-60 should he change, and which channels should he leave alone?
  2. Why does the green scale keep the hue and the chroma and step only the lightness, and why do its ends have less chroma?
  1. Lower the lightness and keep the chroma and the hue, as in oklch(45% 0.13 40). The hue keeps it terracotta, and the chroma keeps it as colorful.
  2. Fixed hue and chroma keep every step the same green, and since oklch() lightness follows what people see, equal steps look even. Near white and black, a screen can’t show much chroma, so the ends use the most that fits. Otherwise, the browser maps them to a different color.

Bea’s site now has its brand colors in oklch() and a green scale whose names say how light each step is. But the terracotta and yellow have one shade each, and the “Returns week” banner will want a see-through green. Picking each by hand is slow. In the next lesson, Mix Tints With color-mix(), you’ll let the browser mix tints and translucent versions from the colors you already have.

Become a Member to join the conversation.