Find the nearest palette color
Choose the existing entry with the shortest implemented Oklab distance to a target.
Candidates
ink
muted
accent
Target
#6d45c4
closest
accent
target #6d45c4
closest #7c3aed
Reuse the nearest existing token
For this target, closest() selects the accent entry from the palette rather than creating a new color.
Search the palette in Oklab
The current implementation converts the target and candidates to Oklab and compares straight-line distance between their three channels.
<?php
use PhpColor\Color\Color;
use PhpColor\Color\Palette\ColorPalette;
$palette = ColorPalette::parse([
'ink' => '#111827',
'muted' => '#64748b',
'accent' => '#7c3aed',
]);
$closest = $palette->closest(Color::parse('#6d45c4'));
echo $closest->toHex();
// #7c3aed
use PhpColor\Color\Color;
use PhpColor\Color\Palette\ColorPalette;
$palette = ColorPalette::parse([
'ink' => '#111827',
'muted' => '#64748b',
'accent' => '#7c3aed',
]);
$closest = $palette->closest(Color::parse('#6d45c4'));
echo $closest->toHex();
// #7c3aed
Name the metric you actually use
This selection does not call Color::distance() or CIEDE2000. It uses the metric implemented by ColorPalette::closest().
CandidatesThe method searches existing palette values.
MetricCurrent behavior is Euclidean distance in Oklab.
TiesThe first candidate at the minimum distance wins.
Empty inputAn empty palette cannot produce a closest color.