Ship CSS

Emit modern CSS with an sRGB fallback, derive a dark theme, and build contextual expressions for the browser.

The tour ends where most color work ends: in a stylesheet. PHPColor emits modern CSS alongside an sRGB hexadecimal fallback, derives a dark value from a light one, and builds expressions the browser can resolve in context.

Modern output, sRGB fallback

Every color can be formatted in the native syntax of its current space and as an eight-bit sRGB hexadecimal value. A design token can expose both representations:

use PhpColor\Color\Color;

$brand = Color::parse('#3b82f6')->to('oklch');

echo '--brand: '.$brand->toHex().';';
// --brand: #3b82f6;

echo '--brand-modern: '.$brand->toCss().';';
// --brand-modern: oklch(0.623083 0.188015 259.815);
use PhpColor\Color\Color; $brand = Color::parse('#3b82f6')->to('oklch'); echo '--brand: '.$brand->toHex().';'; // --brand: #3b82f6; echo '--brand-modern: '.$brand->toCss().';'; // --brand-modern: oklch(0.623083 0.188015 259.815);

Dark from light

A dark theme is one mix away: pull the light value toward black in perceptual space, then let CSS pick the right one with light-dark():

use PhpColor\Color\Css\CssColor;

$light = Color::parse('#3b82f6');
$dark  = Color::mix($light, 'black', 0.55)->toSrgb();
// #0d2752

echo CssColor::lightDark($light, $dark)->toCss();
// light-dark(rgb(59 130 246), rgb(13 39 82))
use PhpColor\Color\Css\CssColor; $light = Color::parse('#3b82f6'); $dark = Color::mix($light, 'black', 0.55)->toSrgb(); // #0d2752 echo CssColor::lightDark($light, $dark)->toCss(); // light-dark(rgb(59 130 246), rgb(13 39 82))

$light
$dark

Defer to the browser

Some color math belongs at render time, when custom properties and user preferences are known. CssColor builds expressions the browser resolves:

echo CssColor::mix('oklab', '#3b82f6', '#f43f5e', 0.5)->toCss();
// color-mix(in oklab, rgb(59 130 246) 50%, rgb(244 63 94))

echo CssColor::relative('oklch', '#3b82f6', ['l' => 'calc(l - 0.2)'])->toCss();
// oklch(from rgb(59 130 246) calc(l - 0.2))
echo CssColor::mix('oklab', '#3b82f6', '#f43f5e', 0.5)->toCss(); // color-mix(in oklab, rgb(59 130 246) 50%, rgb(244 63 94)) echo CssColor::relative('oklch', '#3b82f6', ['l' => 'calc(l - 0.2)'])->toCss(); // oklch(from rgb(59 130 246) calc(l - 0.2))

The same objects, one more trick: instead of computing a color, they can describe one and let the cascade finish the job.

Go deeper: Modern CSS color workflows covers var() with fallbacks, currentColor, and resolving deferred expressions server-side.

Your colors speak fluent CSS. One last stop.