Sub topic 5
Picking the interpolation color space
This chapter assumes you can write color-mix() with percentages and that you know the oklch() components (lightness, chroma, hue). It also assumes you know what CSS custom properties are. Here you will learn how the in <space> argument changes the result of a mix, and how to choose it.
What the space controls
Mixing two colors means interpolating, which is calculating a point between them. The interpolation happens channel by channel, in whatever color space you name. The same two inputs give different outputs in different spaces, because "halfway" depends on how the channels are laid out.
color-mix() has no default space. You must write in <space>, or the declaration is invalid.
.a { background: color-mix(in srgb, #1e66f5, #f5a623); }
.b { background: color-mix(in oklab, #1e66f5, #f5a623); }
.c { background: color-mix(in oklch, #1e66f5, #f5a623); }Each rule mixes the same blue and orange 50/50. Each produces a different middle color.
Rectangular and polar spaces
Color spaces fall into two groups. This matters because only one group has a hue angle.
| Group | Examples | Channels | Hue handling |
|---|---|---|---|
| Rectangular | `srgb`, `srgb-linear`, `lab`, `oklab`, `xyz` | Coordinates on straight axes | None. Channels are blended in a straight line. |
| Polar | `hsl`, `hwb`, `lch`, `oklch` | Lightness, chroma or saturation, and a hue angle | Hue is an angle, so you can choose the direction. |
Rectangular mixes travel in a straight line between the two colors. When the colors are far apart in hue, the midpoint often loses chroma. The result looks grayer than either input. Polar mixes travel around the hue wheel, so chroma stays closer to its original level. They also introduce intermediate hues you did not name, such as the green that appears between blue and yellow.
Choosing a space
Think about what you want the mix to preserve.
flowchart TDReplace the above; see the diagram below.
For most design-system work, oklch is the right default. Its lightness tracks perceived brightness, so a 15% tint looks like an even step from a 30% tint.
.card {
background: color-mix(in oklch, var(--brand) 15%, white);
}This takes 15% of --brand and the remaining 85% white. White has no hue, so the browser keeps the hue of --brand and the tint does not drift toward another color.
Seeing the difference
Mixing complementary colors shows why the space matters.
.swatch-a {
background: color-mix(in oklab, oklch(65% 0.2 30), oklch(65% 0.2 210));
}
.swatch-b {
background: color-mix(in oklch, oklch(65% 0.2 30), oklch(65% 0.2 210));
}The two inputs are a red-orange and a cyan-blue with equal lightness and chroma. In oklab, the colors blend along a straight line through the center of the color space. The midpoint has very low chroma and looks gray. In oklch, the hue angle moves from 30 to 210, so the midpoint has a hue near 120 and keeps close to full chroma. It is a saturated green.
Neither result is wrong. Choose oklab when you want a muted midpoint. Choose oklch when you want a vivid transition.
Hue direction in polar spaces
A polar mix needs to know which way to go around the hue circle. The default is shorter, the shorter arc. You can override it:
.swatch-a {
background: color-mix(in oklch, red, blue);
}
.swatch-b {
background: color-mix(in oklch longer hue, red, blue);
}The first mix takes the short arc between red and blue. The second takes the long way around, through yellow and green. The increasing and decreasing keywords force a direction instead of picking by distance. A later chapter covers these in detail. For now, remember that the hue keyword only works in polar spaces. Writing in oklab longer hue is invalid.
Common errors
- Omitting the space.
color-mix(red, blue)is invalid. Writein srgbat minimum. - Expecting
srgbto match perception. It mixes encoded sRGB values, so midpoints between saturated colors often look dark and muddy. - Adding a hue method to a rectangular space. The declaration is dropped.
- Assuming percentages change with the space. Percentages set the weights. The space changes only where those weights land.
Exercise
You mix a brand blue with its orange complement 50/50 and want a vivid green-ish transition rather than a gray midpoint. Which space do you pick, and why?
Show answer
Pick oklch (a polar space). It interpolates the hue angle, so the midpoint passes through intermediate hues and keeps its chroma. oklab blends in a straight line through the center and produces a low-chroma gray.
Exercise
Why does color-mix(in oklab longer hue, red, blue) fail?
Show answer
oklab is a rectangular space with no hue channel. Hue interpolation keywords are valid only in polar spaces such as oklch, lch, hsl and hwb.