Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

CSS  Module 6 – Colour and theming

OKLCH, Oklab and wide-gamut colour

Write colours in oklch(), build evenly stepped palettes by OKLCH lightness, and use Display P3 colours with an sRGB fallback and color-gamut.

  • Intermediate
  • 30 minutes
  • Examples run with Node.js 24.21.0 and quickjs 0.32.0
  • By MySmartCoPilot
Free core, Pro depth: 11 of 30 minutes free
Free: Read lightness chroma and hue. With a pass: 2 in-depth parts and 3 interview answers, about 19 minutes.

What you will learn

  • Write colours in oklch() and explain lightness, chroma and hue
  • Build an evenly stepped palette by varying OKLCH lightness
  • Predict what an sRGB screen shows for a colour outside its gamut
  • Use display-p3 colours with an sRGB fallback and the color-gamut media query

Before you start

What you’ll learn · Before you start

What you will learn

  • Write colours in oklch() and explain lightness, chroma and hue
  • Build an evenly stepped palette by varying OKLCH lightness
  • Predict what an sRGB screen shows for a colour outside its gamut
  • Use display-p3 colours with an sRGB fallback and the color-gamut media query

Before you start

On this page

The supplied nine-step palettes need white labels that pass across three hues. At The L coordinate of OKLCH, describing perceptual lightness separately from chroma and hue. 0.55, their recorded ratios are 4.89, 4.63 and 4.87; equal HSL lightness does not line those decisions up. OKLCH separates lightness, A coordinate describing how far a colour is from a neutral colour at its lightness. and hue so you can state the intended edit, while A perceptual colour space with lightness and two rectangular colour coordinates; OKLCH is its polar form. expresses the same space with rectangular colour axes.

Display P3 can show colours outside A colour space used by CSS hex, RGB and HSL values, with a defined range of reproducible colours., but understanding their syntax does not enlarge a screen’s range. Keep an sRGB branch and choose its P3 enhancement with the gamut query, checking each visible pair. The supplied programmes and pixel tables make those decisions concrete.

Read lightness chroma and hue

Wide-gamut colour uses a colour range larger than sRGB. Wide-gamut colour still needs an sRGB fallback when the target display cannot reproduce that range.

This lesson builds the same palette in HSL and in OKLCH and measures its contrast, checks which colours fit which screens, and reads the pixels that Chromium 153, Firefox 155 and WebKit 26.6 painted for a colour an sRGB screen can’t show.

  • Lightness is a number from 0 to 1, or a percentage where 100% means 1. 0 is black and 1 is white, whatever the other two numbers say.
  • Chroma is 0 for a gray. A percentage is allowed too, with 100% meaning 0.4. The most colourful sRGB colours reach about 0.32 (the gamut program below finds pure blue #0000ff at oklch(0.452 0.313 264.1)), and Display P3’s pure green reaches 0.369, so in practice chroma stays below 0.4.
  • Hue is an angle in degrees. CSS Color 4 gives the landmarks: 0 points to a purplish red, 90 to a mustard yellow, 180 to a greenish cyan and 270 to a sky blue. The angles are not those of HSL: HSL’s blue is at 240, while #0000ff sits at 264 in OKLCH.

An alpha comes after a slash, as in every modern colour function: oklch(0.55 0.15 262 / 50%). The keyword none marks a missing component, which matters mostly when colours are mixed. Commas are not allowed in oklch() at all: it was never part of the legacy syntax.

Oklab is the same space in rectangular form: oklab(0.55 -0.02 -0.15) gives the lightness, then a, from green (negative) to a purplish red (positive), and b, from blue (negative) to yellow (positive). OKLCH is easier to edit by hand, because hue and chroma are separate numbers; Oklab is what browsers use by default when they mix colours, which the next lesson covers. CSS also has the older CIE Lab and LCH as lab() and lch(). They were the first perceptual spaces in CSS, but blues in LCH drift towards purple as their chroma falls, a flaw Oklab was designed to fix. oklab(), oklch(), lab(), lch() and color() are all Baseline widely available.

A script reads an oklch() colour back in its own form, not as rgb(): getComputedStyle() reported oklch(0.4 0.15 264) for oklch(0.4 0.15 264) in all three engines, with the lightness as a number. Converting it to sRGB would lose any colour outside sRGB, so CSS Color 4 keeps it.

Color Converter Paste a hex colour to see its oklch() value, or an oklch() value to see the nearest hex.

Color-gamut reports roughly which display range the browser and screen support. It chooses a display branch and does not decide whether a colour notation parses.

Interactive · step by step Three coordinates separate Read each coordinate of the supplied oklch(0.55 0.15 262).
Three coordinates separate, step 3 of 3. Showing Lightness (L = 0.55), Chroma (C = 0.15), Hue (H = 262 degrees). In focus: Hue.readdecideLightnessL = 0.55ChromaC = 0.15HueH = 262 degrees

The drawing shows the whole scene; its 3 steps are listed below.

Every step as text
  1. Read the first coordinate. Lightness 0.55 locates this colour between black and white. The coordinate is independent of the optional alpha after a slash.
  2. Change the second. Chroma 0.15 supplies its colourfulness. Setting chroma to zero makes a neutral gray; it does not make the alpha zero.
  3. Keep the third. Hue 262 is the blue direction in this space. Holding hue while editing lightness or chroma states which property of the colour should remain.

The original blue uses lightness 0.55, chroma 0.15 and hue 262. Editing one coordinate states which property of the colour should change; alpha remains a separate optional component.

Checkpoint

Check: Read lightness chroma and hue

  1. Question 1 of 2 What changes when oklch(0.55 0.15 262) becomes oklch(0.55 0 262)?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: The chroma becomes zero, giving a neutral gray

    The second coordinate controls chroma and the first still holds lightness.

  2. Question 2 of 2 A designer wants less colourfulness while keeping this lightness and hue. Which coordinate should change?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: Lower C and keep L and H

    The edit states which coordinate is intended to change.

Build palette steps and trace display gamut Pro: opens with a Pro, Premium or Ultimate pass

Measure every supplied palette step and keep its output range in the decision. The recorded clipping cases separate a valid colour from the pixel an sRGB surface showed.

In this part:

  • Check every palette step
  • Distinguish syntax and display range
  • Build a scale that stays in its chosen range
  • Build a nine-step OKLCH scale that every screen can show
    • Results of the sample tests

3 diagrams · 3 runnable examples (HTML, JavaScript) · 1 exercise with sample tests · about 14 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Choose and audit both accent branches Pro: opens with a Pro, Premium or Ultimate pass

Select syntax support and display range independently. Keep both original accent colours and every foreground with the branch that actually uses it.

In this part:

  • Choose the right fallback branch
  • Keep the coordinate starting case in context
  • Keep the depth decisions together

1 diagram · 1 runnable example (HTML) · about 6 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Practise: choose coordinates before comparing colours

Practice that adapts to you

Practise this lesson

2 questions about 4 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 2 What does the second number of oklch(0.55 0.15 262) describe?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: How colourful it is (chroma), from 0 for a gray upwards

    OKLCH lists lightness, chroma and hue in that order. Chroma 0 is a gray; the most colourful sRGB colours reach about 0.32, and Display P3's green about 0.37. An alpha would follow a slash, as in oklch(0.55 0.15 262 / 50%).

  2. Question 2 of 2 An element has color: oklch(0.4 0.15 264). What did getComputedStyle(element).color return in the lesson’s three-engine capture?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: oklch(0.4 0.15 264)

    Unlike hex, rgb(), hsl() and hwb(), which are reported as rgb(), an oklch() colour keeps its own form, with the lightness as a number between 0 and 1, because converting it to sRGB could lose colours outside sRGB. All three engines in the lesson's run reported oklch(0.4 0.15 264).

Interview questions

Fresher · What do the three numbers in oklch(0.55 0.15 262) mean, and why use OKLCH instead of HSL for a palette?

The first number is the lightness, from 0 for black to 1 for white; the second is the chroma, how colourful the colour is, from 0 for a gray to about 0.32 for the most vivid sRGB colours; the third is the hue in degrees, where 262 is a blue. OKLCH is built on Oklab, which was designed so that equal steps in its numbers look like equal steps to the eye. HSL’s lightness is only the average of the largest and smallest channel, so the same HSL lightness gives yellow 13 times the luminance of blue. In the supplied chroma-0.1 scale, white text at lightness 0.55 passes for its three hues at 4.89, 4.63 and 4.87. That is a measured starting pattern; changing chroma, mapping, alpha or backing requires a new visible-pair comparison.

  1. Mid to senior

    A design system built its colour scales by changing HSL lightness in 10% steps. Teams complain that white text on step 50% is readable on the blue scale but not on the green or yellow ones. How would you rebuild the scales, and what rule would you give teams?

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Mid to senior

    Marketing wants a vivid Display P3 green for the main Buy button, but many visitors use sRGB laptops. How do you ship it so that both groups get a good, accessible button?

    Model answer, Pro. First step and follow-ups

  3. Mid to staff

    A button has an sRGB default and a Display P3 enhancement. Its syntax test passes on an sRGB laptop, but a designer calls that proof the P3 branch is accessible. Design a regression matrix that separates syntax, gamut, state and contrast.

    Model answer, Pro. First step and follow-ups

Key takeaways

  • oklch(L C H) uses lightness from 0 to 1, nonnegative chroma and hue in degrees; the chroma that fits depends on the colour and gamut, rather than a syntax maximum of 0.37. oklab() uses the same space with a and b axes.

  • Give sRGB screens a colour you chose with @media (color-gamut: p3), and check contrast for both values of the token.

  • Editing lightness, chroma or hue states which coordinate should change while the others remain fixed.

Correction: the contrast starting rows describe the supplied chroma-0.1 trials. They do not approve another chroma, output mapping, alpha, backing or label without checking its actual pair.

Terms in this lesson

Chroma
A coordinate describing how far a colour is from an achromatic colour at its lightness.
Gamut
The range of colours a colour space or output device can represent.
OKLCH lightness
The L coordinate of OKLCH, describing perceptual lightness separately from chroma and hue.

Corrections

  • Kept captured clipping separate from general gamut mapping and aligned the scale hint with its chroma ceiling.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.