Evaluate `color-mix()`

Choose immediate PHP color mixing or a deferred CSS color-mix expression according to when the operands become known.

Use Color::mix() when both operands are concrete now. Use CssColor::mix() when variables, currentColor, or another CSS expression must remain unresolved until context is supplied.

Separate immediate and deferred mixing

Inputs API Result
Two concrete application colors Color::mix() Concrete color immediately
CSS expressions or contextual operands CssColor::mix() Deferred ColorMix expression
Deferred expression plus authoritative context CssColor::resolve() Concrete color when every dependency resolves

Build a deferred mix with an explicit interpolation space:

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

$expression = CssColor::mix(
    'oklab',
    '#2563eb',
    '#ffffff',
    0.75,
    0.25,
);

$color = CssColor::resolve(
    $expression,
    CssContext::light(),
);

echo $color->toHex();
use PhpColor\Color\Css\CssColor; use PhpColor\Color\Css\CssContext; $expression = CssColor::mix( 'oklab', '#2563eb', '#ffffff', 0.75, 0.25, ); $color = CssColor::resolve( $expression, CssContext::light(), ); echo $color->toHex();

The result is #5b8ef4.

CssColor::colorMix() is an alias of mix(). Weights can be fractions such as 0.75 or percentages expressed as numbers such as 75.0. If both are absent, each defaults to 50%. If one is absent, PHPColor uses the complement of the supplied weight. It then normalizes both weights before mixing.

When the normalized sum is not positive, resolution throws InvalidColorException. When either operand remains unresolved, the result remains a ColorMix expression.

Call toCss() when you want the deferred CSS text instead of a concrete color:

use PhpColor\Color\Css\CssColor;

$expression = CssColor::mix(
    'oklab',
    '#2563eb',
    '#ffffff',
    0.75,
    0.25,
);

echo $expression->toCss();
// color-mix(in oklab, rgb(37 99 235) 75%, rgb(255 255 255) 25%)
use PhpColor\Color\Css\CssColor; $expression = CssColor::mix( 'oklab', '#2563eb', '#ffffff', 0.75, 0.25, ); echo $expression->toCss(); // color-mix(in oklab, rgb(37 99 235) 75%, rgb(255 255 255) 25%)

The space is passed to the underlying color mixer. Use a color space supported by PHPColor’s mixing API; invalid spaces fail during resolution.

Keep weight and alpha behavior explicit

Weights describe each operand's contribution and are normalized by their sum. They are not the single endpoint ratio accepted by Color::mix(). When either operand remains unresolved, PHPColor preserves the complete ColorMix expression rather than guessing a concrete value.

PHPColor serializes supported color-mix expressions but is not a browser cascade or feature-support engine. Preserve the expression for browser-owned late binding; resolve it only when the server context is complete.

Continue with Mix and composite colors, Evaluate CSS color-mix, or CssColor.