/** * 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 testCreate() { $width = 250; $height = 750; $color = 'ff00ff'; $res = ImageGenerator::create($width, $height, new Color($color)); $this->assertEquals($width, $res->getX()); $this->assertEquals($height, $res->getY()); $this->assertEquals($color, ColorUtil::getColorAt($res, Coordinate::create(0, 0))->getHexColor()); $this->assertEquals($color, ColorUtil::getColorAt($res, Coordinate::create($width / 2, $height / 2))->getHexColor()); $this->assertEquals($color, ColorUtil::getColorAt($res, Coordinate::create($width - 1, $height - 1))->getHexColor()); }
public function testGradient() { $width = 250; $height = 750; $startColor = 'ffffff'; $endColor = '000000'; $res = ImageGenerator::gradient($width, $height, 1, new Color($startColor), new Color($endColor)); $this->assertEquals($width, $res->getWidth()); $this->assertEquals($height, $res->getHeight()); $this->assertEquals($startColor, ColorUtil::getColorAt($res, Coordinate::create(0, 0))->getHexColor()); $this->assertEquals('7e7e7e', ColorUtil::getColorAt($res, Coordinate::create($width / 2, $height / 2))->getHexColor()); $this->assertEquals($endColor, ColorUtil::getColorAt($res, Coordinate::create($width - 1, $height - 1))->getHexColor()); }
public function testFilter() { $image = $this->getOriginalImage(ImageType::PNG); $filter = new ImageFilterGrayScale(); $rasterizer = new Rasterizer($image, new GridRasterStrategy(25, 25, true)); $segments = $rasterizer->getSegments(); $rasterizer->filter($filter, $segments->offsetGet(0)); $rasterizer->filter($filter, $segments->offsetGet(5)); $originalColor = "ff0000"; $filteredColor = "4c4c4c"; $color = ColorUtil::getColorAt($rasterizer->getResource(), Coordinate::create(50, 50)); $this->assertEquals($filteredColor, $color->getHexColor()); $color = ColorUtil::getColorAt($rasterizer->getResource(), Coordinate::create(160, 50)); $this->assertEquals($originalColor, $color->getHexColor()); $color = ColorUtil::getColorAt($rasterizer->getResource(), Coordinate::create(50, 160)); $this->assertEquals($originalColor, $color->getHexColor()); $color = ColorUtil::getColorAt($rasterizer->getResource(), Coordinate::create(160, 160)); $this->assertEquals($filteredColor, $color->getHexColor()); }
/** * Returns the color at a coordinate of the image * * @see RGBColor::getColorAt (ImageResource, Coordinate) * * @param Coordinate aCoordinatel * @return \imagemanipulation\color\Color */ public function getColorAt(Coordinate $aCoordinate) { return ColorUtil::getColorAt($this, $aCoordinate); }
public function testRgba2Int() { $this->assertEquals('285212671', ColorUtil::rgba2int(255, 255, 255, 16)); }