From af11d3be46fd2a03a15c2e50b56a98960102b529 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Tue, 4 Sep 2018 09:34:11 +0200 Subject: [PATCH 1/2] Add getChannelsMaxValue to PaletteInterface It's often useful to know the max value that a color channel can accept --- CHANGELOG.md | 2 ++ src/Gmagick/Image.php | 6 +----- src/Image/Palette/CMYK.php | 19 ++++++++++++++---- src/Image/Palette/Grayscale.php | 13 +++++++++++- src/Image/Palette/PaletteInterface.php | 7 +++++++ src/Image/Palette/RGB.php | 20 ++++++++++++++----- src/Imagick/Image.php | 6 +----- .../Test/Constraint/IsColorSimilar.php | 6 +----- 8 files changed, 54 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d15a3c66..b01b67541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ * Fix handling of PNG compression in Imagick `save` method (@mlocati) * New drawer methods: `rectangle` and `circle` (@mlocati) **BREAKING CHANGE** if you have your own implementation of `DrawerTest` you should add these two new methods + * The `getChannelsMaxValue` method has been added to `PaletteInterface` (@mlocati) + **BREAKING CHANGE** if you have your own `PaletteInterface` implementation, it now must implement this new method ### 1.0.0-alpha1 (2018-08-28) * Imagine is now tested under Windows too (@mlocati) diff --git a/src/Gmagick/Image.php b/src/Gmagick/Image.php index 8b374e736..9fc85b6e6 100644 --- a/src/Gmagick/Image.php +++ b/src/Gmagick/Image.php @@ -672,11 +672,7 @@ public function pixelToColor(\GmagickPixel $pixel) } } - if ($this->palette()->name() === PaletteInterface::PALETTE_CMYK) { - $multiplier = 100; - } else { - $multiplier = 255; - } + $multiplier = $this->palette()->getChannelsMaxValue(); return $this->palette->color(array_map(function ($color) use ($multiplier, $pixel, $colorMapping) { if (!isset($colorMapping[$color])) { diff --git a/src/Image/Palette/CMYK.php b/src/Image/Palette/CMYK.php index 2d7278102..c0126acfd 100644 --- a/src/Image/Palette/CMYK.php +++ b/src/Image/Palette/CMYK.php @@ -78,6 +78,16 @@ public function supportsAlpha() return false; } + /** + * {@inheritdoc} + * + * @see \Imagine\Image\Palette\PaletteInterface::getChannelsMaxValue() + */ + public function getChannelsMaxValue() + { + return 100; + } + /** * {@inheritdoc} * @@ -109,12 +119,13 @@ public function blend(ColorInterface $color1, ColorInterface $color2, $amount) if (!$color1 instanceof CMYKColor || !$color2 instanceof CMYKColor) { throw new RuntimeException('CMYK palette can only blend CMYK colors'); } + $max = $this->getChannelsMaxValue(); return $this->color(array( - min(100, $color1->getCyan() + $color2->getCyan() * $amount), - min(100, $color1->getMagenta() + $color2->getMagenta() * $amount), - min(100, $color1->getYellow() + $color2->getYellow() * $amount), - min(100, $color1->getKeyline() + $color2->getKeyline() * $amount), + min($max, $color1->getCyan() + $color2->getCyan() * $amount), + min($max, $color1->getMagenta() + $color2->getMagenta() * $amount), + min($max, $color1->getYellow() + $color2->getYellow() * $amount), + min($max, $color1->getKeyline() + $color2->getKeyline() * $amount), )); } diff --git a/src/Image/Palette/Grayscale.php b/src/Image/Palette/Grayscale.php index de786706e..5f918984f 100644 --- a/src/Image/Palette/Grayscale.php +++ b/src/Image/Palette/Grayscale.php @@ -72,6 +72,16 @@ public function supportsAlpha() return true; } + /** + * {@inheritdoc} + * + * @see \Imagine\Image\Palette\PaletteInterface::getChannelsMaxValue() + */ + public function getChannelsMaxValue() + { + return 255; + } + /** * {@inheritdoc} * @@ -129,10 +139,11 @@ public function blend(ColorInterface $color1, ColorInterface $color2, $amount) if (!$color1 instanceof GrayColor || !$color2 instanceof GrayColor) { throw new RuntimeException('Grayscale palette can only blend Grayscale colors'); } + $max = $this->getChannelsMaxValue(); return $this->color( array( - (int) min(255, min($color1->getGray(), $color2->getGray()) + round(abs($color2->getGray() - $color1->getGray()) * $amount)), + (int) min($max, min($color1->getGray(), $color2->getGray()) + round(abs($color2->getGray() - $color1->getGray()) * $amount)), ), (int) min(100, min($color1->getAlpha(), $color2->getAlpha()) + round(abs($color2->getAlpha() - $color1->getAlpha()) * $amount)) ); diff --git a/src/Image/Palette/PaletteInterface.php b/src/Image/Palette/PaletteInterface.php index b32dec5da..adcb5dd89 100644 --- a/src/Image/Palette/PaletteInterface.php +++ b/src/Image/Palette/PaletteInterface.php @@ -102,4 +102,11 @@ public function pixelDefinition(); * @return bool */ public function supportsAlpha(); + + /** + * Get the max value of palette components (255 for RGB and Grayscale, 100 for CMYK). + * + * @return int + */ + public function getChannelsMaxValue(); } diff --git a/src/Image/Palette/RGB.php b/src/Image/Palette/RGB.php index ef63d1bfd..c853dab38 100644 --- a/src/Image/Palette/RGB.php +++ b/src/Image/Palette/RGB.php @@ -76,6 +76,16 @@ public function supportsAlpha() return true; } + /** + * {@inheritdoc} + * + * @see \Imagine\Image\Palette\PaletteInterface::getChannelsMaxValue() + */ + public function getChannelsMaxValue() + { + return 255; + } + /** * {@inheritdoc} * @@ -133,17 +143,17 @@ public function blend(ColorInterface $color1, ColorInterface $color2, $amount) if (!$color1 instanceof RGBColor || !$color2 instanceof RGBColor) { throw new RuntimeException('RGB palette can only blend RGB colors'); } - $amount = (float) $amount; $amountComplement = 1 - $amount; + $max = $this->getChannelsMaxValue(); return $this->color( array( - min(max((int) round($color2->getRed() * $amount + $color1->getRed() * $amountComplement), 0), 255), - min(max((int) round($color2->getGreen() * $amount + $color1->getGreen() * $amountComplement), 0), 255), - min(max((int) round($color2->getBlue() * $amount + $color1->getBlue() * $amountComplement), 0), 255), + min(max((int) round($color2->getRed() * $amount + $color1->getRed() * $amountComplement), 0), $max), + min(max((int) round($color2->getGreen() * $amount + $color1->getGreen() * $amountComplement), 0), $max), + min(max((int) round($color2->getBlue() * $amount + $color1->getBlue() * $amountComplement), 0), $max), ), - min(max((int) round($color2->getAlpha() * $amount + $color1->getAlpha() * $amountComplement), 0), 255) + min(max((int) round($color2->getAlpha() * $amount + $color1->getAlpha() * $amountComplement), 0), 100) ); } } diff --git a/src/Imagick/Image.php b/src/Imagick/Image.php index 3a9833d3a..e2cd6ae96 100644 --- a/src/Imagick/Image.php +++ b/src/Imagick/Image.php @@ -624,11 +624,7 @@ public function pixelToColor(\ImagickPixel $pixel) $alpha = min(max($alpha, 0), 100); } - if ($this->palette()->name() === PaletteInterface::PALETTE_CMYK) { - $multiplier = 100; - } else { - $multiplier = 255; - } + $multiplier = $this->palette()->getChannelsMaxValue(); return $this->palette->color(array_map(function ($color) use ($multiplier, $pixel, $colorMapping) { if (!isset($colorMapping[$color])) { diff --git a/tests/Imagine/Test/Constraint/IsColorSimilar.php b/tests/Imagine/Test/Constraint/IsColorSimilar.php index e97787cf9..154805707 100644 --- a/tests/Imagine/Test/Constraint/IsColorSimilar.php +++ b/tests/Imagine/Test/Constraint/IsColorSimilar.php @@ -65,11 +65,7 @@ public function __construct($value, $maxDistance = 0.1) } $this->maxDistance = $maxDistance; $this->pixelDefinition = $this->value->getPalette()->pixelDefinition(); - if ($this->value->getPalette() === PaletteInterface::PALETTE_CMYK) { - $this->channelMultiplier = 100; - } else { - $this->channelMultiplier = 255; - } + $this->channelMultiplier = $this->value->getPalette()->getChannelsMaxValue(); } /** From e69cd16b4d18493b3f271352080d1922996cc2ed Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Tue, 4 Sep 2018 09:50:37 +0200 Subject: [PATCH 2/2] Fix coding style --- tests/Imagine/Test/Constraint/IsColorSimilar.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Imagine/Test/Constraint/IsColorSimilar.php b/tests/Imagine/Test/Constraint/IsColorSimilar.php index 154805707..3ed39544f 100644 --- a/tests/Imagine/Test/Constraint/IsColorSimilar.php +++ b/tests/Imagine/Test/Constraint/IsColorSimilar.php @@ -12,7 +12,6 @@ namespace Imagine\Test\Constraint; use Imagine\Image\Palette\Color\ColorInterface; -use Imagine\Image\Palette\PaletteInterface; use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Util\InvalidArgumentHelper;