-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
561 lines (428 loc) · 19.1 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
---
title: oklch()
slug: Web/CSS/color_value/oklch
page-type: css-function
browser-compat: css.types.color.oklch
spec-urls:
- https://drafts.csswg.org/css-color-5/#relative-Oklch
- https://drafts.csswg.org/css-color/#ok-lab
---
{{CSSRef}}
The **`oklch()`** functional notation expresses a given color in the Oklab {{glossary("color space")}}. `oklch()` is the cylindrical form of {{CSSXref("color_value/oklab", "oklab()")}}, using the same `L` axis, but with polar Chroma (`C`) and Hue (`h`) coordinates.
## Syntax
```css
/* Absolute values */
oklch(40.1% 0.123 21.57)
oklch(59.69% 0.156 49.77)
oklch(59.69% 0.156 49.77 / .5)
/* Relative values */
oklch(from green l c h / 0.5)
oklch(from #0000FF calc(l + 0.1) c h)
oklch(from hsl(180 100% 50%) calc(l - 0.1) c h)
oklch(from var(--aColor) l c h / calc(alpha - 0.1))
```
### Values
Below are descriptions of the allowed values for both absolute and [relative colors](/en-US/docs/Web/CSS/CSS_colors/Relative_colors).
> **Note:** Usually when percentage values have a numeric equivalent in CSS, `100%` is equal to the number `1`.
> This is not the case for `oklch()`. Here `100%` is equal to the number `100` for the `L` value and `0.4` for the `C` value.
#### Absolute value syntax
```text
oklch(L C H[ / A])
```
The parameters are as follows:
- `L`
- : A {{CSSXref("<number>")}} between `0` and `1`, a {{CSSXref("<percentage>")}} between `0%` and `100%`, or the keyword `none` (equivalent to `0%` in this case). In this case, the number `0` corresponds to `0%` (black) and the number `1` corresponds to `100%` (white). This value specifies the color's perceived lightness, or "brightness".
> **Note:** The `L` in `oklch()` is the perceived lightness, which refers to the "brightness" we visually perceive with our eyes. This is different from the `L` in `hsl()`, where it represents lightness as compared to other colors.
- `C`
- : A {{CSSXref("<number>")}}, a {{CSSXref("<percentage>")}}, or the keyword `none` (equivalent to `0%` in this case). This value is a measure of the color's chroma (roughly representing the "amount of color"). Its minimum useful value is `0`, while the maximum is theoretically unbounded (but in practice does not exceed `0.5`). In this case, `0%` is `0` and `100%` is the number `0.4`.
- `H`
- : A {{CSSXref("<number>")}}, an {{CSSXref("<angle>")}}, or the keyword `none` (equivalent to `0deg` in this case) representing the color's {{CSSXref("<hue>")}} angle.
> **Note:** The angles corresponding to particular hues differ across the sRGB (used by {{CSSXref("color_value/hsl", "hsl()")}} and {{CSSXref("color_value/hwb", "hwb()")}}), CIELAB (used by {{CSSXref("color_value/lch", "lch()")}}), and Oklab (used by `oklch()`) color spaces. See the [Hues in `oklch()`](#hues_in_oklch) example below and the {{CSSXref("<hue>")}} reference page for more details and examples.
- `A` {{optional_inline}}
- : An {{CSSXref("<alpha-value>")}} representing the alpha channel value of the color, where the number `0` corresponds to `0%` (fully transparent) and `1` corresponds to `100%` (fully opaque). Additionally, the keyword `none` can be used to explicitly specify no alpha channel. If the `A` channel value is not explicitly specified, it defaults to 100%. If included, the value is preceded by a slash (`/`).
> **Note:** See [Missing color components](/en-US/docs/Web/CSS/color_value#missing_color_components) for more information on the effect of `none`.
#### Relative value syntax
```text
oklch(from <color> L C H[ / A])
```
The parameters are as follows:
- `from <color>`
- : The keyword `from` is always included when defining a relative color, followed by a {{cssxref("<color>")}} value representing the **origin color**: This is the original color that the relative color is based on. The origin color can be _any_ valid {{cssxref("<color>")}} syntax, including another relative color.
- `L`
- : A {{CSSXref("<number>")}} between `0` and `1`, a {{CSSXref("<percentage>")}} between `0%` and `100%`, or the keyword `none` (equivalent to `0%` in this case). This represents the lightness value of the output color. Here the number `0` corresponds to `0%` (black) and the number `1` corresponds to `100%` (white).
- `C`
- : A {{CSSXref("<number>")}}, a {{CSSXref("<percentage>")}}, or the keyword `none` (equivalent to `0%` in this case). This value represents the output color's chroma value (roughly representing the "amount of color"). Its minimum useful value is `0`, while its maximum is theoretically unbounded (but in practice does not exceed `0.5`). In this case, `0%` is `0` and `100%` is the number `0.4`.
- `H`
- : A {{CSSXref("<number>")}}, an {{CSSXref("<angle>")}}, or the keyword `none` (equivalent to `0deg` in this case) representing the output color's {{CSSXref("<hue>")}} angle. See a [sample of different hues](#result_3) in the [Examples](#examples) section below.
- `A` {{optional_inline}}
- : An {{CSSXref("<alpha-value>")}} representing the alpha channel value of the output color, where the number `0` corresponds to `0%` (fully transparent) and `1` corresponds to `100%` (fully opaque). Additionally, the keyword `none` can be used to explicitly specify no alpha channel. If the `A` channel value is not explicitly specified, it defaults to the alpha channel value of the origin color. If included, the value is preceded by a slash (`/`).
#### Defining relative color output channel components
When using relative color syntax inside an `oklch()` function, the browser converts the origin color into an equivalent Oklch color (if it is not already specified as such). The color is defined as three distinct color channel values — `l` (lightness), `c` (chroma), and `h` (hue) — plus an alpha channel value (`alpha`). These channel values are made available inside the function to be used when defining the output color channel values:
- The `l` channel value is resolved to a `<number>` between `0` and `1`, inclusive.
- The `c` channel value is resolved to a `<number>` between `0` and `0.4`, inclusive.
- The `h` channel value is resolved to a `<number>` between `0` and `360`, inclusive.
- The `alpha` channel is resolved to a `<number>` between `0` and `1`, inclusive.
When defining a relative color, the different channels of the output color can be expressed in several different ways. Below, we'll study some examples to illustrate these.
In the first two examples below, we are using relative color syntax. However, the first one outputs the same color as the origin color and the second one outputs a color not based on the origin color at all. They don't really create relative colors! You'd be unlikely to ever use these in a real codebase, and would probably just use an absolute color value instead. We included these examples as a starting point for learning about relative `oklch()` syntax.
Let's start with an origin color of `hsl(0 100% 50%)` (equivalent to `red`). The following function outputs the same color as the origin color — it uses the origin color's `l`, `c`, and `h` channel values (`0.627966`, `0.257704`, and `29.2346`) as the output channel values:
```css
oklch(from hsl(0 100% 50%) l c h)
```
This function's output color is `oklch(0.627966 0.257704 29.2346)`.
The next function uses absolute values for the output color's channel values, outputting a completely different color not based on the origin color:
```css
oklch(from hsl(0 100% 50%) 42.1% 0.25 328.363)
```
In the above case, the output color is `oklch(0.421 0.25 328.363)`.
The following function creates a relative color based on the origin color:
```css
oklch(from hsl(0 100% 50%) 0.8 0.4 h)
```
This example:
- Converts the `hsl()` origin color to an equivalent `oklch()` color — `oklch(0.627966 0.257704 29.2346)`.
- Sets the `H` channel value for the output color to that of the origin `oklch()` equivalent's `H` channel value — `29.2346`.
- Sets the output color's `L` and `C` channel values to new values not based on the origin color: `0.8` and `0.4` respectively.
The final output color is `oklch(0.8 0.4 29.2346)`.
> **Note:** As mentioned above, if the output color is using a different color model to the origin color, the origin color is converted to the same model as the output color in the background so that it can be represented in a way that is compatible (i.e. using the same channels).
In the examples we've seen so far in this section, the alpha channels have not been explicitly specified for either the origin or output colors. When the output color alpha channel is not specified, it defaults to the same value as the origin color alpha channel. When the origin color alpha channel is not specified (and it is not a relative color), it defaults to `1`. Therefore, the origin and output alpha channel values are `1` for the above examples.
Let's look at some examples that specify origin and output alpha channel values. The first one specifies the output alpha channel value as being the same as the origin alpha channel value, whereas the second one specifies a different output alpha channel value, unrelated to the origin alpha channel value.
```css
oklch(from hsl(0 100% 50% / 0.8) l c h / alpha)
/* Computed output color: oklch(0.627966 0.257704 29.2346 / 0.8) */
oklch(from hsl(0 100% 50% / 0.8) l c h / 0.5)
/* Computed output color: oklch(0.627966 0.257704 29.2346 / 0.5) */
```
In the following example, the `hsl()` origin color is again converted to the `oklch()` equivalent — `oklch(0.627966 0.257704 29.2346)`. {{cssxref("calc")}} calculations are applied to the `L`, `C`, `H`, and `A` values, resulting in an output color of `oklch(0.827966 0.357704 9.23462 / 0.9)`:
```css
oklch(from hsl(0 100% 50%) calc(l + 0.2) calc(c + 0.1) calc(h - 20) / calc(alpha - 0.1))
```
> **Note:** Because the origin color channel values are resolved to `<number>` values, you have to add numbers to them when using them in calculations, even in cases where a channel would normally accept `<percentage>`, `<angle>`, or other value types. Adding a `<percentage>` to a `<number>`, for example, doesn't work.
## Formal syntax
{{csssyntax}}
## Examples
### Adjusting the brightness of a color
This example shows the effect of varying the `L` (lightness) value of the `oklch()` functional notation.
#### HTML
```html
<div data-color="blue-dark"></div>
<div data-color="blue"></div>
<div data-color="blue-light"></div>
<div data-color="red-dark"></div>
<div data-color="red"></div>
<div data-color="red-light"></div>
<div data-color="green-dark"></div>
<div data-color="green"></div>
<div data-color="green-light"></div>
```
#### CSS
```css hidden
body {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 20px;
}
div {
height: 50px;
border: 1px solid black;
}
```
```css
[data-color="blue-dark"] {
background-color: oklch(10% 0.4 240);
}
[data-color="blue"] {
background-color: oklch(50% 0.4 240);
}
[data-color="blue-light"] {
background-color: oklch(90% 0.4 240);
}
[data-color="red-dark"] {
background-color: oklch(10% 0.4 20);
}
[data-color="red"] {
background-color: oklch(50% 0.4 20);
}
[data-color="red-light"] {
background-color: oklch(90% 0.4 20);
}
[data-color="green-dark"] {
background-color: oklch(10% 0.4 130);
}
[data-color="green"] {
background-color: oklch(50% 0.4 130);
}
[data-color="green-light"] {
background-color: oklch(90% 0.4 130);
}
```
#### Result
{{EmbedLiveSample("Adjusting the brightness of a color", "", "200")}}
### Adjusting color intensity via chroma
The following example shows the effect of varying the `C` (chroma) value of the `oklch()` functional notation, with colors decreasing in intensity as the `C` value decreases from fully saturated to almost grey.
#### HTML
```html
<div data-color="blue"></div>
<div data-color="blue-chroma1"></div>
<div data-color="blue-chroma2"></div>
<div data-color="blue-chroma3"></div>
<div data-color="red"></div>
<div data-color="red-chroma1"></div>
<div data-color="red-chroma2"></div>
<div data-color="red-chroma3"></div>
<div data-color="green"></div>
<div data-color="green-chroma1"></div>
<div data-color="green-chroma2"></div>
<div data-color="green-chroma3"></div>
```
#### CSS
With the initial starting colors blue, red, and green, we declare progressively smaller values for chroma on them: starting from full color saturation at the high value of `0.4` (equivalent to `100%`) down to `0.01` (equivalent to `2%`), which is almost grey for all the colors.
```css hidden
body {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
gap: 20px;
}
div {
height: 50px;
border: 1px solid black;
}
```
```css
[data-color="blue"] {
background-color: oklch(50% 0.4 240);
}
[data-color="blue-chroma1"] {
background-color: oklch(50% 0.2 240);
}
[data-color="blue-chroma2"] {
background-color: oklch(50% 0.1 240);
}
[data-color="blue-chroma3"] {
background-color: oklch(50% 0.01 240);
}
[data-color="red"] {
background-color: oklch(50% 100% 20deg);
}
[data-color="red-chroma1"] {
background-color: oklch(50% 50% 20deg);
}
[data-color="red-chroma2"] {
background-color: oklch(50% 25% 20deg);
}
[data-color="red-chroma3"] {
background-color: oklch(50% 2% 20deg);
}
[data-color="green"] {
background-color: oklch(50% 0.4 130);
}
[data-color="green-chroma1"] {
background-color: oklch(50% 0.2 130);
}
[data-color="green-chroma2"] {
background-color: oklch(50% 0.1 130);
}
[data-color="green-chroma3"] {
background-color: oklch(50% 0.01 130);
}
```
#### Result
{{EmbedLiveSample("Adjusting color intensity via chroma", '', '200')}}
If we had used `0` instead of `0.01` and `2%`, with the same lightness values, the colors would have all been the same shade of grey. In this example, they are almost grey.
### Hues in oklch
The following example shows swatches with different `H` (hue) values of the `oklch()` functional notation.
#### HTML
```html
<div data-color="0">0deg</div>
<div data-color="20">20deg</div>
<div data-color="40">40deg</div>
<div data-color="60">60deg</div>
```
and so on.
```html hidden
<div data-color="80">80deg</div>
<div data-color="100">100deg</div>
<div data-color="120">120deg</div>
<div data-color="140">140deg</div>
<div data-color="160">160deg</div>
<div data-color="180">180deg</div>
<div data-color="200">200deg</div>
<div data-color="220">220deg</div>
<div data-color="240">240deg</div>
<div data-color="260">260deg</div>
<div data-color="280">280deg</div>
<div data-color="300">300deg</div>
<div data-color="320">320deg</div>
<div data-color="340">340deg</div>
<div data-color="360">360deg</div>
```
#### CSS
```css hidden
body {
display: flex;
flex-wrap: wrap;
gap: 3px;
}
div {
flex: 0 0 4em;
text-align: center;
line-height: 4em;
display: inline-block;
border: 1px solid black;
color: white;
font-family: monospace;
}
```
```css
[data-color="0"] {
background-color: oklch(50% 0.4 0deg);
}
[data-color="20"] {
background-color: oklch(50% 0.4 20deg);
}
[data-color="40"] {
background-color: oklch(50% 0.4 40deg);
}
[data-color="60"] {
background-color: oklch(50% 0.4 60deg);
}
```
and so on.
```css hidden
[data-color="80"] {
background-color: oklch(50% 0.4 80deg);
}
[data-color="100"] {
background-color: oklch(50% 0.4 100deg);
}
[data-color="120"] {
background-color: oklch(50% 0.4 120deg);
}
[data-color="140"] {
background-color: oklch(50% 0.4 140deg);
}
[data-color="160"] {
background-color: oklch(50% 0.4 160deg);
}
[data-color="180"] {
background-color: oklch(50% 0.4 180deg);
}
[data-color="200"] {
background-color: oklch(50% 0.4 200deg);
}
[data-color="220"] {
background-color: oklch(50% 0.4 220deg);
}
[data-color="240"] {
background-color: oklch(50% 0.4 240deg);
}
[data-color="260"] {
background-color: oklch(50% 0.4 260deg);
}
[data-color="280"] {
background-color: oklch(50% 0.4 280deg);
}
[data-color="300"] {
background-color: oklch(50% 0.4 300deg);
}
[data-color="320"] {
background-color: oklch(50% 0.4 320deg);
}
[data-color="340"] {
background-color: oklch(50% 0.4 340deg);
}
[data-color="360"] {
background-color: oklch(50% 0.4 360deg);
}
```
#### Result
{{EmbedLiveSample("hues in oklch")}}
The hue angles in `oklch()` are different from those in {{CSSXref("color_value/hsl", "hsl()")}}. See {{cssxref("hue")}} for more information. In `hsl()`, the sRGB color `0deg` represents red. However, in the CIELab color space, `0deg` corresponds to magenta, while red is approximately `41deg`.
### Adjusting the alpha value of a color
The following example shows the effect of varying the `A` (alpha) value of the `oklch()` color function.
The `red` and `red-alpha` elements overlap the `#background-div` element to demonstrate the effect of opacity.
Giving `A` a value of `0.4` makes the color 40% opaque.
#### HTML
```html
<div id="background-div">
<div data-color="red"></div>
<div data-color="red-alpha"></div>
</div>
```
#### CSS
```css hidden
div {
width: 50px;
height: 50px;
padding: 5px;
margin: 5px;
display: inline-block;
border: 1px solid black;
}
#background-div {
background-color: oklch(100% 0.57 217);
width: 150px;
height: 40px;
}
```
```css
[data-color="red"] {
background-color: oklch(50% 0.5 20);
}
[data-color="red-alpha"] {
background-color: oklch(50% 0.5 20 / 0.4);
}
```
#### Result
{{EmbedLiveSample("adjusting_the_alpha_value_of_a_color")}}
### Using relative colors with oklch()
This example styles three {{htmlelement("div")}} elements with different background colors. The middle one is given the unmodified `--base-color`, while the left and right ones are given lightened and darkened variants of that `--base-color`.
These variants are defined using relative colors — the `--base-color` [custom property](/en-US/docs/Web/CSS/--*) is passed into an `oklch()` function, and the output colors have their lightness channel modified to achieve the desired effect via a `calc()` function. The lightened color has `0.15` (15%) added to the lightness channel, and the darkened color has `0.15` (15%) subtracted from the lightness channel.
```html hidden
<div id="container">
<div class="item" id="one"></div>
<div class="item" id="two"></div>
<div class="item" id="three"></div>
</div>
```
#### CSS
```css hidden
#container {
display: flex;
width: 100vw;
height: 100vh;
box-sizing: border-box;
}
.item {
flex: 1;
margin: 20px;
}
```
```css
:root {
--base-color: orange;
}
#one {
background-color: oklch(from var(--base-color) calc(l + 0.15) c h);
}
#two {
background-color: var(--base-color);
}
#three {
background-color: oklch(from var(--base-color) calc(l - 0.15) c h);
}
```
#### Result
The output is as follows:
{{ EmbedLiveSample("Using relative colors with oklch()", "100%", "200") }}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [List of all color notations](/en-US/docs/Web/CSS/color_value)
- [Using relative colors](/en-US/docs/Web/CSS/CSS_colors/Relative_colors)
- [CSS colors](/en-US/docs/Web/CSS/CSS_colors) module
- {{CSSXref("<hue>")}} data type
- {{cssxref("color_value/lch","lch()")}} and {{cssxref("color_value/oklab","oklab()")}} color functions
- [A perceptual color space for image processing](https://bottosson.github.io/posts/oklab/)
- [OKLCH in CSS](https://evilmartians.com/chronicles/oklch-in-css-why-quit-rgb-hsl)