Before
Translucent text must be measured after compositing.
alpha 0.503.98:1AA failFind a translucent foreground that reaches the target ratio, then verify the representation you will store or send.
Before
Translucent text must be measured after compositing.
alpha 0.503.98:1AA failSerialized result
Translucent text must be measured after compositing.
#0000008a4.61:1AA passSolving for 4.6:1 returns alpha 0.541176470588. Eight-bit alpha becomes #0000008a, whose composited ratio is 4.607518093747374:1.
The solver searches continuous alpha. The hexadecimal handoff quantizes that value, so the final check belongs after serialization.
<?php
use PhpColor\Color\Color;
use PhpColor\Color\Contrast\ContrastSolver;
$foreground = Color::parse('#000000');
$background = Color::parse('#ffffff');
$alpha = ContrastSolver::requiredAlpha(
$foreground,
$background,
4.6,
);
$hex = $foreground->withAlpha($alpha)->toHex(true);
$stored = Color::parse($hex);
$ratio = ContrastSolver::compositedRatio($stored, $background);
// #0000008a
// 4.607518093747374
use PhpColor\Color\Color;
use PhpColor\Color\Contrast\ContrastSolver;
$foreground = Color::parse('#000000');
$background = Color::parse('#ffffff');
$alpha = ContrastSolver::requiredAlpha(
$foreground,
$background,
4.6,
);
$hex = $foreground->withAlpha($alpha)->toHex(true);
$stored = Color::parse($hex);
$ratio = ContrastSolver::compositedRatio($stored, $background);
// #0000008a
// 4.607518093747374
When the fully opaque foreground remains below the target, requiredAlpha() returns 1.0. The return value does not distinguish that unsolved case, so verify the resulting ratio.