From: Vincent Vanwaelscappel Date: Fri, 9 Dec 2022 12:04:37 +0000 (+0100) Subject: wip #5637 @0.25 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=b8380231a798e59ca386aaad9dc809287f719a5f;p=cubist_util.git wip #5637 @0.25 --- diff --git a/src/Graphics/Color.php b/src/Graphics/Color.php index 4dd77dc..131bbed 100644 --- a/src/Graphics/Color.php +++ b/src/Graphics/Color.php @@ -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,]; + } }