/**
  * Generate a gradient image
  * 
  * @param int $width The new image width in pixels
  * @param int $height The new image height in pixels
  * @param int $start The start of the gradient in pixels
  * @param Color $src_color The start image color
  * @param Color $dest_color The end image color
  * 
  * @return \imagemanipulation\ImageResource
  */
 public static function gradient($width, $height, $start, Color $src_color, Color $dest_color)
 {
     Args::int($width, 'width')->required()->min(1);
     Args::int($height, 'height')->required()->min(1);
     $res = self::create($width, $height, $src_color);
     $img = $res->getResource();
     $srcA = $src_color->getAlpha();
     $srcR = $src_color->getRed();
     $srcG = $src_color->getGreen();
     $srcB = $src_color->getBlue();
     $destA = $dest_color->getAlpha();
     $destR = $dest_color->getRed();
     $destG = $dest_color->getGreen();
     $destB = $dest_color->getBlue();
     $incA = ($destA - $srcA) / ($width - $start);
     $incR = ($destR - $srcR) / ($width - $start);
     $incG = ($destG - $srcG) / ($width - $start);
     $incB = ($destB - $srcB) / ($width - $start);
     for ($i = $start; $i < $width; $i++) {
         $srcA += $incA;
         $srcB += $incB;
         $srcG += $incG;
         $srcR += $incR;
         imagefilledrectangle($img, $i, 0, $i, $height, imagecolorallocatealpha($img, $srcR, $srcG, $srcB, $srcA));
     }
     return $res;
 }
 private function checkColorDDD(Color $aColor)
 {
     $this->assertEquals(221, $aColor->getRed(), 'Checking red');
     $this->assertEquals(221, $aColor->getGreen(), 'Checking green');
     $this->assertEquals(221, $aColor->getBlue(), 'Checking blue');
     $this->assertEquals(127, $aColor->getAlpha(), 'Checking alpha');
     $this->assertEquals('dddddd', $aColor->getHexColor(), 'Checking hex color code');
     $this->assertEquals(2145246685, $aColor->getColorIndex(), 'Checking color index');
 }
 /**
  * Applies the filter to the resource
  *
  * @param ImageResource $aResource
  */
 public function applyFilter(ImageResource $aResource)
 {
     $width = $aResource->getX();
     $height = $aResource->getY();
     $blockImg = imagecreate(1, 1);
     imagecolorallocatealpha($blockImg, $this->blockColor->getRed(), $this->blockColor->getGreen(), $this->blockColor->getBlue(), $this->blockColor->getAlpha());
     for ($i = 0; $i <= $this->nrOfBlocks; $i++) {
         $xPos = rand(0, $width - $this->blockSize - 1);
         $yPos = rand(0, $height - $this->blockSize - 1);
         imagecopy($aResource->getResource(), $blockImg, $xPos, $yPos, $xPos, $yPos, $this->blockSize, $this->blockSize);
     }
 }
 /**
  * Applies the filter to the image resource
  *
  * @param ImageResource $aResource
  */
 public function applyFilter(ImageResource $aResource)
 {
     if ($this->angle == 0 || $this->angle == 360) {
         return;
     }
     imageantialias($aResource->getResource(), true);
     $aResource->setResource(imagerotate($aResource->getResource(), $this->angle, $this->bgColor->getColorIndex(), $this->bgColor->getAlpha()));
     $new_imgres = imagecreatetruecolor($aResource->getX(), $aResource->getY());
     $success = imagecopy($new_imgres, $aResource->getResource(), 0, 0, 0, 0, $aResource->getX(), $aResource->getY());
     if (!$success) {
         throw new FilterException(self::$filterType);
     }
     imagedestroy($new_imgres);
 }
 /**
  * Applies the filter to the resource
  *
  * @param ImageResource $aResource
  */
 public function applyFilter(ImageResource $aResource)
 {
     $dest = $aResource->getResource();
     if (imageistruecolor($dest)) {
         imagetruecolortopalette($dest, false, 256);
     }
     foreach ($this->search as $search) {
         $searchRgb = new Color($search);
         $index = imagecolorclosest($aResource->getResource(), $searchRgb->getRed(), $searchRgb->getGreen(), $searchRgb->getBlue());
         // get White COlor
         imagecolorset($aResource->getResource(), $index, $this->replace->getRed(), $this->replace->getGreen(), $this->replace->getBlue());
         // SET NEW COLOR
     }
     $aResource->setResource($dest);
 }
 /**
  * Calculates the average color of an image.
  * 
  * @param ImageResource $aResource
  * 
  * @return IColor
  */
 public static function average(ImageResource $aResource)
 {
     $w = $aResource->getX();
     $h = $aResource->getY();
     $r = $g = $b = 0;
     $res = $aResource->getResource();
     for ($y = 0; $y < $h; $y++) {
         for ($x = 0; $x < $w; $x++) {
             $rgb = imagecolorat($res, $x, $y);
             $r += $rgb >> 16;
             $g += $rgb >> 8 & 255;
             $b += $rgb & 255;
         }
     }
     $pxls = $w * $h;
     $r = dechex(round($r / $pxls));
     $g = dechex(round($g / $pxls));
     $b = dechex(round($b / $pxls));
     if (strlen($r) < 2) {
         $r = 0 . $r;
     }
     if (strlen($g) < 2) {
         $g = 0 . $g;
     }
     if (strlen($b) < 2) {
         $b = 0 . $b;
     }
     $index = Color::createColorIndex($r, $g, $b);
     return new Color($index);
 }
 /**
  * asserts that the color and the hex provided are the same
  * @param Color $color
  * @param string $hex
  */
 protected function assertColor(Color $color, $hex)
 {
     $this->assertEquals($color->getHexColor(), $hex, "Colors are not the same... " . $color->getHexColor() . " - " . $hex);
 }
 /**
  * Returns the allocated color
  *
  * @param RGBColor $aColor
  * @return int
  */
 public static function allocateColor($aImgRes, Color $aColor)
 {
     return imagecolorallocatealpha($aImgRes, $aColor->getRed(), $aColor->getGreen(), $aColor->getBlue(), $aColor->getAlpha());
 }