Concrete and deferred CSS colors

Decide whether a CSS color can resolve immediately or must retain variables, currentColor, or color-scheme choices until context is available.

Use Color::parse() for self-contained values that can become a color object immediately. Use CssColor when the expression depends on information supplied later by a CSS resolution context.

Ask whether every dependency is present

Expression Resolve now? Entry point
#3b82f6 Yes Color::parse()
oklch(from rgb(255 0 0) l c h / 50%) Yes Color::parse()
var(--brand) Not without a variable value CssColor::parse()
currentColor Not without a current color CssColor::parse()
light-dark(white, black) Not without a color scheme CssColor::parse()

A relative color can still be concrete when its origin and channel expressions are all present:

use PhpColor\Color\Color;

$color = Color::parse('oklch(from rgb(255 0 0) l c h / 50%)');

echo $color::getSpaceName().PHP_EOL;
echo $color->getAlpha().PHP_EOL;
use PhpColor\Color\Color; $color = Color::parse('oklch(from rgb(255 0 0) l c h / 50%)'); echo $color::getSpaceName().PHP_EOL; echo $color->getAlpha().PHP_EOL;

The output is:

oklch
0.5
oklch 0.5

No external variable or inherited value is needed, so the parser returns a concrete OklchColor.

Preserve contextual input for the CSS workflow

CssColor::parse() preserves expressions such as var(--brand, #3b82f6) rather than forcing them into a concrete color object. A CssContext can later supply variables, the current foreground color, and a light or dark color scheme.

The fallback inside var() matters: it can produce a concrete color when the variable is absent. An unresolved variable without a fallback may remain a CssResolvableInterface in a non-strict context. Code that needs channels, conversion, or contrast must first obtain a concrete ColorInterface.

PHPColor does not reproduce the browser cascade, inheritance, selector matching, or computed-style processing. Construction and string parsing are also separate capabilities: do not assume every expression created through a builder can be parsed back from text.

The Modern CSS color workflows section owns the procedures for variables, currentColor, light-dark(), relative colors, and color-mix(). Continue with Choose a normalization boundary once the result is concrete.

Continue