RGB 并不是衡量两种颜色之间距离的最佳选择,因为它忽略了一些事实,例如,我们将深绿色和浅绿色都视为“绿色”(#000000 和 #7f7f7f 之间的 RGB 距离与 #000000 和 #5443c0 之间的距离相同 - #5443c0是一种稍微变暗的板岩蓝)。(RGB space isn't the best choice for measuring the distance between two colours, since it ignores, for example, the fact that we count both dark green and light green as "green" (the RGB distance between #000000 and #7f7f7f is the same as the distance between #000000 and #5443c0 - a slightly darkened SlateBlue).)
Lab 颜色空间是一种更符合颜色感知方式的颜色空间,它根据颜色的明暗、红/绿和黄/蓝来测量颜色。(还有更好的模型,但它们的计算量更大。)(A better choice of colour space that conforms better to how colours are perceived is the so-called Lab space, which measures colours according to how light/dark, red/green, and yellow/blue they are. (There are still better models, but they come at the expense of increased computation.))
<?php
function warp1($c)
{
if($c > 10.3148)
{
return pow((561 + 40*$c)/10761, 2.4);
}
else
{
return $c / 3294.6;
}
}
function warp2($c)
{
if($c > 0.008856)
{
return pow($c, 1/3);
}
else
{
return 7.787 * $c + 4/29;
}
}
function rgb2lab($rgb)
{
[$red, $green, $blue] = array_map('warp1', $rgb);
$x = warp2($red * 0.4339 + $green * 0.3762 + $blue * 0.1899);
$y = warp2($red * 0.2126 + $green * 0.7152 + $blue * 0.0722);
$z = warp2($red * 0.0178 + $green * 0.1098 + $blue * 0.8730);
$l = 116*$y - 16;
$a = 500 * ($x - $y);
$b = 200 * ($y - $z);
return array_map('intval', [$l, $a, $b]);
}
function generate_palette_from_image($image)
{
$pal = [];
$width = imagesx($image);
$height = imagesy($image);
for($x = 0; $x < $width; ++$x)
{
for($y = 0; $y < $height; ++$y)
{
$pal[] = imagecolorat($image, $x, $y);
}
}
return array_map(function($col)use($image)
{
$rgba = imagecolorsforindex($image, $col);
return [$rgba['red'], $rgba['green'], $rgba['blue']];
}, array_unique($pal));
}
function closest_rgb_in_palette($rgb, $palette)
{
if(($idx = array_search($rgb, $palette)) !== false)
{
return $idx;
}
[$tl, $ta, $tb] = rgb2lab($rgb);
$dists = array_map(function($plab)use($tl, $ta, $tb)
{
[$pl, $pa, $pb] = $plab;
$dl = $pl - $tl;
$da = $pa - $ta;
$db = $pa - $tb;
return $dl * $dl + $da * $da + $db * $db;
}, array_map('rgb2lab', $palette));
return array_search(min($dists), $dists);
}
function closest_rgb_in_image($rgb, $image)
{
$palette = generate_palette_from_image($image);
return $palette[closest_rgb_in_palette($rgb, $palette)];
}
$lena = imagecreatefrompng('lena.png');
$red = closest_rgb_in_image([255,0,0],$lena);
echo join(' ', $red); ?>
如果要将许多颜色匹配到一个调色板,则可能需要预先计算并重用 Lab 调色板,而不是像此处这样每次都重新生成。