lab() and lch() in CSS.
This page is also about function(s):
lch()L*C*H* system.oklab()L*a*b* system with a perceptual correction.oklch()L*C*H* system with a perceptual correctionSummary of characteristics of the lab() function
L*a*b* system.0 1 autoComputed value: during an animation, the lab() property gradually changes from one value to another.Per grammar: serialization in the order of syntax.Syntactic diagram of functions lab() and lch().
The terms on the diagram are described below:
coloris a possible reference color, introduced by thefromkeyword, and expressed in one of the syntaxes recognized by CSS (see Colors).lis a unitless number, ranging between0and100, or a percentage.aandbare two numbers without units, ranging from-160to+160, or percentages.αis a unitless number between0and1, or a percentage, introduced by the slash character. It represents the opacity of the color (alpha channel).
In this second diagram, the terms are described below:
coloris a possible reference color, introduced by thefromkeyword, and expressed in one of the syntaxes recognized by CSS (see Colors).lis a unitless number, ranging between0and100or a percentage.cis a unitless number, ranging from0to230, or a percentage.his a number followed by one of the CSS angle units.αis a unitless number between0and1, or a percentage, introduced by the slash character. It represents the opacity of the color (alpha channel).
Description of the functions lab() and lch().
The color spaces L*a*b* and L*C*H* were defined by the CIE (International Commission on Illumination) to characterize surface colors.
They are poorly suited for colors displayed on a screen, but well suited for printed colors.
The three dimensions that define a color in these spaces can be represented in the following diagram. We can see that L*a*b* is a Cartesian
reference system, while L*C*H* is a polar coordinate system.
The L*a*b* space is characterized by three values:
Lis lightness (Lightness in English). The higher the lightness, the lighter the color, with white corresponding to a lightness of100.ais the position of the color on the green-red axis.bis the position of the color on the blue-yellow axis.
The L*C*H* space is characterized by the three values:
Lis lightness.Cis the chrominance. A low value corresponds to a dull color, close to gray, while a high value corresponds to a vivid color.His the hue. It is expressed in degrees and indicates the position of the color on the color wheel.0degcorresponds to a color between magenta and red.
The lab() and lch() functions can be used with:
All properties that expect a color definition.
accent-color: Defines the colour of active or checked items.background-color: Sets the background color.border-color: Sets the border color for all four sides of the element.
caret-color: Chooses the color of the text marker (flashing cursor).
color: Sets the foreground color and assigns a value tocurrentcolor.column-rule-color: Defines the color of the column-separating rules in the context of a multi-column layout, a grid container, or a flex container.
lighting-color: Sets the color of the lighting in the context of an SVG filter.
outline-color: Sets the color of the outline.
scrollbar-color: Sets two colors that will be used for scrollbars.
text-decoration-color: Defines the color of the decorative line (underlined, stripe...).
text-emphasis-color: Sets the color of the emphasis characters.
Syntax of the lab() and lch() functions.
All of these functions accept three parameters separated by spaces, and a fourth, optional one, separated by a slash ( / ).
Commas are not accepted (this is the current trend in CSS syntax). A fourth parameter is now recognized: it is a reference color from which
the new color will be calculated.
- <propriété>: lab(67.5345% -8.6912 -41.601); <propriété>: lab(67.5345% -8.6912 -41.601 / 0.8); l a b α
The first three values define the color. The fourth value, optional, is separated from the others by a slash. It defines the opacity of the color. If this fourth value is not specified, the opacity is set by default to
1(full opacity).l: the lightness of the color. It must be a number between0and100, without a unit, or a percentage between0%and100%.0is black;100is white.aandb: two unitless numbers between-160and160. In fact, these two values can exceed these limits, but beyond that, the color difference is no longer noticeable. Ifaandbare expressed as percentages,-100%corresponds to-125and100%corresponds to125.α: the color opacity, a unitless number between0and1or a percentage between0%and100%. Total transparency is noted as0and full opacity corresponds to1.
- <propriété>: lch(52.2345% 72.25 45deg); <propriété>: lch(52.2345% 72.25 45deg / 0.7); l c h α
As with the
lab()function, the first three values define the color, and the fourth value (optional) defines the opacity of this color.l: the lightness of the color. It must be a number between0and100, without a unit, or a percentage between0%and100%.0is black;100is white.c: chrominance: a unitless number between0and230. Higher values are allowed but no longer produce a noticeable effect on the resulting color. This value can also be expressed as a percentage with the correspondence100%=150.h: hue. A number followed by an angle unit between0degand360deg. Values outside this range are brought back to the equivalent value within the range.
Here are some approximate values for hueh:
- Red (red,maroon...) =40.9deg
- Green (green,lime...) =134.4deg
- Blue (blue,navy...) =301.4deg
- Cyan (cyan,teal...) =196.5deg
α: the opacity of the color, a unitless number between0and1or a percentage between0%and100%.
0corresponds to a completely transparent color, and1or100%to a completely opaque color.
- <propriété>: oklab(67.534% -0.3911 -0.2019); <propriété>: oklab(67.534% -0.3911 -0.2019 / 0.5); l a b α
The function
oklab()provides a more perceptual rendering thanlab(). Its parameters have the same meaning but vary within different ranges.l: la clarté de la couleur. Ce doit être un nombre sans unité entre0(noir) et100(blanc) ou un pourcentage entre0%et100%.aandb: two unitless numbers between-0.5and0.5. Larger values can be indicated without disturbing the syntax, but the effect on the color will be negligible.aandbcan also be expressed as percentages, with-100%corresponding to-0.4and100%corresponding to0.4.α: the opacity of the color, a unitless number between0and1. Percentages are not allowed for this fourth value.0corresponds to a fully transparent color, and1to a fully opaque color.
- <propriété>: oklch(52.2345% 72.2 56.2); <propriété>: oklch(52.2345% 72.2 56.2 / 0.7); l c h α
oklch()gives a more perceptually accurate rendering than the basic functionlch().l: the brightness of the color. It must be a unitless number between0and100. It can also be a percentage between0%and100%. Black corresponds to0and white to100or100%.c: Chrominance: a unitless number between0and0.5. Higher values are allowed but no longer produce a noticeable effect on the resulting color.h:the hue. A number followed by an angle unit between0degand360deg. Values outside this range are brought back to the equivalent value within the range.α: this fourth optional value is the color's opacity, a unitless number between0deg(transparent color) and1(completely opaque).
Color animation.
It is very easy to create very smooth animations with the functions lab(), lch(), oklab(), or oklch().
For this example, we used lab() by varying only the h parameter (the hue).
There is a small trap when varying the hue from 0 to 360 degrees: if you only define the start and end values, some browsers will take the shortest path, not going through the full range of hues, but instead going backward to go directly from 0 to 360. To avoid this, you need to define an intermediate step at 50% for 180 degrees.
Simulator.
Compatibilité des navigateurs.
Defining a color with the L*a*b* or L*C*H* systems is now possible on all browsers.
lab() and lch() functions defining a color in the CIE color space, closer to human perception.oklab() and oklch() functions.lab()lch()functions
oklab()oklch()functions
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

Baidu Browser

QQ Browser

Opéra Mobile

Firefox

Chrome

Safari

Edge

Opéra

Chrome pour Androïd

Safari sur IOS

Samsung Internet

UC Browser pour Androïd

Androïd Brower

Firefox pour Androïd

KaiOS Browser

Opéra mini
Historic of the lab() function.
-
CSS Color Module Level 4
This level 4 of the specification finally introduces the concept of color spaces, with a number of predefined spaces.
Introduction of the HWB (Hue, White, and Black) color system and the CIE (International Commission on Illumination) referencesLabandLCH.
Introduction of thecolor()function, which allows defining a color in a color space other thansRGB.Regardinglab(). Introduction of device-independent color definition systems: CIE Lab, and LCH.
And definition of the corresponding functions:lab(),lch(),oklab()andoklch().July 05, 2016Working Draft.July 05, 2022Candidate Recommendation. -
CSS Color Module Level 5
This level 5 of the specification introduces custom color spaces and relative colors.
All functions that allow defining a color now accept thefromclause to define a color relative to another. The@color-profileat-rule allows you to create your own colorimetric spaces.
Added new functions such ascolor-mix(),device-cmyk(),light-dark(), orcontrast-color().Regardinglab(). Introduction of relative colors, defined in relation to another color.
Adjustment of the syntax of thelab(),lch(),oklab()andoklch()functions for satisfy this new functionality (fromclause).March 03, 2020Working Draft.
See also, regarding colors.
The functions lab() and lch(), as well as oklab() and oklch(), are described in the CSS Color Module module,
which encompasses everything related to color management. The following definitions are also described in this module:
Properties:
currentcolor.Functions:
sRGB, sRGB-linear, profoto, etc).

L*a*b* system.L*C*H* system.L*a*b* system with a perceptual correction.L*C*H* system with a perceptual correction
At-rules:

color() function.Descriptors :

@color-profile at-rule.
@color-profile directive. Defines the method for converting from one color profile to another.


