Added: Incorrect image orientation

This commit is contained in:
RainLoop Team 2015-10-29 23:02:58 +03:00
parent 217207883f
commit e3b62d24d8
87 changed files with 10461 additions and 21 deletions

View file

@ -0,0 +1,118 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Image\Palette\Color\CMYK as CMYKColor;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Exception\RuntimeException;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Image\Profile;
use Imagine\Image\ProfileInterface;
class CMYK implements PaletteInterface
{
private $parser;
private $profile;
private static $colors = array();
public function __construct()
{
$this->parser = new ColorParser();
}
/**
* {@inheritdoc}
*/
public function name()
{
return PaletteInterface::PALETTE_CMYK;
}
/**
* {@inheritdoc}
*/
public function pixelDefinition()
{
return array(
ColorInterface::COLOR_CYAN,
ColorInterface::COLOR_MAGENTA,
ColorInterface::COLOR_YELLOW,
ColorInterface::COLOR_KEYLINE,
);
}
/**
* {@inheritdoc}
*/
public function supportsAlpha()
{
return false;
}
/**
* {@inheritdoc}
*/
public function color($color, $alpha = null)
{
if (null !== $alpha) {
throw new InvalidArgumentException('CMYK palette does not support alpha');
}
$color = $this->parser->parseToCMYK($color);
$index = sprintf('cmyk(%d, %d, %d, %d)', $color[0], $color[1], $color[2], $color[3]);
if (false === array_key_exists($index, self::$colors)) {
self::$colors[$index] = new CMYKColor($this, $color);
}
return self::$colors[$index];
}
/**
* {@inheritdoc}
*/
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');
}
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),
));
}
/**
* {@inheritdoc}
*/
public function useProfile(ProfileInterface $profile)
{
$this->profile = $profile;
return $this;
}
/**
* {@inheritdoc}
*/
public function profile()
{
if (!$this->profile) {
$this->profile = Profile::fromPath(__DIR__ . '/../../resources/Adobe/CMYK/USWebUncoated.icc');
}
return $this->profile;
}
}

View file

@ -0,0 +1,219 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette\Color;
use Imagine\Image\Palette\CMYK as CMYKPalette;
use Imagine\Exception\RuntimeException;
use Imagine\Exception\InvalidArgumentException;
final class CMYK implements ColorInterface
{
/**
* @var integer
*/
private $c;
/**
* @var integer
*/
private $m;
/**
* @var integer
*/
private $y;
/**
* @var integer
*/
private $k;
/**
*
* @var CMYK
*/
private $palette;
public function __construct(CMYKPalette $palette, array $color)
{
$this->palette = $palette;
$this->setColor($color);
}
/**
* {@inheritdoc}
*/
public function getValue($component)
{
switch ($component) {
case ColorInterface::COLOR_CYAN:
return $this->getCyan();
case ColorInterface::COLOR_MAGENTA:
return $this->getMagenta();
case ColorInterface::COLOR_YELLOW:
return $this->getYellow();
case ColorInterface::COLOR_KEYLINE:
return $this->getKeyline();
default:
throw new InvalidArgumentException(sprintf('Color component %s is not valid', $component));
}
}
/**
* Returns Cyan value of the color
*
* @return integer
*/
public function getCyan()
{
return $this->c;
}
/**
* Returns Magenta value of the color
*
* @return integer
*/
public function getMagenta()
{
return $this->m;
}
/**
* Returns Yellow value of the color
*
* @return integer
*/
public function getYellow()
{
return $this->y;
}
/**
* Returns Key value of the color
*
* @return integer
*/
public function getKeyline()
{
return $this->k;
}
/**
* {@inheritdoc}
*/
public function getPalette()
{
return $this->palette;
}
/**
* {@inheritdoc}
*/
public function getAlpha()
{
return null;
}
/**
* {@inheritdoc}
*/
public function dissolve($alpha)
{
throw new RuntimeException('CMYK does not support dissolution');
}
/**
* {@inheritdoc}
*/
public function lighten($shade)
{
return $this->palette->color(
array(
$this->c,
$this->m,
$this->y,
max(0, $this->k - $shade),
)
);
}
/**
* {@inheritdoc}
*/
public function darken($shade)
{
return $this->palette->color(
array(
$this->c,
$this->m,
$this->y,
min(100, $this->k + $shade),
)
);
}
/**
* {@inheritdoc}
*/
public function grayscale()
{
$color = array(
$this->c * (1 - $this->k / 100) + $this->k,
$this->m * (1 - $this->k / 100) + $this->k,
$this->y * (1 - $this->k / 100) + $this->k,
);
$gray = min(100, round(0.299 * $color[0] + 0.587 * $color[1] + 0.114 * $color[2]));
return $this->palette->color(array($gray, $gray, $gray, $this->k));
}
/**
* {@inheritdoc}
*/
public function isOpaque()
{
return true;
}
/**
* Returns hex representation of the color
*
* @return string
*/
public function __toString()
{
return sprintf('cmyk(%d%%, %d%%, %d%%, %d%%)', $this->c, $this->m, $this->y, $this->k);
}
/**
* Internal, Performs checks for color validity (an of array(C, M, Y, K))
*
* @param array $color
*
* @throws InvalidArgumentException
*/
private function setColor(array $color)
{
if (count($color) !== 4) {
throw new InvalidArgumentException('Color argument must look like array(C, M, Y, K), where C, M, Y, K are the integer values between 0 and 255 for cyan, magenta, yellow and black color indexes accordingly');
}
$colors = array_values($color);
array_walk($colors, function ($color) {
return max(0, min(100, $color));
});
list($this->c, $this->m, $this->y, $this->k) = $colors;
}
}

View file

@ -0,0 +1,95 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette\Color;
use Imagine\Image\Palette\PaletteInterface;
interface ColorInterface
{
const COLOR_RED = 'red';
const COLOR_GREEN = 'green';
const COLOR_BLUE = 'blue';
const COLOR_CYAN = 'cyan';
const COLOR_MAGENTA = 'magenta';
const COLOR_YELLOW = 'yellow';
const COLOR_KEYLINE = 'keyline';
const COLOR_GRAY = 'gray';
/**
* Return the value of one of the component.
*
* @param string $component One of the ColorInterface::COLOR_* component
*
* @return Integer
*/
public function getValue($component);
/**
* Returns percentage of transparency of the color
*
* @return integer
*/
public function getAlpha();
/**
* Returns the palette attached to the current color
*
* @return PaletteInterface
*/
public function getPalette();
/**
* Returns a copy of current color, incrementing the alpha channel by the
* given amount
*
* @param integer $alpha
*
* @return ColorInterface
*/
public function dissolve($alpha);
/**
* Returns a copy of the current color, lightened by the specified number
* of shades
*
* @param integer $shade
*
* @return ColorInterface
*/
public function lighten($shade);
/**
* Returns a copy of the current color, darkened by the specified number of
* shades
*
* @param integer $shade
*
* @return ColorInterface
*/
public function darken($shade);
/**
* Returns a gray related to the current color
*
* @return ColorInterface
*/
public function grayscale();
/**
* Checks if the current color is opaque
*
* @return Boolean
*/
public function isOpaque();
}

View file

@ -0,0 +1,164 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette\Color;
use Imagine\Image\Palette\Grayscale;
use Imagine\Exception\InvalidArgumentException;
final class Gray implements ColorInterface
{
/**
* @var integer
*/
private $gray;
/**
* @var integer
*/
private $alpha;
/**
*
* @var Grayscale
*/
private $palette;
public function __construct(Grayscale $palette, array $color, $alpha)
{
$this->palette = $palette;
$this->setColor($color);
$this->setAlpha($alpha);
}
/**
* {@inheritdoc}
*/
public function getValue($component)
{
switch ($component) {
case ColorInterface::COLOR_GRAY:
return $this->getGray();
default:
throw new InvalidArgumentException(sprintf('Color component %s is not valid', $component));
}
}
/**
* Returns Gray value of the color
*
* @return integer
*/
public function getGray()
{
return $this->gray;
}
/**
* {@inheritdoc}
*/
public function getPalette()
{
return $this->palette;
}
/**
* {@inheritdoc}
*/
public function getAlpha()
{
return $this->alpha;
}
/**
* {@inheritdoc}
*/
public function dissolve($alpha)
{
return $this->palette->color(
array($this->gray), $this->alpha + $alpha
);
}
/**
* {@inheritdoc}
*/
public function lighten($shade)
{
return $this->palette->color(array(min(255, $this->gray + $shade)), $this->alpha);
}
/**
* {@inheritdoc}
*/
public function darken($shade)
{
return $this->palette->color(array(max(0, $this->gray - $shade)), $this->alpha);
}
/**
* {@inheritdoc}
*/
public function grayscale()
{
return $this;
}
/**
* {@inheritdoc}
*/
public function isOpaque()
{
return 100 === $this->alpha;
}
/**
* Returns hex representation of the color
*
* @return string
*/
public function __toString()
{
return sprintf('#%02x%02x%02x', $this->gray, $this->gray, $this->gray);
}
/**
* Performs checks for validity of given alpha value and sets it
*
* @param integer $alpha
*
* @throws InvalidArgumentException
*/
private function setAlpha($alpha)
{
if (!is_int($alpha) || $alpha < 0 || $alpha > 100) {
throw new InvalidArgumentException(sprintf('Alpha must be an integer between 0 and 100, %s given', $alpha));
}
$this->alpha = $alpha;
}
/**
* Performs checks for color validity (array of array(gray))
*
* @param array $color
*
* @throws InvalidArgumentException
*/
private function setColor(array $color)
{
if (count($color) !== 1) {
throw new InvalidArgumentException('Color argument must look like array(gray), where gray is the integer value between 0 and 255 for the grayscale');
}
list($this->gray) = array_values($color);
}
}

View file

@ -0,0 +1,214 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette\Color;
use Imagine\Image\Palette\RGB as RGBPalette;
use Imagine\Exception\InvalidArgumentException;
final class RGB implements ColorInterface
{
/**
* @var integer
*/
private $r;
/**
* @var integer
*/
private $g;
/**
* @var integer
*/
private $b;
/**
* @var integer
*/
private $alpha;
/**
*
* @var RGBPalette
*/
private $palette;
public function __construct(RGBPalette $palette, array $color, $alpha)
{
$this->palette = $palette;
$this->setColor($color);
$this->setAlpha($alpha);
}
/**
* {@inheritdoc}
*/
public function getValue($component)
{
switch ($component) {
case ColorInterface::COLOR_RED:
return $this->getRed();
case ColorInterface::COLOR_GREEN:
return $this->getGreen();
case ColorInterface::COLOR_BLUE:
return $this->getBlue();
default:
throw new InvalidArgumentException(sprintf('Color component %s is not valid', $component));
}
}
/**
* Returns RED value of the color
*
* @return integer
*/
public function getRed()
{
return $this->r;
}
/**
* Returns GREEN value of the color
*
* @return integer
*/
public function getGreen()
{
return $this->g;
}
/**
* Returns BLUE value of the color
*
* @return integer
*/
public function getBlue()
{
return $this->b;
}
/**
* {@inheritdoc}
*/
public function getPalette()
{
return $this->palette;
}
/**
* {@inheritdoc}
*/
public function getAlpha()
{
return $this->alpha;
}
/**
* {@inheritdoc}
*/
public function dissolve($alpha)
{
return $this->palette->color(array($this->r, $this->g, $this->b), $this->alpha + $alpha);
}
/**
* {@inheritdoc}
*/
public function lighten($shade)
{
return $this->palette->color(
array(
min(255, $this->r + $shade),
min(255, $this->g + $shade),
min(255, $this->b + $shade),
), $this->alpha
);
}
/**
* {@inheritdoc}
*/
public function darken($shade)
{
return $this->palette->color(
array(
max(0, $this->r - $shade),
max(0, $this->g - $shade),
max(0, $this->b - $shade),
), $this->alpha
);
}
/**
* {@inheritdoc}
*/
public function grayscale()
{
$gray = min(255, round(0.299 * $this->getRed() + 0.114 * $this->getBlue() + 0.587 * $this->getGreen()));
return $this->palette->color(array($gray, $gray, $gray), $this->alpha);
}
/**
* {@inheritdoc}
*/
public function isOpaque()
{
return 100 === $this->alpha;
}
/**
* Returns hex representation of the color
*
* @return string
*/
public function __toString()
{
return sprintf('#%02x%02x%02x', $this->r, $this->g, $this->b);
}
/**
* Internal
*
* Performs checks for validity of given alpha value and sets it
*
* @param integer $alpha
*
* @throws InvalidArgumentException
*/
private function setAlpha($alpha)
{
if (!is_int($alpha) || $alpha < 0 || $alpha > 100) {
throw new InvalidArgumentException(sprintf('Alpha must be an integer between 0 and 100, %s given', $alpha));
}
$this->alpha = $alpha;
}
/**
* Internal
*
* Performs checks for color validity (array of array(R, G, B))
*
* @param array $color
*
* @throws InvalidArgumentException
*/
private function setColor(array $color)
{
if (count($color) !== 3) {
throw new InvalidArgumentException('Color argument must look like array(R, G, B), where R, G, B are the integer values between 0 and 255 for red, green and blue color indexes accordingly');
}
list($this->r, $this->g, $this->b) = array_values($color);
}
}

View file

@ -0,0 +1,153 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Exception\InvalidArgumentException;
class ColorParser
{
/**
* Parses a color to a RGB tuple
*
* @param string|array|integer $color
*
* @return array
*
* @throws InvalidArgumentException
*/
public function parseToRGB($color)
{
$color = $this->parse($color);
if (4 === count($color)) {
$color = array(
255 * (1 - $color[0] / 100) * (1 - $color[3] / 100),
255 * (1 - $color[1] / 100) * (1 - $color[3] / 100),
255 * (1 - $color[2] / 100) * (1 - $color[3] / 100),
);
}
return $color;
}
/**
* Parses a color to a CMYK tuple
*
* @param string|array|integer $color
*
* @return array
*
* @throws InvalidArgumentException
*/
public function parseToCMYK($color)
{
$color = $this->parse($color);
if (3 === count($color)) {
$r = $color[0] / 255;
$g = $color[1] / 255;
$b = $color[2] / 255;
$k = 1 - max($r, $g, $b);
$color = array(
1 === $k ? 0 : round((1 - $r - $k) / (1- $k) * 100),
1 === $k ? 0 : round((1 - $g - $k) / (1- $k) * 100),
1 === $k ? 0 : round((1 - $b - $k) / (1- $k) * 100),
round($k * 100)
);
}
return $color;
}
/**
* Parses a color to a grayscale value
*
* @param string|array|integer $color
*
* @return array
*
* @throws InvalidArgumentException
*/
public function parseToGrayscale($color)
{
if (is_array($color) && 1 === count($color)) {
return array_values($color);
}
$color = array_unique($this->parse($color));
if (1 !== count($color)) {
throw new InvalidArgumentException('The provided color has different values of red, green and blue components. Grayscale colors must have the same values for these.');
}
return $color;
}
/**
* Parses a color
*
* @param string|array|integer $color
*
* @return array
*
* @throws InvalidArgumentException
*/
private function parse($color)
{
if (!is_string($color) && !is_array($color) && !is_int($color)) {
throw new InvalidArgumentException(sprintf('Color must be specified as a hexadecimal string, array or integer, %s given', gettype($color)));
}
if (is_array($color)) {
if (3 === count($color) || 4 === count($color)) {
return array_values($color);
}
throw new InvalidArgumentException('Color argument if array, must look like array(R, G, B), or array(C, M, Y, K) where R, G, B are the integer values between 0 and 255 for red, green and blue or cyan, magenta, yellow and black color indexes accordingly');
}
if (is_string($color)) {
if (0 === strpos($color, 'cmyk(')) {
$substrColor = substr($color, 5, strlen($color) - 6);
$components = array_map(function ($component) {
return round(trim($component, ' %'));
}, explode(',', $substrColor));
if (count($components) !== 4) {
throw new InvalidArgumentException(sprintf('Unable to parse color %s', $color));
}
return $components;
} else {
$color = ltrim($color, '#');
if (strlen($color) !== 3 && strlen($color) !== 6) {
throw new InvalidArgumentException(sprintf('Color must be a hex value in regular (6 characters) or short (3 characters) notation, "%s" given', $color));
}
if (strlen($color) === 3) {
$color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
}
$color = array_map('hexdec', str_split($color, 2));
}
}
if (is_int($color)) {
$color = array(255 & ($color >> 16), 255 & ($color >> 8), 255 & $color);
}
return $color;
}
}

View file

@ -0,0 +1,123 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Image\Palette\Color\Gray as GrayColor;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\ProfileInterface;
use Imagine\Image\Profile;
use Imagine\Exception\RuntimeException;
class Grayscale implements PaletteInterface
{
/**
* @var ColorParser
*/
private $parser;
/**
* @var ProfileInterface
*/
private $profile;
/**
* @var array
*/
protected static $colors = array();
public function __construct()
{
$this->parser = new ColorParser();
}
/**
* {@inheritdoc}
*/
public function name()
{
return PaletteInterface::PALETTE_GRAYSCALE;
}
/**
* {@inheritdoc}
*/
public function pixelDefinition()
{
return array(ColorInterface::COLOR_GRAY);
}
/**
* {@inheritdoc}
*/
public function supportsAlpha()
{
return true;
}
/**
* {@inheritdoc}
*/
public function useProfile(ProfileInterface $profile)
{
$this->profile = $profile;
return $this;
}
/**
* {@inheritdoc}
*/
public function profile()
{
if (!$this->profile) {
$this->profile = Profile::fromPath(__DIR__ . '/../../resources/colormanagement.org/ISOcoated_v2_grey1c_bas.ICC');
}
return $this->profile;
}
/**
* {@inheritdoc}
*/
public function color($color, $alpha = null)
{
if (null === $alpha) {
$alpha = 0;
}
$color = $this->parser->parseToGrayscale($color);
$index = sprintf('#%02x%02x%02x-%d', $color[0], $color[0], $color[0], $alpha);
if (false === array_key_exists($index, static::$colors)) {
static::$colors[$index] = new GrayColor($this, $color, $alpha);
}
return static::$colors[$index];
}
/**
* {@inheritdoc}
*/
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');
}
return $this->color(
array(
(int) min(255, 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))
);
}
}

View file

@ -0,0 +1,87 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Image\ProfileInterface;
use Imagine\Image\Palette\Color\ColorInterface;
interface PaletteInterface
{
const PALETTE_GRAYSCALE = 'gray';
const PALETTE_RGB = 'rgb';
const PALETTE_CMYK = 'cmyk';
/**
* Returns a color given some values
*
* @param string|array|integer $color A color
* @param integer|null $alpha Set alpha to null to disable it
*
* @return ColorInterface
*
* @throws InvalidArgumentException In case you pass an alpha value to a
* Palette that does not support alpha
*/
public function color($color, $alpha = null);
/**
* Blend two colors given an amount
*
* @param ColorInterface $color1
* @param ColorInterface $color2
* @param float $amount The amount of color2 in color1
*
* @return ColorInterface
*/
public function blend(ColorInterface $color1, ColorInterface $color2, $amount);
/**
* Attachs an ICC profile to this Palette.
*
* (A default profile is provided by default)
*
* @param ProfileInterface $profile
*
* @return PaletteInterface
*/
public function useProfile(ProfileInterface $profile);
/**
* Returns the ICC profile attached to this Palette.
*
* @return ProfileInterface
*/
public function profile();
/**
* Returns the name of this Palette, one of PaletteInterface::PALETTE_*
* constants
*
* @return String
*/
public function name();
/**
* Returns an array containing ColorInterface::COLOR_* constants that
* define the structure of colors for a pixel.
*
* @return array
*/
public function pixelDefinition();
/**
* Tells if alpha channel is supported in this palette
*
* @return Boolean
*/
public function supportsAlpha();
}

View file

@ -0,0 +1,129 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Image\Palette\Color\RGB as RGBColor;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\ProfileInterface;
use Imagine\Image\Profile;
use Imagine\Exception\RuntimeException;
class RGB implements PaletteInterface
{
/**
* @var ColorParser
*/
private $parser;
/**
* @var ProfileInterface
*/
private $profile;
/**
* @var array
*/
protected static $colors = array();
public function __construct()
{
$this->parser = new ColorParser();
}
/**
* {@inheritdoc}
*/
public function name()
{
return PaletteInterface::PALETTE_RGB;
}
/**
* {@inheritdoc}
*/
public function pixelDefinition()
{
return array(
ColorInterface::COLOR_RED,
ColorInterface::COLOR_GREEN,
ColorInterface::COLOR_BLUE,
);
}
/**
* {@inheritdoc}
*/
public function supportsAlpha()
{
return true;
}
/**
* {@inheritdoc}
*/
public function useProfile(ProfileInterface $profile)
{
$this->profile = $profile;
return $this;
}
/**
* {@inheritdoc}
*/
public function profile()
{
if (!$this->profile) {
$this->profile = Profile::fromPath(__DIR__ . '/../../resources/color.org/sRGB_IEC61966-2-1_black_scaled.icc');
}
return $this->profile;
}
/**
* {@inheritdoc}
*/
public function color($color, $alpha = null)
{
if (null === $alpha) {
$alpha = 100;
}
$color = $this->parser->parseToRGB($color);
$index = sprintf('#%02x%02x%02x-%d', $color[0], $color[1], $color[2], $alpha);
if (false === array_key_exists($index, static::$colors)) {
static::$colors[$index] = new RGBColor($this, $color, $alpha);
}
return static::$colors[$index];
}
/**
* {@inheritdoc}
*/
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');
}
return $this->color(
array(
(int) min(255, min($color1->getRed(), $color2->getRed()) + round(abs($color2->getRed() - $color1->getRed()) * $amount)),
(int) min(255, min($color1->getGreen(), $color2->getGreen()) + round(abs($color2->getGreen() - $color1->getGreen()) * $amount)),
(int) min(255, min($color1->getBlue(), $color2->getBlue()) + round(abs($color2->getBlue() - $color1->getBlue()) * $amount)),
),
(int) min(100, min($color1->getAlpha(), $color2->getAlpha()) + round(abs($color2->getAlpha() - $color1->getAlpha()) * $amount))
);
}
}