Modern Color Spaces: OKLCH & color-mix()

Modern Color Spaces: OKLCH & color-mix() in CSS: Traditional web colors used sRGB via HEX ( ), RGB ( ), or HSL ( ).

Traditional web colors used sRGB via HEX (#ff0000), RGB (rgb(255, 0, 0)), or HSL (hsl(0, 100%, 50%)).

However, HSL is perceptually non-uniform: yellow at 50% lightness appears dazzlingly bright to human vision, while blue at 50% lightness appears dark and muddy. Furthermore, standard RGB cannot render the vivid wide-gamut colors supported by modern P3 Retina and OLED displays.


The OKLCH Color Space

OKLCH (Lightness, Chroma, Hue in Oklab space) is the new standard for web color design:

/* oklch(LIGHTNESS CHROMA HUE [/ ALPHA]) */
.button-primary {
  background-color: oklch(0.65 0.22 250);
}
  1. Lightness (L): 0 (pure black) to 1 / 100% (pure white). Perceptually uniform: a lightness of 0.7 has identical perceived brightness across all hues.
  2. Chroma (C): Color saturation/vibrancy. 0 is pure grayscale; values above 0.2 access ultra-vivid Display-P3 gamuts.
  3. Hue (H): Color angle from 0 to 360 degrees on the color wheel.
/* Generating an accessible, harmonized color palette in OKLCH:
   Because lightness is identical, both buttons share identical perceived contrast! */
.blue-badge  { background: oklch(0.7 0.18 250); color: #000; }
.yellow-badge{ background: oklch(0.7 0.18 90);  color: #000; }

The color-mix() Function

color-mix() enables mixing two colors in a specified color space directly in CSS without Sass preprocessors:

:root {
  --brand: oklch(0.6 0.2 250);
}

/* Generate hover states dynamically by mixing 20% white */
.button:hover {
  background-color: color-mix(in oklch, var(--brand), white 20%);
}

/* Generate semi-transparent tinted backgrounds */
.card-highlight {
  background-color: color-mix(in oklch, var(--brand) 12%, transparent);
}

The light-dark() CSS Function

Modern browsers support light-dark(), which automatically toggles between two colors based on the current color scheme without media query nesting:

:root {
  color-scheme: light dark; /* Mandatory: declares support for both modes */
}

.card {
  /* Automatically resolves to white in light mode, dark gray in dark mode! */
  background-color: light-dark(#ffffff, #1e293b);
  color: light-dark(#0f172a, #f8fafc);
}

Exercises

  1. Convert a legacy HEX color palette to OKLCH and verify perceptual uniformity across hues.
  2. Use color-mix() to create disabled, hover, and active states from a single brand token.
  3. Implement the light-dark() function to build a 2-line themeable card component.