Composite translucent colors

Resolve foreground alpha against a backdrop before measuring or storing the visible color.

Backdrop opaque #2563eb
Foreground alpha 0.65 #f43f5ea6
Visible result opaque #ac4c8f

Resolve the layer stack into one visible color

The translucent rose foreground over the opaque blue backdrop becomes #ac4c8f with alpha 1.

Composite before evaluating the result

ContrastSolver::composite() accepts the foreground first and the background second, then returns an sRGB color.

<?php

use PhpColor\Color\Color;
use PhpColor\Color\Contrast\ContrastSolver;

$foreground = Color::parse('rgb(244 63 94 / 65%)');
$backdrop = Color::parse('#2563eb');
$visible = ContrastSolver::composite($foreground, $backdrop);

echo $visible->toHex(true);

// #ac4c8fff
use PhpColor\Color\Color; use PhpColor\Color\Contrast\ContrastSolver; $foreground = Color::parse('rgb(244 63 94 / 65%)'); $backdrop = Color::parse('#2563eb'); $visible = ContrastSolver::composite($foreground, $backdrop); echo $visible->toHex(true); // #ac4c8fff

Compositing depends on the backdrop

A translucent color has no single visible RGB result until the background and layer order are known.

OrderThe first argument is foreground; the second is background.
SpaceThe returned object is an sRGB color.
Opaque backdropAn opaque background makes this demonstrated result opaque.
ContrastMeasure contrast against the composited representation viewers receive.

Choose a next step

Resolve the layers before measuring the visible color