Shade interpolation, loosely talking, is the method of figuring out the colours between two shade factors. It permits us to create distinctive colours, lovely palettes, higher gradients, and clean transitions.
I just lately wrote a Information to CSS Shade Capabilities however didn’t have the prospect to elucidate shade interpolation in any nice depth — which is a disgrace, because it permits us to create cool demos like this one:
Did you discover how oklch(80% 0.3 340)
interpolates to oklch(80% 0.3 60)
, then to oklch(80% 0.3 180)
, then to oklch(80% 0.3 270)
and again to oklch(80% 0.3 340)
utilizing CSS animation? Properly, I did! And that’s only a highly effective use of interpolation.
The place can we use shade interpolation?
Once more, shade interpolation is throughout CSS. These properties and features help shade interpolation both by direct mixing, gradients, or transitions:
In gradients and the color-mix()
operate, we also have a formal syntax for shade interpolation:
= in [ | ? ]
= |
= srgb | srgb-linear | display-p3 | a98-rgb | prophoto-rgb | rec2020 | lab | oklab | xyz | xyz-d50 | xyz-d65
= hsl | hwb | lch | oklch
= [ shorter | longer | increasing | decreasing ] hue
Sure, that’s a convoluted definition, but when we go forward and examine how this syntax works in color-mix()
, for instance, we might have one thing like this:
.ingredient{
shade: color-mix(in lch longer hue, crimson, blue);
}
The CSS color-mix()
operate supplies a means for us to combine totally different colours in any shade area, which is all what shade interpolation is about: going from shade to a different.
Our key focus is the in lab longer hue
half, which specifies how color-mix()
does the interpolation. That is mainly saying, “Hey CSS, interpolate the following colours within the CIELCH shade area utilizing an extended hue arc.” Sure, the in lab
half means the interpolation is finished in CIELCH, one of many many CSS shade areas, however we’ll get to what longer hue
precisely means later.
Simply keep in mind:
- The
in
key phrase all the time precedes the colour interpolation technique. - The second worth is the colour area used for mixing.
- The third worth is an non-compulsory hue interpolation technique ending with the
hue
key phrase.
This identical syntax seems in all gradient features, the place colours are interpolated step by step to get a clean gradient. Have a look at how tweaking the gradient with the colour interpolation syntax may give us a very new gradient:
.ingredient {
background: linear-gradient(in oklch longer hue 90deg, magenta, cyan);
}
Let’s backtrack a little bit, although. Interpolation can happen in two main shade areas: rectangular and polar.
Rectangular shade areas
Rectangular shade areas signify colours utilizing Cartesian coordinates on a three-dimensional airplane, which you may already know because the X (horizontal), Y (vertical), and Z (depth) axes on a graph.
Rectangular shade areas are like the identical type of graph, however is a map of shade factors as an alternative. For instance, the sRGB shade area has three axes, chargeable for the quantity of a shade’s redness, blueness, and greenness.

Polar shade areas
Polar shade areas additionally signify colours in a three-dimensional airplane, identical to rectangular shade areas, however it’s formed like a cylinder as an alternative of an oblong. A shade level is represented by three values:
- The top from the purpose to the middle, normally assigned to lightness or brightness.
- The radial distance from the middle, normally assigned to chroma or saturation.
- The angle across the middle, assigned to the hue.

What makes polar shade areas distinctive is the hue angle. Because it’s an angle, and they’re cyclic (like a steady circle), now we have extra choices for a way it may be interpolated.
Hue interpolation
Consider hue interpolation like discovering the space between the 2 instances on a clock.

Let’s assume the clock can go clockwise (forwards) or counterclockwise (backwards) in time.

The minute hand is at 10 minutes (2). If we wish to take the shortest distance between 50 minutes (10), then we might make a counterclockwise flip, like going again in time since that’s shorter than shifting ahead in a clockwise path.

That’s as a result of for those who take the longer route, you’ll need to move by 3, 4, 5, and many others. all the way in which to 10. Taking the shorter counterclockwise) route , you’d attain 10 in much less time (quarter-hour).
Hue interpolation works equally. It’s a CSS algorithm that determines the way you need hue colours in polar shade areas to be blended, and the path you wish to take between two hue factors.
There are 4 kinds of hue interpolation in CSS. Let’s go over these subsequent.
shorter
and longer
The shorter
(default worth) hue interpolation technique merely takes the shorter route, whereas the longer
hue interpolation technique takes the longer route when mixing colours between two hue factors.
Think about mixing two hue values crimson (0deg
) and blue (240deg
). There are two methods to do that:
- Go the longer route (distance of
240deg
). - Go the shorter route (distance of
120deg
).
If shorter
is used, the browser takes the shorter route (120deg
). In any other case, if longer
is used, the browser takes the longer route (240deg
).
This presents up a pleasant and distinctive mix of colours relying in your preferences. Hue interpolation is beneficial in creating clean shade transitions and gradients, giving loads of life to the web sites utilizing shade.
The shorter or longer hue interpolation, relying on the shortest or longest distances between two hue worth factors, can both go clockwise or counterclockwise. We are able to additionally set this mechanically with out really utilizing considered one of these key phrases, which we’ll take a look at subsequent.
rising
and lowering
Sticking with our clock analogy, the rising
hue interpolation technique is like shifting the minutes hand from 2 to 10, all the time in a clockwise path. Even when the ultimate worth is 1, it could nonetheless go in a clockwise path, doing virtually a full flip.
If, nevertheless, the hue interpolation technique is ready to lowering
, the minutes hand will all the time go in a counterclockwise path. Because the specification says, “[d]epending on the distinction between the 2 angles, this may both look the identical as shorter or as longer.”
If the angle goes from 20deg
to 50deg
utilizing the rising
hue interpolation worth, the worth will transfer clockwise from 20deg
to 50deg
, displaying the colours in between. Nevertheless, if the hue interpolation technique is ready to lowering
, then the algorithm takes the worth from 20deg
to 50deg
in a counterclockwise path.
Since rising
means the clock’s minute hand is consistently shifting ahead, this implies the worth can attain as much as 360deg
, a full circle. If the angle reaches 360deg
, it resets again to 0deg
till it reaches the following level. But when lowering
reaches 0deg
, then it resets to 360deg
, protecting the hue change constant.
How is this convenient?
Sure, all this principle is nice: we will use interpolation to get the middleman shade(s) between two colours and make new sorts of colours, however how can we really use it to create higher shade experiences in CSS?
Creating gradients
Shade interpolation occurs ceaselessly in all CSS gradient features. Take, for instance, the conic-gradient()
operate, which makes it simple to create a clean transition of colours that rotate round a middle level:
background: conic-gradient(
from 0deg,
oklch(70% 0.3 0deg),
oklch(70% 0.3 120deg),
oklch(70% 0.3 240deg),
oklch(70% 0.3 360deg)
);
Discover how the hue blends easily between every shade cease level? It’s lovely.
Shade mixing
Studying about color-mix()
within the CSS-Methods Almanac gives you a primary thought of how that is completed, however for those who’re like me and wish the uncooked code, right here it’s:
/* First Field */
background-color: color-mix(in oklch, rgb(255 0 0) 50%, lch(60% 40% 220deg) 50%);
/* Second Field */
background-color: color-mix(in oklch longer hue, rgb(255 0 0) 50%, lch(60% 40% 220deg) 50%);
An excellent benefit of color-mix()
is that you simply acquire the flexibility to combine colours in numerous shade areas inside one other shade area, thereby producing a novel shade. Once more, it’s shifting from one shade into one other and the path we take for mixing colours issues.
Animation
We are able to animate the transition between colours! So, as an alternative of blending two particular factors, we will watch the colour transition between all the colours in between the 2 factors!
@keyframes bg-shift {
from {
background-color: oklch(30% 0.3 20deg); /* darkish pink */
}
to {
background-color: oklch(70% 0.3 200deg); /* Cool bluish */
}
}