From: Michael M Slusarz Date: Tue, 20 Apr 2010 20:31:09 +0000 (-0600) Subject: Handle color strings other than #xxxxxx X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=106959a50b1aadb6b12e4cafe8cc58bed54fa900;p=horde.git Handle color strings other than #xxxxxx --- diff --git a/framework/Image/lib/Horde/Image.php b/framework/Image/lib/Horde/Image.php index d5e53fb7b..449606c2d 100644 --- a/framework/Image/lib/Horde/Image.php +++ b/framework/Image/lib/Horde/Image.php @@ -1,7 +1,7 @@ * @author Michael J. Rubinsky - * - * @package Horde_Image + * @package Image */ class Horde_Image { - static protected $_loadedEffects = array(); /** * Calculate a lighter (or darker) version of a color. * - * @static - * * @param string $color An HTML color, e.g.: #ffffcc. * @param string $factor TODO * @@ -29,13 +25,11 @@ class Horde_Image */ static public function modifyColor($color, $factor = 0x11) { - $r = hexdec(substr($color, 1, 2)) + $factor; - $g = hexdec(substr($color, 3, 2)) + $factor; - $b = hexdec(substr($color, 5, 2)) + $factor; + list($r, $g, $b) = self::_getColor($color); - $r = min(max($r, 0), 255); - $g = min(max($g, 0), 255); - $b = min(max($b, 0), 255); + $r = min(max($r + $factor, 0), 255); + $g = min(max($g + $factor, 0), 255); + $b = min(max($b + $factor, 0), 255); return '#' . 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); } @@ -43,8 +37,6 @@ class Horde_Image /** * Calculate a more intense version of a color. * - * @static - * * @param string $color An HTML color, e.g.: #ffffcc. * @param string $factor TODO * @@ -52,9 +44,7 @@ class Horde_Image */ static public function moreIntenseColor($color, $factor = 0x11) { - $r = hexdec(substr($color, 1, 2)); - $g = hexdec(substr($color, 3, 2)); - $b = hexdec(substr($color, 5, 2)); + list($r, $g, $b) = self::_getColor($color); if ($r >= $g && $r >= $b) { $g = $g / $r; @@ -89,22 +79,44 @@ class Horde_Image /** * Returns the brightness of a color. * - * @static - * * @param string $color An HTML color, e.g.: #ffffcc. * * @return integer The brightness on a scale of 0 to 255. */ static public function brightness($color) { - $r = hexdec(substr($color, 1, 2)); - $g = hexdec(substr($color, 3, 2)); - $b = hexdec(substr($color, 5, 2)); + list($r, $g, $b) = self::_getColor($color); return round((($r * 299) + ($g * 587) + ($b * 114)) / 1000); } /** + * Normalizes an HTML color. + * + * @param string $color An HTML color, e.g.: #ffffcc or #ffc. + * + * @return array Array with three elements: red, green, and blue. + */ + static public function _getColor($color) + { + if ($color[0] == '#') { + $color = substr($color, 1); + } + + if (strlen($color) == 3) { + $color = str_repeat($color[0], 2) . + str_repeat($color[1], 2) . + str_repeat($color[2], 2); + } + + return array( + hexdec(substr($color, 0, 2)), + hexdec(substr($color, 2, 2)), + hexdec(substr($color, 4, 2)) + ); + } + + /** * Get the RGB value for a given colorname. * * @param string $colorname The colorname