From: Vincent Vanwaelscappel Date: Fri, 1 Dec 2023 16:53:58 +0000 (+0100) Subject: wip #6542 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=f1bcacb0b0a3b34a12c39d1789afa0b730df0ff7;p=cubist_util.git wip #6542 --- diff --git a/src/Graphics/Color.php b/src/Graphics/Color.php index b06b148..efa1836 100644 --- a/src/Graphics/Color.php +++ b/src/Graphics/Color.php @@ -300,4 +300,23 @@ class Color (59 * ($color->toIntComposante('green') - $this->toIntComposante('green'))) ** 2 + (11 * ($color->toIntComposante('blue') - $this->toIntComposante('blue'))) ** 2; } + + /** + * @param $colors Color[] + * @return Color + */ + public function closestColor($colors, $reverse = false) + { + $distances = []; + foreach ($colors as $k => $color) { + $distances[$k] = $this->distance($color); + } + asort($distances); + if ($reverse) { + $distances = array_reverse($distances, true); + } + $keys = array_keys($distances); + $k = array_shift($keys); + return $colors[$k]; + } }