/** * 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); $alpha = $this->palette->supportsAlpha() ? (int) round($pixel->getColorValue(\Imagick::COLOR_ALPHA) * 100) : null; return $this->palette->color(array_map(function ($color) use($pixel, $colorMapping) { if (!isset($colorMapping[$color])) { throw new InvalidArgumentException('Color %s is not mapped in Imagick'); } return $pixel->getColorValue($colorMapping[$color]) * 255; }, $this->palette->pixelDefinition()), $alpha); }
/** * 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); }