@@ -9,12 +9,13 @@ Sets the background color of a component.
99- Known color name: ` red ` , ` limegreen ` , ` transparent `
1010- Hex color: ` #a5a5a5 ` , ` #777 ` , ` #00ff0033 `
1111- RGBA color: ` rgba(23, 16, 168, 0.3) ` , ` rgb(23 16 168 / 30%) `
12- - HSL color: ` hsl(152, 37%, 59%) ` , ` hsla(152 37% 59% / 0.75) `
12+ - HSL color: ` hsl(152, 37%, 59%) ` , ` hsla(152 37% 59% / 0.75) ` , ` hsl(152 37 59) `
1313- OKLCh color: ` oklch(63.7% 0.237 25.331) ` , ` oklch(0.5 0.2 30deg / 50%) `
1414- OKLab color: ` oklab(0.5 0.1 -0.1) `
1515- LCh color: ` lch(54.29% 106.84 40.86) ` , ` lch(70 30 0.5turn) `
1616- Lab color: ` lab(50 40 -30) ` , ` lab(50 40 -30 / 50%) `
1717- Mixed color: ` color-mix(in oklab, red 30%, blue) ` — see below
18+ - Relative color: ` rgb(from var(--brand) r g b / 50%) ` — see below
1819- CSS keywords: ` inherit `
1920
2021Every component of these four is a number or a percentage, and one written as ` none ` is ` 0 ` . What
@@ -28,7 +29,10 @@ Every component of these four is a number or a percentage, and one written as `n
2829| ` lab() ` | ` 0 ` –` 100 ` | ` 125 ` |
2930
3031So ` oklch(0.637 …) ` and ` oklch(63.7% …) ` are the same lightness, while in ` lch() ` both ` 54.29 ` and
31- ` 54.29% ` mean ` 54.29 ` . Hue takes any angle unit.
32+ ` 54.29% ` mean ` 54.29 ` . In ` hsl() ` and ` hsv() ` saturation and lightness run ` 0 ` –` 100 ` , so the ` % ` may
33+ be left off: ` hsl(152 37 59) ` is ` hsl(152 37% 59%) ` . Hue takes any angle unit and wraps, so ` 512deg `
34+ is ` 152deg ` . A component outside its range is clamped rather than making the declaration invalid, so
35+ ` rgb(300 0 0) ` is red and ` hsl(0 150 50) ` is a saturation of ` 100% ` .
3236
3337` color-mix() ` interpolates two colors in ` srgb ` , ` srgb-linear ` , ` hsl ` , ` lab ` , ` lch ` , ` oklab ` or
3438` oklch ` . Each percentage is optional and may be written on either side of its color; giving one
@@ -37,6 +41,34 @@ is premultiplied, so mixing with `transparent` fades the color instead of darken
3741spaces — ` hsl ` , ` lch ` and ` oklch ` — the hue may carry ` shorter hue ` , ` longer hue ` , ` increasing hue `
3842or ` decreasing hue ` , defaulting to ` shorter ` .
3943
44+ ### Relative colors { /* relative-colors*/ }
45+
46+ Every color function above also takes a ` from <color> ` origin, which is CSS Color 5's relative color
47+ syntax. The origin is converted into the function's own space, and each of its channels becomes a
48+ keyword the channel slots can use — on its own, or inside ` calc() ` :
49+
50+ ``` css
51+ .button {
52+ /* A hover shade of whatever the button's own color turns out to be */
53+ background-color : oklch(from var (--brand ) calc (l * 0.8 ) c h);
54+ /* The same color at half opacity, without repeating it */
55+ border-color : rgb (from var (--brand ) r g b / 50% );
56+ /* The complement */
57+ outline-color : hsl (from var (--brand ) calc (h + 180 ) s l);
58+ }
59+ ```
60+
61+ The keywords are ` r g b ` in ` rgb() ` , ` h s l ` in ` hsl() ` , ` h s v ` in ` hsv() ` , ` l a b ` in ` lab() ` and
62+ ` oklab() ` , ` l c h ` in ` lch() ` and ` oklch() ` , and ` alpha ` in all of them. Any of them may appear in
63+ any slot, so ` rgb(from red b g r) ` is blue. Each is worth what the slot would make of that number,
64+ so ` r ` is ` 0 ` –` 255 ` , ` hsl() ` 's ` s ` is ` 0 ` –` 100 ` , ` oklch() ` 's ` l ` is ` 0 ` –` 1 ` , a hue is degrees and
65+ ` alpha ` is ` 0 ` –` 1 ` — the ranges listed above.
66+
67+ An omitted alpha is the origin's own rather than ` 1 ` , so ` rgb(from <color> r g b) ` is that color
68+ unchanged. The origin may be any color, including ` currentColor ` , a ` var() ` , a ` color-mix() ` or
69+ another relative color; one that is not a color makes the declaration invalid. Only the
70+ space-separated form is relative — ` rgb(from red, r, g, b) ` is not.
71+
4072A color outside the sRGB gamut is clamped per channel, which shifts the hue of a very saturated
4173color. Naming an interpolation space that is not in the list above, such as ` display-p3 ` or
4274` rec2020 ` , makes the declaration invalid rather than silently mixing in a different space.
0 commit comments