Resolve CSS color variables

Resolve a color custom property from an explicit context map or use its declared fallback.

Aliasvar(--alias, #111827)
Lookup--alias: var(--brand)
--brand: #806eae
SrgbColor#806eae
Missing keyvar(--missing, #334155)
Lookup--missing: absent
SrgbColor#334155

Follow an alias or use its fallback

Without a value or fallback, non-strict resolution retains var(--missing).

Resolve from an explicit context map

PHPColor follows resolvable aliases until it reaches a concrete color. When the requested key is absent, it resolves the declared fallback instead.

<?php

use PhpColor\Color\Css\CssColor;
use PhpColor\Color\Css\CssContext;

$context = CssContext::light([
    '--brand' => '#806eae',
    '--alias' => 'var(--brand)',
]);
$alias = CssColor::var('--alias', '#111827');
$fallback = CssColor::var('--missing', '#334155');

echo CssColor::resolve($alias, $context)->toHex();    // #806eae
echo CssColor::resolve($fallback, $context)->toHex(); // #334155
use PhpColor\Color\Css\CssColor; use PhpColor\Color\Css\CssContext; $context = CssContext::light([ '--brand' => '#806eae', '--alias' => 'var(--brand)', ]); $alias = CssColor::var('--alias', '#111827'); $fallback = CssColor::var('--missing', '#334155'); echo CssColor::resolve($alias, $context)->toHex(); // #806eae echo CssColor::resolve($fallback, $context)->toHex(); // #334155

A context map is not the cascade

PHPColor resolves only the custom-property values supplied by the application. It does not discover declarations or calculate which declaration wins.

Variable keys include their leading --. Invalid values fail when PHPColor attempts to parse them.

Missing variables without fallbacks remain deferred in non-strict mode and throw InvalidColorException in strict mode.

PHPColor does not parse arbitrary token streams, stylesheets, selectors, or inheritance.

Choose a next step

Resolve the color values your application supplies