1) $h /= 360; $i = floor($h * 6); $f = $h * 6 - $i; $p = $v * (1 - $s); $q = $v * (1 - $f * $s); $t = $v * (1 - (1 - $f) * $s); switch($i % 6) { case 0: $r = $v; $g = $t; $b = $p; break; case 1: $r = $q; $g = $v; $b = $p; break; case 2: $r = $p; $g = $v; $b = $t; break; case 3: $r = $p; $g = $q; $b = $v; break; case 4: $r = $t; $g = $p; $b = $v; break; case 5: $r = $v; $g = $p; $b = $q; break; } $r = round($r * 255); $g = round($g * 255); $b = round($b * 255); return array( 'r' => $r, 'g' => $g, 'b' => $b, 'hex'=> '#' . str_pad(dechex($r), 2, '0', STR_PAD_LEFT) . str_pad(dechex($g), 2, '0', STR_PAD_LEFT) . str_pad(dechex($b), 2, '0', STR_PAD_LEFT), ); } static public function rgbHexToDec($hex) { if (strlen($hex) == 4) { $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3]; } elseif (strlen($hex) == 7) { $hex = substr($hex, 1); } elseif (strlen($hex) == 3) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } elseif (strlen($hex) != 6) { throw new Exception("Invalid string length for RGB value '$hex'"); } $b = hexdec(substr($hex, 4, 2)); $g = hexdec(substr($hex, 2, 2)); $r = hexdec(substr($hex, 0, 2)); return array($r, $g, $b); } /** * Converts an RGB color value to HSV. Conversion formula * adapted from http://en.wikipedia.org/wiki/HSV_color_space. * Assumes r, g, and b are contained in the set [0, 255] and * returns h, s, and v in the set [0, 1]. * * @param Number r The red color value * @param Number g The green color value * @param Number b The blue color value * @return Array The HSV representation */ static public function rgbToHsv($r, $g = null, $b = null) { if (is_string($r) && is_null($g) && is_null($b)) { list($r, $g, $b) = self::rgbHexToDec($r); } $r /= 255; $g /= 255; $b /= 255; $max = max($r, $g, $b); $min = min($r, $g, $b); $h = $s = $v = $max; $d = $max - $min; $s = ($max == 0) ? 0 : $d / $max; if($max == $min) { $h = 0; // achromatic } else { switch($max) { case $r: $h = ($g - $b) / $d + ($g < $b ? 6 : 0); break; case $g: $h = ($b - $r) / $d + 2; break; case $b: $h = ($r - $g) / $d + 4; break; } $h /= 6; } return array($h * 360, $s, $v); } static public function listFonts() { return array( 'Sans serif'=> 'Arial, Helvetica, sans-serif', 'Comics' => '\'Comic Sans MS\', cursive', 'Courier' => '\'Courier New\', Courier, monospace', 'Serif' => '\'Times New Roman\', Times, serif', 'Georgia' => 'Georgia, serif', //'Trebuchet' => '\'Trebuchet MS\', Helvetica, sans-serif', ); } } ?>