/** * Applies the sepia filter to an image resource * * @param ImageResource $aResource */ public function applyFilter(ImageResource $aResource) { if ($this->degrees === 0) { return; } $width = $aResource->getX(); $height = $aResource->getY(); // cache calculated colors in a map... $colorMap = array(); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $color = ColorUtil::getColorAt($aResource, Coordinate::create($x, $y)); if (!isset($colorMap[$color->getColorIndex()])) { // calculate the new color $hsl = ColorUtil::rgb2hsl($color->getRed(), $color->getGreen(), $color->getBlue()); $hsl[0] += $this->degrees; $rgb = ColorUtil::hsl2rgb($hsl[0], $hsl[1], $hsl[2]); $newcol = imagecolorallocate($aResource->getResource(), $rgb[0], $rgb[1], $rgb[2]); $colorMap[$color->getColorIndex()] = $newcol; } else { $newcol = $colorMap[$color->getColorIndex()]; } imagesetpixel($aResource->getResource(), $x, $y, $newcol); } } $colorMap = null; }
public function testhsl2Rgb() { $this->assertEquals(array(255, 255, 255), ColorUtil::hsl2rgb(0, 0, 100), "Error converting hsl into rgb white "); $this->assertEquals(array(0, 0, 0), ColorUtil::hsl2rgb(0, 0, 0), "Error converting hsl into rgb black into hsl"); $this->assertEquals(array(0, 255, 0), ColorUtil::hsl2rgb(120, 100, 50), "Error converting hsl into rgb green"); $this->assertEquals(array(0, 0, 255), ColorUtil::hsl2rgb(240, 100, 50), "Error converting hsl into rgb blue"); }