Preserve light-dark() when the browser owns the active color scheme. Resolve it in PHP when the server already knows which theme it is rendering, such as for an email, PDF, export, or deterministic preview.
Keep branch selection with the theme authority
CssColor::lightDark() stores a light and dark branch without selecting one:
use PhpColor\Color\Css\CssColor;
use PhpColor\Color\Css\CssContext;
$expression = CssColor::lightDark(
'var(--text-light, #111827)',
'var(--text-dark, #f9fafb)',
);
$darkText = CssColor::resolve(
$expression,
CssContext::dark(),
);
echo $expression->toCss().PHP_EOL;
echo $darkText->toHex().PHP_EOL;
use PhpColor\Color\Css\CssColor;
use PhpColor\Color\Css\CssContext;
$expression = CssColor::lightDark(
'var(--text-light, #111827)',
'var(--text-dark, #f9fafb)',
);
$darkText = CssColor::resolve(
$expression,
CssContext::dark(),
);
echo $expression->toCss().PHP_EOL;
echo $darkText->toHex().PHP_EOL;
The output is:
light-dark(var(--text-light, rgb(17 24 39)), var(--text-dark, rgb(249 250 251)))
#f9fafb
light-dark(var(--text-light, rgb(17 24 39)), var(--text-dark, rgb(249 250 251)))
#f9fafb
Preserve branches when the scheme is unknown
In a light context, the first branch resolves. In a dark context, the second resolves. If the context has no color scheme, PHPColor resolves what it can inside both branches and returns a LightDarkColor expression.
CssColor::parse() also recognizes light-dark(...), including nested var() fallbacks:
$expression = CssColor::parse(
'light-dark(var(--fg, #111827), var(--fg-dark, #f9fafb))',
);
$expression = CssColor::parse(
'light-dark(var(--fg, #111827), var(--fg-dark, #f9fafb))',
);
Malformed light-dark() input with missing, extra, or unbalanced arguments throws InvalidArgumentException.
An absent scheme is not automatically an error, even in strict mode: PHPColor resolves what it can inside both branches and preserves the LightDarkColor. The application must decide whether deferred output is acceptable at that boundary.
PHPColor does not inspect media queries or system preferences. Continue with Build resolution contexts, Resolve CSS light-dark, or LightDarkColor.