I’ve had the chance to edit over a number of the brand new coloration entries coming to the CSS-Methods Almanac. We’ve already revealed a number of with extra on the way in which, together with a full information on coloration capabilities:
And I have to admit: I didn’t know quite a bit about coloration in CSS (I nonetheless used rgb()
, which apparently isn’t what cool folks do anymore), so it has been a enjoyable studying expertise. One of many issues I seen whereas attempting to maintain up with all this new info was how lengthy the glossary of coloration goes, particularly the “coloration” ideas. There are “coloration areas,” “coloration fashions,” “coloration gamuts,” and mainly a “coloration” one thing for every thing.
They’re all considerably associated, and it may well get complicated as you dig into utilizing coloration in CSS, particularly the brand new coloration capabilities which have been shipped currently, like contrast-color()
and color-mix()
. Therefore, I wished to make the glossary I want I had after I was listening to for the primary time about every idea, and that anybody can verify each time they neglect what a selected “coloration” factor is.
As a disclaimer, I’m not attempting to elucidate coloration, or particularly, coloration replica, on this publish; that may most likely be unimaginable for a mortal like me. As a substitute, I need to offer you a sufficiently big image for some technicalities behind coloration in CSS, such that you simply really feel assured utilizing capabilities like lab()
or oklch()
whereas additionally understanding what makes them particular.
What’s a coloration?
Let’s decelerate first. In an effort to perceive every thing in coloration, we first want to grasp the coloration in every thing.
Mild waves are on the market on the planet, however coloration occurs within the interplay between mild waves and the attention, mind, and thoughts.
Even when coloration isn’t a bodily factor, we nonetheless need to replicate it as reliably as attainable, particularly within the digital period. If we take a photograph of a fantastic bouquet of lilies (just like the one on my desk) after which show it on a display screen, we anticipate to see the identical colours in each the picture and actuality. Nevertheless, “actuality” here’s a deceptive time period since, as soon as once more, the actuality of coloration will depend on the viewer. To unravel this, we have to perceive how mild wavelengths (one thing measurable and replicable) create completely different coloration responses in viewers (one thing not so measurable).
Fortunately, this process was already carried out 95 years in the past by the Worldwide Fee on Illumination (CIE, by its French identify). I want I may get into the main points of the experiment, however we haven’t gotten into our first coloration thingie but. What’s vital is that from these measurements, the CIE was capable of map all the colours seen to the common human (within the experiment) to mild wavelengths and describe them with solely three values.
This new coloration house additionally has three values, X and Z symbolize the chromaticity of a coloration, whereas Y represents its luminance. Because it has three axes, it makes a 3D form, but when we slice it such that its luminance is similar, we get all of the seen colours for a given luminance in a determine you’ve gotten most likely seen earlier than.

That is referred to as the xy chromaticity diagram and holds all the colours seen by the common human eye (primarily based on the common viewer within the CIE 1931 experiment). Colours inside the form are thought-about actual, whereas these outdoors are deemed imaginary.
Colour Areas
The aim of the final rationalization was to achieve the CIEXYZ Colour Area idea, however what precisely is a “coloration house”? And why is the CIEXYZ Colour Area so vital?
The CIEXYZ Colour Area is a mapping from all the colours seen by the common human eye right into a 3D coordinate system, so we solely want three values to outline a coloration. Then, a coloration house may be considered a common mapping of coloration, without having to incorporate each seen coloration, and it’s often outlined by way of three values as nicely.
RGB Colour Areas
Essentially the most well-known coloration areas are the RGB coloration areas (word the plural). As you might guess from the identify, right here we solely want the quantity of purple, inexperienced, and blue to explain a coloration. And to explain an RGB coloration house, we solely have to outline its “reddest”, “greenest”, and “bluest” values. If we use coordinates going from 0 to 1 to outline a coloration within the RGB coloration house, then:
(1, 0, 0)
means the reddest coloration.(0, 1, 0)
means the greenest coloration.(0, 0, 1)
means the bluest coloration.
Nevertheless, “reddest”, “bluest”, and “greenest” are solely arbitrary descriptions of coloration. What makes a coloration the “bluest” is as much as every particular person. For instance, which of the next colours do you assume is the bluest?

As you may guess, one thing like “bluest” is an appalling description. Fortunately, we simply should look again on the CIEXYZ coloration house — it’s fairly helpful! Right here, we are able to outline what we contemplate the reddest, greenest, and bluest colours simply as coordinates contained in the xy chromaticity diagram. That’s all it takes to create an RGB coloration house, and why there are such a lot of!

In CSS, essentially the most used coloration house is the usual RGB (sRGB) coloration house, which, as you may see within the final picture, leaves a number of colours out. Nevertheless, in CSS, we are able to use trendy RGB coloration areas with much more colours by way of the coloration()
operate, comparable to display-p3
, prophoto-rgb
, and rec2020
.

Discover how the ProPhoto RGB coloration house goes out of the seen coloration. That is okay. Colours outdoors are clamped; they aren’t new or invisible colours.
In CSS, apart from sRGB, now we have two extra coloration areas: the CIELAB coloration house and the Oklab coloration house. Fortunately, as soon as we understood what the CIEXYZ coloration house is, then these two needs to be less complicated to grasp. Let’s dig into that subsequent.
CIELAB and Oklab Colour Areas
As we noticed earlier than, the sRGB coloration house lacks most of the colours seen by the common human eye. And as trendy screens bought higher at displaying extra colours, CSS wanted to undertake newer coloration areas to completely make the most of these newer shows. That wasn’t the one drawback with sRGB — it additionally lacks perceptual uniformity, that means that adjustments within the coloration’s chromaticity additionally change its perceived lightness. Test, for instance, this demo by Adam Argyle:
Created in 1976 by the CIE, CIELAB, derived from CIEXYZ, additionally encompasses all the colours seen by the human eye. It really works with three coordinates: L for perceptual lightness, a for the quantity of red-green, and b* for the quantity of yellow-blue within the coloration.

It has a approach higher perceptual uniformity than sRGB, but it surely nonetheless isn’t utterly uniform, particularly in gradients involving blue. For instance, within the following white-to-blue gradient, CIELAB shifts in direction of purple.

As a ultimate enchancment, Björn Ottosson got here up with the Oklab coloration house, which additionally holds all colours seen by the human eye whereas preserving a greater perceptual uniformity. Oklab additionally makes use of the three L*a*b*
coordinates. Because of all these enhancements, it’s the coloration house I attempt to use essentially the most currently.
Colour Fashions
Once I was studying about these ideas, my largest problem after understanding coloration areas was not getting them confused with coloration fashions and coloration gamuts. These two ideas, whereas complementary and carefully associated to paint areas, aren’t the identical, so they’re a typical pitfall when studying about coloration.
A coloration mannequin refers back to the mathematical description of coloration by way of tuples of numbers, often involving three numbers, however these values don’t give us a precise coloration till we pair them with a coloration house. For instance, you understand that within the RGB coloration mannequin, we outline coloration by way of three values: purple, inexperienced, and blue. Nevertheless, it isn’t till we match it to an RGB coloration house (e.g., sRGB with display-p3) that now we have a coloration. On this sense, a coloration house can have a number of coloration fashions, like sRGB, which makes use of RGB, HSL, and HWB. On the similar time, a coloration mannequin can be utilized in a number of coloration areas.
I discovered loads of articles and tutorials the place “coloration areas” and “coloration fashions” had been used interchangeably. And a few locations had been they’d a unique definition of coloration areas and fashions than the one offered right here. For instance, Chrome’s Excessive definition CSS coloration information defines CSS’s RGB and HSL as completely different coloration areas, whereas MDN’s Colour Area entry does outline RGB and HSL as a part of the sRGB coloration house.
Personally, in CSS, I discover it simpler to grasp the concept of RGB, HSL and HWB as completely different fashions to entry the sRGB coloration house.
Colour Gamuts
A coloration gamut is extra easy to elucidate. You’ll have seen how now we have talked a few coloration house having extra colours than one other, however it will be extra right to say it has a “wider” gamut, since a coloration gamut is the vary of colours obtainable in a coloration house. Nevertheless, a coloration gamut isn’t solely restricted by coloration house boundaries, but in addition by bodily limitations. For instance, an older display screen could lower the colour gamut because it isn’t capable of show every coloration obtainable in a given coloration house. On this case the place a coloration can’t be represented (as a consequence of bodily limitation or being outdoors the colour house itself), it’s stated to be “out of gamut”.
Colour Features
In CSS, the one coloration house obtainable was once sRGB. These days, we are able to work with a number of trendy coloration areas by way of their respective coloration capabilities. As a fast reference, every of the colour areas in CSS makes use of the next capabilities:
- sRGB: We are able to work in sRGB utilizing the ol’ hexadecimal notation, named colours, and the
rgb()
,rgba()
,hsl()
,hsla()
andhwb()
capabilities. - CIELAB: Right here now we have the
lab()
for Cartesian coordinates andlch()
for polar coordinates. - Oklab: Much like CIELAB, now we have
oklab()
for Cartesian coordinates andoklch()
for polar coordinates. - Extra by way of the
coloration()
andcolor-mix()
. Exterior these three coloration areas, we are able to use many extra utilizing thecoloration()
andcolor-mix()
capabilities. Particularly, we are able to use the RGB coloration areas:rgb-linear
,display-p3
,a98-rgb
,prophoto-rgb
,rec2020
and the XYZ coloration house:xyz
,xyz-d50
, orxyz-d65
.
TL;DR
- Colour areas are a mapping between obtainable colours and a coordinate system. In CSS, now we have three principal coloration areas: sRGB, CIELAB, and Oklab, however many extra are accessible by way of the
coloration()
operate. - Colour fashions outline coloration with tuples of numbers, however they don’t give us details about the precise coloration till we pair them with a coloration house. For instance, the RGB mannequin doesn’t imply something till we assign it an RGB coloration house.
- More often than not, we need to speak about what number of colours a coloration house holds, so we use the time period coloration gamut for the duty. Nevertheless, a coloration gamut can be tied to the bodily limitations of a digital camera/show. A coloration could also be out-of-gamut, that means it may well’t be represented in a given coloration house.
- In CSS, we are able to entry all these coloration areas by way of coloration capabilities, of which there are numerous.
- The CIEXYZ coloration house is extraordinarily helpful to outline different coloration areas, describe their gamuts, and convert between them.
References
1 Mild is the principle reason for coloration, however coloration may be created by issues aside from mild. For instance, rubbing your closed eyes mechanically stimulates your retina, creating coloration in what’s referred to as phosphene. ⤴️
2 If unfavourable mild additionally makes you scratch your head, and for more information on how the CIEXYZ coloration house was created, I extremely suggest Douglas A. Kerr The CIE XYZ and xyY Colour Areas paper. ⤴️
3 We additionally have to outline the darkest darkish coloration (“black”) and the lightest mild coloration (“white”). Nevertheless, for well-behaved coloration areas, these two may be abstracted from the reddest, blues, and greenest colours. ⤴️