]> _ Git - cubist_util.git/commitdiff
wip #6542
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 1 Dec 2023 16:53:58 +0000 (17:53 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 1 Dec 2023 16:53:58 +0000 (17:53 +0100)
src/Graphics/Color.php

index b06b1482b28424ccc354f42084adb0b2a95257a1..efa1836306406c4ca4df7888a354aa8a1f6038c2 100644 (file)
@@ -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];
+    }
 }