]> _ Git - cubist_util.git/commitdiff
wip #5637 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 9 Dec 2022 12:04:37 +0000 (13:04 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 9 Dec 2022 12:04:37 +0000 (13:04 +0100)
src/Graphics/Color.php

index 4dd77dcefb1b87a05e7d8f29e57875afec4cd918..131bbedc2b2af1eab97ccfefa0dd5bb95e6dfe68 100644 (file)
@@ -95,16 +95,24 @@ class Color
             if (isset($matches[5])) {
                 $this->_alpha = $matches[5];
             }
+        } else if (preg_match('/hsv\(\s*(\d+)\s*,\s*(\d+)%?\s*,\s*(\d+)%?\s*\)/', $input, $matches)) {
+            $this->_processComposantes(self::HSVtoRGB($matches[1], $matches[2], $matches[3]));
         } else {
-            if (strlen($input) == 6) {
-                $s = 0;
-            } elseif (strlen($input) >= 8) {
-                $s = 2;
-                $c['alpha'] = substr($input, 0, 2);
+            if (strlen($input) == 3) {
+                $c['red'] = str_repeat(substr($input, 0, 1), 2);
+                $c['green'] = str_repeat(substr($input, 1, 1), 2);
+                $c['blue'] = str_repeat(substr($input, 2, 1), 2);
+            } else {
+                if (strlen($input) == 6) {
+                    $s = 0;
+                } elseif (strlen($input) >= 8) {
+                    $s = 2;
+                    $c['alpha'] = substr($input, 0, 2);
+                }
+                $c['red'] = substr($input, $s + 0, 2);
+                $c['green'] = substr($input, $s + 2, 2);
+                $c['blue'] = substr($input, $s + 4, 2);
             }
-            $c['red'] = substr($input, $s + 0, 2);
-            $c['green'] = substr($input, $s + 2, 2);
-            $c['blue'] = substr($input, $s + 4, 2);
             $this->_processComposantes($c);
         }
 
@@ -206,4 +214,40 @@ class Color
         return round($c * self::NORM);
     }
 
+
+    public static function HSVtoRGB($h, $s, $v)
+    {
+        //1
+        $h *= 6;
+        //2
+        $i = floor($h);
+        $f = $h - $i;
+        //3
+        $m = $v * (1 - $s);
+        $n = $v * (1 - $s * $f);
+        $k = $v * (1 - $s * (1 - $f));
+        //4
+        switch ($i) {
+            case 0:
+                list($r, $g, $b) = array($v, $k, $m);
+                break;
+            case 1:
+                list($r, $g, $b) = array($n, $v, $m);
+                break;
+            case 2:
+                list($r, $g, $b) = array($m, $v, $k);
+                break;
+            case 3:
+                list($r, $g, $b) = array($m, $n, $v);
+                break;
+            case 4:
+                list($r, $g, $b) = array($k, $m, $v);
+                break;
+            case 5:
+            case 6: //for when $h=1 is given
+                list($r, $g, $b) = array($v, $m, $n);
+                break;
+        }
+        return ['red' => $r, 'green' => $g, 'blue' => $b,];
+    }
 }