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()ย andยhwb()ย capabilities. - CIELAB:ย Right here now we have theย
lab()ย for Cartesian coordinates andยlch()ย for polar coordinates. - Oklab:ย Much like CIELAB, now we haveย
oklab()ย for Cartesian coordinates andยoklch()ย for polar coordinates. - Extra by way of theย
coloration()ย andยcolor-mix().ย Exterior these three coloration areas, we are able to use many extra utilizing theยcoloration()ย andยcolor-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, orยxyz-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. โคด๏ธ








