Find the minimum overlay opacity

Calculate the lowest alpha that lets one foreground reach a required contrast ratio over one known background.

Solve alpha against the actual background, then check the composited result. A translucent foreground has no independent contrast ratio.

Solve alpha for one pair

requiredAlpha() searches the alpha range for the requested WCAG ratio:

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

$foreground = Color::black();
$background = Color::parse('#f3f4f6');

$alpha = ContrastSolver::requiredAlpha(
    $foreground,
    $background,
    targetRatio: 4.5,
);

$overlay = $foreground->withAlpha($alpha);
use PhpColor\Color\Color; use PhpColor\Color\Contrast\ContrastSolver; $foreground = Color::black(); $background = Color::parse('#f3f4f6'); $alpha = ContrastSolver::requiredAlpha( $foreground, $background, targetRatio: 4.5, ); $overlay = $foreground->withAlpha($alpha);

Verify what becomes visible

Measure the composited pair, not the stored foreground channels:

$ratio = ContrastSolver::compositedRatio($overlay, $background);

assert($alpha > 0.54 && $alpha < 0.55);
assert($ratio >= 4.5);

echo $overlay->toCss();
$ratio = ContrastSolver::compositedRatio($overlay, $background); assert($alpha > 0.54 && $alpha < 0.55); assert($ratio >= 4.5); echo $overlay->toCss();

If fully opaque foreground cannot reach the target, the method returns 1.0; always verify the ratio. Evaluate every background where the overlay can appear, and review images, gradients, filters, and intermediate layers in the rendered component. Continue with check contrast after compositing, find accessible foregrounds, or work with alpha values.