Repair palette contrast

Adjust only the entries that fail against a declared background and threshold.

Before primary secondary strong
After primary secondary strong
primary 4.52:1
secondary 4.76:1
strong 17.74:1

Change the failing entry and retain the passing ones

Only primary fails the requested 4.5:1 ratio on white. The fixer darkens it to #2b72e5 and keeps the other named entries unchanged.

Repair one declared use case

Pass the background and minimum ratio explicitly. Serialize and recheck the resulting colors before treating them as accepted tokens.

<?php

use PhpColor\Color\Color;
use PhpColor\Color\Palette\ColorPalette;
use PhpColor\Color\Palette\Fixer\ContrastFixer;

$palette = ColorPalette::parse([
    'primary' => '#3b82f6',
    'secondary' => '#64748b',
    'strong' => '#111827',
]);

$fixed = (new ContrastFixer())->fix($palette, [
    'background_color' => '#ffffff',
    'min_contrast' => 4.5,
]);

foreach ($fixed->toHex() as $name => $hex) {
    printf("%s %s %.13f:1\n",
        $name,
        $hex,
        Color::contrast($hex, '#ffffff'),
    );
}

// primary #2b72e5 4.5248163639632:1
// secondary #64748b 4.7588427878687:1
// strong #111827 17.7397170040741:1
use PhpColor\Color\Color; use PhpColor\Color\Palette\ColorPalette; use PhpColor\Color\Palette\Fixer\ContrastFixer; $palette = ColorPalette::parse([ 'primary' => '#3b82f6', 'secondary' => '#64748b', 'strong' => '#111827', ]); $fixed = (new ContrastFixer())->fix($palette, [ 'background_color' => '#ffffff', 'min_contrast' => 4.5, ]); foreach ($fixed->toHex() as $name => $hex) { printf("%s %s %.13f:1\n", $name, $hex, Color::contrast($hex, '#ffffff'), ); } // primary #2b72e5 4.5248163639632:1 // secondary #64748b 4.7588427878687:1 // strong #111827 17.7397170040741:1

Verify the palette after serialization

ContrastFixer returns adjusted colors but no batch success status. Check every final representation against the same background and threshold.

NamesNamed keys and their order are retained.
Passing colorsEntries already meeting the ratio remain unchanged.
AdjustmentThe fixer steps through OKLCH lightness.
VerificationReparse serialized colors and calculate their ratios again.

Choose a next step

Repair the collection, then verify every stored color