Resolve light and dark branches

Use one deferred expression and an explicit scheme to select its concrete color.

Expression light-dark(var(--surface), rgb(11 12 14))
Schemelight--surface: #f8fafc
Resolved#f8fafcSrgbColor
Schemedark
Resolved#0b0c0eSrgbColor

Choose one branch with explicit context

The expression stays the same. A light context selects #f8fafc; a dark context selects #0b0c0e. Without a scheme, the expression remains deferred.

Supply the active scheme

Each branch may be a concrete color or another resolvable expression. Here the light branch reads --surface from the context.

<?php

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

$expression = CssColor::lightDark(
    CssColor::var('--surface'),
    '#0b0c0e',
);

$light = CssContext::light(['--surface' => '#f8fafc']);
$dark = CssContext::dark(['--surface' => '#f8fafc']);

echo CssColor::resolve($expression, $light)->toHex(); // #f8fafc
echo CssColor::resolve($expression, $dark)->toHex();  // #0b0c0e
use PhpColor\Color\Css\CssColor; use PhpColor\Color\Css\CssContext; $expression = CssColor::lightDark( CssColor::var('--surface'), '#0b0c0e', ); $light = CssContext::light(['--surface' => '#f8fafc']); $dark = CssContext::dark(['--surface' => '#f8fafc']); echo CssColor::resolve($expression, $light)->toHex(); // #f8fafc echo CssColor::resolve($expression, $dark)->toHex(); // #0b0c0e

Branch selection is not theme generation

light-dark() selects between two supplied expressions. It does not derive a palette or decide which scheme is active.

The application supplies the scheme

PHPColor does not inspect browser preferences, evaluate media queries, or perform browser support checks.

An unknown scheme stays deferred

PHPColor resolves what it can inside both branches and retains light-dark(rgb(248 250 252), rgb(11 12 14)), even in strict mode.

Resolve both branches

Supply the scheme and inspect the selected color