]> _ Git - cubist_util.git/commitdiff
wip #5902
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 2 May 2023 09:13:50 +0000 (11:13 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 2 May 2023 09:13:50 +0000 (11:13 +0200)
src/Graphics/Color.php

index 131bbedc2b2af1eab97ccfefa0dd5bb95e6dfe68..31fe8bd562ab555900dc1d70e36532714b219369 100644 (file)
@@ -176,18 +176,32 @@ class Color
         return 1 - $diff;
     }
 
+    /**
+     * @param $c string
+     * @return string
+     */
     public function toHexComposante($c)
     {
         $res = dechex(self::_to255($this->{'_' . $c}));
-        if (strlen($res) == 2) {
+        $len = strlen($res);
+        if ($len == 2) {
             return $res;
-        } elseif (strlen($res) == 0) {
+        } elseif ($len == 0) {
             return '00';
-        } elseif (strlen($res) == 1) {
+        } elseif ($len == 1) {
             return '0' . $res;
         }
     }
 
+    /**
+     * @param $c string
+     * @return int
+     */
+    public function toIntComposante($c)
+    {
+        return self::_to255($this->{'_' . $c});
+    }
+
     public function toArray()
     {
         return array(
@@ -250,4 +264,16 @@ class Color
         }
         return ['red' => $r, 'green' => $g, 'blue' => $b,];
     }
+
+    /**
+     * @param Color $color
+     * @return int
+     */
+    public function distance(Color $color)
+    {
+
+        return (30 * ($color->toIntComposante('red') - $this->toIntComposante('red'))) ** 2 +
+            (59 * ($color->toIntComposante('green') - $this->toIntComposante('green'))) ** 2 +
+            (11 * ($color->toIntComposante('blue') - $this->toIntComposante('blue'))) ** 2;
+    }
 }