/** * {@inheritdoc} */ public final function getColor(PointInterface $position) { $l = $this->getDistance($position); if ($l >= $this->length) { return $this->end; } if ($l < 0) { return $this->start; } $color = new Color(array((int) min(255, min($this->start->getRed(), $this->end->getRed()) + round(abs($this->end->getRed() - $this->start->getRed()) * $l / $this->length)), (int) min(255, min($this->start->getGreen(), $this->end->getGreen()) + round(abs($this->end->getGreen() - $this->start->getGreen()) * $l / $this->length)), (int) min(255, min($this->start->getBlue(), $this->end->getBlue()) + round(abs($this->end->getBlue() - $this->start->getBlue()) * $l / $this->length))), (int) min(100, min($this->start->getAlpha(), $this->end->getAlpha()) + round(abs($this->end->getAlpha() - $this->start->getAlpha()) * $l / $this->length))); return $color; }
/** * {@inheritdoc} */ public function colorize(Color $color) { if (false === imagefilter($this->resource, IMG_FILTER_COLORIZE, $color->getRed(), $color->getGreen(), $color->getBlue())) { throw new RuntimeException('Failed to colorize the image'); } return $this; }
/** * @cover Imagine\Image\Color::__construct * @cover Imagine\Image\Color::getRed * @cover Imagine\Image\Color::getGreen * @cover Imagine\Image\Color::getAlpha * @cover Imagine\Image\Color::__toString * @cover Imagine\Image\Color::isOpaque */ public function testShouldSetColorToWhite() { $color = new Color('fff'); $this->assertEquals(255, $color->getRed()); $this->assertEquals(255, $color->getGreen()); $this->assertEquals(255, $color->getBlue()); $this->assertEquals(0, $color->getAlpha()); $this->assertEquals('#ffffff', (string) $color); $this->assertEquals('#00ff00', (string) new Color('00ff00')); $this->assertTrue($color->isOpaque()); }
/** * Internal * * Generates a GD color from Color instance * * @param Color $color * * @return resource * * @throws RuntimeException */ private function getColor(Color $color) { $index = imagecolorallocatealpha($this->resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round(127 * $color->getAlpha() / 100)); if (false === $index) { throw new RuntimeException(sprintf('Unable to allocate color "RGB(%s, %s, %s)" with transparency ' . 'of %d percent', $color->getRed(), $color->getGreen(), $color->getBlue(), $color->getAlpha())); } return $index; }
/** * Internal * * Generates a GD color from Color instance * * @param Imagine\Image\Color $color * * @return resource * * @throws Imagine\Exception\RuntimeException */ private function getColor(Color $color) { static $cache = array(); $key = (string) $color . "-" . $color->getAlpha(); if (!isset($cache[$key])) { $cache[$key] = imagecolorallocatealpha( $this->resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round(127 * $color->getAlpha() / 100) ); } if (false === $cache[$key]) { throw new RuntimeException(sprintf( 'Unable to allocate color "RGB(%s, %s, %s)" with transparency '. 'of %d percent', $color->getRed(), $color->getGreen(), $color->getBlue(), $color->getAlpha() )); } return $cache[$key]; }