Skip to content

Commit

Permalink
Merge pull request #675 from mlocati/palette-max-value
Browse files Browse the repository at this point in the history
Add getChannelsMaxValue to PaletteInterface
  • Loading branch information
mlocati authored Sep 4, 2018
2 parents c998405 + e69cd16 commit 8a176dd
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions src/Gmagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
19 changes: 15 additions & 4 deletions src/Image/Palette/CMYK.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public function supportsAlpha()
return false;
}

/**
* {@inheritdoc}
*
* @see \Imagine\Image\Palette\PaletteInterface::getChannelsMaxValue()
*/
public function getChannelsMaxValue()
{
return 100;
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -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),
));
}

Expand Down
13 changes: 12 additions & 1 deletion src/Image/Palette/Grayscale.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public function supportsAlpha()
return true;
}

/**
* {@inheritdoc}
*
* @see \Imagine\Image\Palette\PaletteInterface::getChannelsMaxValue()
*/
public function getChannelsMaxValue()
{
return 255;
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -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))
);
Expand Down
7 changes: 7 additions & 0 deletions src/Image/Palette/PaletteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
20 changes: 15 additions & 5 deletions src/Image/Palette/RGB.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public function supportsAlpha()
return true;
}

/**
* {@inheritdoc}
*
* @see \Imagine\Image\Palette\PaletteInterface::getChannelsMaxValue()
*/
public function getChannelsMaxValue()
{
return 255;
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -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)
);
}
}
6 changes: 1 addition & 5 deletions src/Imagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
7 changes: 1 addition & 6 deletions tests/Imagine/Test/Constraint/IsColorSimilar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -65,11 +64,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();
}

/**
Expand Down

0 comments on commit 8a176dd

Please sign in to comment.