/** * Convert HSV (Hue, Saturation, Brightness) to a hex color string. * * Originally written by Jurgen Schwietering. Integrated into the class by * Jason Lotito. * * @param integer $h Hue * @param integer $s Saturation * @param integer $v Brightness * @return string The hex string. * @access public * @static * @author Jurgen Schwietering <*****@*****.**> * @uses rgb2hex() to convert the return value to a hex string. */ function hsv2hex($h, $s, $v) { $s /= 256.0; $v /= 256.0; if ($s == 0.0) { $r = $g = $b = $v; return ''; } else { $h = $h / 256.0 * 6.0; $i = floor($h); $f = $h - $i; $v *= 256.0; $p = (int) ($v * (1.0 - $s)); $q = (int) ($v * (1.0 - $s * $f)); $t = (int) ($v * (1.0 - $s * (1.0 - $f))); switch ($i) { 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; default: $r = $v; $g = $p; $b = $q; break; } } return Image_Color::rgb2hex(array($r, $g, $b)); }
function testRgb2hex() { $result = Image_Color::rgb2hex(array(171, 205, 239)); $this->assertEquals('ABCDEF', $result); }
/** * This is deprecated. Use rgb2hex() instead. * @access private * @deprecated Function deprecated after 1.0.1 * @see rgb2hex(). */ function _returnColor($color) { return Image_Color::rgb2hex($color); }
/** * Apply tools to image. * * This function scan for mask color and closes colors position, grab color * at found the position on sample image, then set the pixel color at the same * position on destination image. * * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure. * @access private * @see Image_Tools_Mask::_getNearestColors() */ function render() { if (!Image_Tools::isGDImageResource($this->_maskImage)) { return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_maskImage'); } if (!Image_Tools::isGDImageResource($this->_sampleImage)) { return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_sampleImage'); } if (!Image_Tools::isGDImageResource($this->resultImage)) { return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage'); } $maskWidth = imagesx($this->_maskImage); $maskHeight = imagesy($this->_maskImage); $sampleWidth = imagesx($this->_sampleImage); $sampleHeight = imagesy($this->_sampleImage); if ($this->options['antialias']) { $closesColors = $this->_getNearestColors(); } else { $closesColors = array($this->options['maskColor']); } imagealphablending($this->resultImage, true); // scan for mask color or closes colors position for ($x = 0; $x < $maskWidth; $x++) { for ($y = 0; $y < $maskHeight; $y++) { if ($x >= $sampleWidth || $y >= $sampleHeight) { continue; } // grab color at x, y and convert to hex color format $index = imagecolorat($this->_maskImage, $x, $y); $maskRGBA = imagecolorsforindex($this->_maskImage, $index); $maskColor = Image_Color::rgb2hex(array_values($maskRGBA)); // check color in closes colors collection if (in_array($maskColor, $closesColors)) { // grab color at x, y from sample image $index = imagecolorat($this->_sampleImage, $x, $y); $sampleRGBA = imagecolorsforindex($this->_sampleImage, $index); // allocate color on destination image $color = imagecolorresolvealpha($this->resultImage, $sampleRGBA['red'], $sampleRGBA['green'], $sampleRGBA['blue'], $sampleRGBA['alpha']); // set a pixel color at destination image imagesetpixel($this->resultImage, $x, $y, $color); } } } return true; }