/**
  * 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;
 }
Пример #2
0
 public function testRgb2hsl()
 {
     $this->assertEquals(array(0, 0, 100), ColorUtil::rgb2hsl(255, 255, 255), "Error converting rgb white into hsl");
     $this->assertEquals(array(0, 0, 0), ColorUtil::rgb2hsl(0, 0, 0), "Error converting rgb black into hsl");
     $this->assertEquals(array(120, 100, 50), ColorUtil::rgb2hsl(0, 255, 0), "Error converting rgb green into hsl");
     $this->assertEquals(array(240, 100, 50), ColorUtil::rgb2hsl(0, 0, 255), "Error converting rgb blue into hsl");
 }