Check contrast after compositing

Measure the visible contrast of translucent text after it is composited onto its background.

Do not calculate contrast from the stored foreground channels alone when alpha is below 1:

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

$foreground = Color::parse('#000000')->withAlpha(0.54);
$background = Color::parse('#ffffff');

$visible = ContrastSolver::composite($foreground, $background);
$ratio = ContrastSolver::compositedRatio($foreground, $background);

assert('#757575' === $visible->toHex());
assert($ratio >= 4.58);
use PhpColor\Color\Color; use PhpColor\Color\Contrast\ContrastSolver; $foreground = Color::parse('#000000')->withAlpha(0.54); $background = Color::parse('#ffffff'); $visible = ContrastSolver::composite($foreground, $background); $ratio = ContrastSolver::compositedRatio($foreground, $background); assert('#757575' === $visible->toHex()); assert($ratio >= 4.58);

The ratio applies to this one foreground-background pair. If another translucent layer sits between them, composite the layers in display order first.

See color compositing or ContrastSolver.