Find the nearest palette token

Map an arbitrary color to the closest value already available in a controlled palette.

Use closest() when imported colors must resolve to an existing token value:

use PhpColor\Color\Color;
use PhpColor\Color\Palette\ColorPalette;

$palette = ColorPalette::parse([
    'brand' => '#7c3aed',
    'info' => '#0ea5e9',
    'warning' => '#f59e0b',
]);

$target = Color::parse('#6d45c4');
$closest = $palette->closest($target);

assert('#7c3aed' === $closest->toHex());
use PhpColor\Color\Color; use PhpColor\Color\Palette\ColorPalette; $palette = ColorPalette::parse([ 'brand' => '#7c3aed', 'info' => '#0ea5e9', 'warning' => '#f59e0b', ]); $target = Color::parse('#6d45c4'); $closest = $palette->closest($target); assert('#7c3aed' === $closest->toHex());

The method returns a color from the palette; it does not create a replacement. Selection uses Euclidean distance in Oklab, not CIEDE2000.

See nearest palette colors or ColorPalette.