Sub topic 1
Why hex, rgb() and hsl() fall short
Prerequisites
Assume basic CSS selectors, custom properties, and familiarity with hex, rgb(), and hsl() notation.
Why hex, rgb() and hsl() fall short
Hex and rgb() describe how much red, green, and blue light a color contains. They are useful for exact values, but they do not describe color in the way people see it.
hsl() separates hue, saturation, and lightness, which makes some adjustments easier. However, its lightness is mathematical, not perceptual. Increasing 50% lightness does not make every hue appear equally bright. Yellow can look much brighter than blue even when both use the same HSL lightness.
This makes hand-built palettes inconsistent. A button's hover color may become too bright for one hue and too dark for another.
Perceptual uniformity: what Oklab and OKLCH fix
Oklab is a color model designed so numeric changes better match perceived changes. OKLCH is its cylindrical form:
| Component | Meaning |
|---|---|
| `l` | Perceived lightness |
| `c` | Chroma, or color intensity |
| `h` | Hue angle around the color wheel |
Changing only l usually creates a more predictable shade. Changing only c adjusts vividness, while changing h rotates the hue.
Writing colors with `oklch()`
CSS accepts lightness as a percentage or number, chroma as a number, and hue in degrees. An optional alpha value follows a slash.
:root {
--brand: oklch(62% 0.19 255);
--brand-soft: oklch(78% 0.08 255 / 0.7);
}
.button:hover {
background: oklch(52% 0.19 255);
}The hover state keeps the same chroma and hue while lowering lightness. The browser may reduce an out-of-gamut color to fit the display's available colors, so test important colors on real browsers and screens.
Mixing colors with `color-mix()`
color-mix() blends colors. State the interpolation space when predictable perceptual results matter.
.card {
background: color-mix(in oklch, var(--brand) 15%, white);
}
.badge {
color: color-mix(in oklch, var(--brand), black 20%);
}The first rule creates a light tint. Percentages describe each color's contribution; omitted percentages receive the remaining amount.
Exercise
Which OKLCH component should you change to make a button darker while preserving its hue and vividness?
Show answer
Lower the lightness component and keep chroma and hue unchanged.