/** * {@inheritdoc} */ public function getColorAt(PointInterface $point) { if (!$point->in($this->getSize())) { throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight())); } $index = imagecolorat($this->resource, $point->getX(), $point->getY()); $info = imagecolorsforindex($this->resource, $index); return $this->palette->color(array($info['red'], $info['green'], $info['blue']), max(min(100 - (int) round($info['alpha'] / 127 * 100), 100), 0)); }
/** * Returns a color given a pixel, depending the Palette context * * Note : this method is public for PHP 5.3 compatibility * * @param \ImagickPixel $pixel * * @return ColorInterface * * @throws InvalidArgumentException In case a unknown color is requested */ public function pixelToColor(\ImagickPixel $pixel) { static $colorMapping = array(ColorInterface::COLOR_RED => \Imagick::COLOR_RED, ColorInterface::COLOR_GREEN => \Imagick::COLOR_GREEN, ColorInterface::COLOR_BLUE => \Imagick::COLOR_BLUE, ColorInterface::COLOR_CYAN => \Imagick::COLOR_CYAN, ColorInterface::COLOR_MAGENTA => \Imagick::COLOR_MAGENTA, ColorInterface::COLOR_YELLOW => \Imagick::COLOR_YELLOW, ColorInterface::COLOR_KEYLINE => \Imagick::COLOR_BLACK, ColorInterface::COLOR_GRAY => \Imagick::COLOR_RED); $alpha = $this->palette->supportsAlpha() ? (int) round($pixel->getColorValue(\Imagick::COLOR_ALPHA) * 100) : null; $palette = $this->palette(); return $this->palette->color(array_map(function ($color) use($palette, $pixel, $colorMapping) { if (!isset($colorMapping[$color])) { throw new InvalidArgumentException(sprintf('Color %s is not mapped in Imagick', $color)); } $multiplier = 255; if ($palette->name() === PaletteInterface::PALETTE_CMYK) { $multiplier = 100; } return $pixel->getColorValue($colorMapping[$color]) * $multiplier; }, $this->palette->pixelDefinition()), $alpha); }