Skip to content

Commit

Permalink
Refactor colors & add setChannel method
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Dec 3, 2023
1 parent 8d9d3a0 commit 0594322
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 208 deletions.
78 changes: 78 additions & 0 deletions src/Colors/AbstractColor.php
@@ -0,0 +1,78 @@
<?php

namespace Intervention\Image\Colors;

use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;

abstract class AbstractColor implements ColorInterface
{
/**
* Color channels
*/
protected array $channels;

public function channels(): array
{
return $this->channels;
}

public function channel(string $classname): ColorChannelInterface
{
$channels = array_filter($this->channels(), function (ColorChannelInterface $channel) use ($classname) {
return get_class($channel) == $classname;
});

if (count($channels) == 0) {
throw new ColorException('Channel ' . $classname . ' could not be found.');
}

return reset($channels);
}

public function normalize(): array
{
return array_map(function (ColorChannelInterface $channel) {
return $channel->normalize();
}, $this->channels());
}

/**
* {@inheritdoc}
*
* @see ColorInterface::toArray()
*/
public function toArray(): array
{
return array_map(function (ColorChannelInterface $channel) {
return $channel->value();
}, $this->channels());
}

/**
* {@inheritdoc}
*
* @see ColorInterface::convertTo()
*/
public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
{
$colorspace = match (true) {
is_object($colorspace) => $colorspace,
default => new $colorspace(),
};

return $colorspace->importColor($this);
}

/**
* {@inheritdoc}
*
* @see ColorInterface::__toString()
*/
public function __toString(): string
{
return $this->toString();
}
}
38 changes: 2 additions & 36 deletions src/Colors/Cmyk/Color.php
Expand Up @@ -2,23 +2,19 @@

namespace Intervention\Image\Colors\Cmyk;

use Intervention\Image\Colors\AbstractColor;
use Intervention\Image\Colors\Cmyk\Channels\Cyan;
use Intervention\Image\Colors\Cmyk\Channels\Magenta;
use Intervention\Image\Colors\Cmyk\Channels\Yellow;
use Intervention\Image\Colors\Cmyk\Channels\Key;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Colors\Traits\CanHandleChannels;
use Intervention\Image\Drivers\AbstractInputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;

class Color implements ColorInterface
class Color extends AbstractColor
{
use CanHandleChannels;

protected array $channels;

public function __construct(int $c, int $m, int $y, int $k)
{
$this->channels = [
Expand Down Expand Up @@ -48,11 +44,6 @@ public function toHex(string $prefix = ''): string
return $this->convertTo(RgbColorspace::class)->toHex($prefix);
}

public function channels(): array
{
return $this->channels;
}

public function cyan(): ColorChannelInterface
{
return $this->channel(Cyan::class);
Expand All @@ -73,26 +64,6 @@ public function key(): ColorChannelInterface
return $this->channel(Key::class);
}

public function toArray(): array
{
return [
$this->cyan()->value(),
$this->magenta()->value(),
$this->yellow()->value(),
$this->key()->value(),
];
}

public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
{
$colorspace = match (true) {
is_object($colorspace) => $colorspace,
default => new $colorspace(),
};

return $colorspace->importColor($this);
}

public function toString(): string
{
return sprintf(
Expand All @@ -112,9 +83,4 @@ public function isGreyscale(): bool
$this->yellow()->value(),
]);
}

public function __toString(): string
{
return $this->toString();
}
}
48 changes: 2 additions & 46 deletions src/Colors/Hsl/Color.php
Expand Up @@ -2,25 +2,18 @@

namespace Intervention\Image\Colors\Hsl;

use Intervention\Image\Colors\AbstractColor;
use Intervention\Image\Colors\Hsl\Channels\Hue;
use Intervention\Image\Colors\Hsl\Channels\Luminance;
use Intervention\Image\Colors\Hsl\Channels\Saturation;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Colors\Traits\CanHandleChannels;
use Intervention\Image\Drivers\AbstractInputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;

class Color implements ColorInterface
class Color extends AbstractColor
{
use CanHandleChannels;

/**
* Color channels
*/
protected array $channels;

public function __construct(int $h, int $s, int $l)
{
$this->channels = [
Expand Down Expand Up @@ -79,33 +72,6 @@ public function luminance(): ColorChannelInterface
return $this->channel(Luminance::class);
}

/**
* {@inheritdoc}
*
* @see ColorInterface::toArray()
*/
public function toArray(): array
{
return array_map(function (ColorChannelInterface $channel) {
return $channel->value();
}, $this->channels());
}

/**
* {@inheritdoc}
*
* @see ColorInterface::convertTo()
*/
public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
{
$colorspace = match (true) {
is_object($colorspace) => $colorspace,
default => new $colorspace(),
};

return $colorspace->importColor($this);
}

public function toHex(string $prefix = ''): string
{
return $this->convertTo(RgbColorspace::class)->toHex($prefix);
Expand Down Expand Up @@ -135,14 +101,4 @@ public function isGreyscale(): bool
{
return $this->saturation()->value() == 0;
}

/**
* {@inheritdoc}
*
* @see ColorInterface::__toString()
*/
public function __toString(): string
{
return $this->toString();
}
}
48 changes: 2 additions & 46 deletions src/Colors/Hsv/Color.php
Expand Up @@ -2,25 +2,18 @@

namespace Intervention\Image\Colors\Hsv;

use Intervention\Image\Colors\AbstractColor;
use Intervention\Image\Colors\Hsv\Channels\Hue;
use Intervention\Image\Colors\Hsv\Channels\Saturation;
use Intervention\Image\Colors\Hsv\Channels\Value;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Colors\Traits\CanHandleChannels;
use Intervention\Image\Drivers\AbstractInputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;

class Color implements ColorInterface
class Color extends AbstractColor
{
use CanHandleChannels;

/**
* Color channels
*/
protected array $channels;

public function __construct(int $h, int $s, int $v)
{
$this->channels = [
Expand Down Expand Up @@ -79,33 +72,6 @@ public function value(): ColorChannelInterface
return $this->channel(Value::class);
}

/**
* {@inheritdoc}
*
* @see ColorInterface::toArray()
*/
public function toArray(): array
{
return array_map(function (ColorChannelInterface $channel) {
return $channel->value();
}, $this->channels());
}

/**
* {@inheritdoc}
*
* @see ColorInterface::convertTo()
*/
public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
{
$colorspace = match (true) {
is_object($colorspace) => $colorspace,
default => new $colorspace(),
};

return $colorspace->importColor($this);
}

public function toHex(string $prefix = ''): string
{
return $this->convertTo(RgbColorspace::class)->toHex($prefix);
Expand Down Expand Up @@ -135,14 +101,4 @@ public function isGreyscale(): bool
{
return $this->saturation()->value() == 0;
}

/**
* {@inheritdoc}
*
* @see ColorInterface::__toString()
*/
public function __toString(): string
{
return $this->toString();
}
}
48 changes: 2 additions & 46 deletions src/Colors/Rgb/Color.php
Expand Up @@ -2,25 +2,18 @@

namespace Intervention\Image\Colors\Rgb;

use Intervention\Image\Colors\AbstractColor;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Colors\Rgb\Channels\Alpha;
use Intervention\Image\Colors\Traits\CanHandleChannels;
use Intervention\Image\Drivers\AbstractInputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;

class Color implements ColorInterface
class Color extends AbstractColor
{
use CanHandleChannels;

/**
* Color channels
*/
protected array $channels;

/**
* Create new instance
*
Expand Down Expand Up @@ -102,18 +95,6 @@ public function alpha(): ColorChannelInterface
return $this->channel(Alpha::class);
}

/**
* {@inheritdoc}
*
* @see ColorInterface::toArray()
*/
public function toArray(): array
{
return array_map(function (ColorChannelInterface $channel) {
return $channel->value();
}, $this->channels());
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -141,21 +122,6 @@ public function toHex(string $prefix = ''): string
);
}

/**
* {@inheritdoc}
*
* @see ColorInterface::convertTo()
*/
public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
{
$colorspace = match (true) {
is_object($colorspace) => $colorspace,
default => new $colorspace(),
};

return $colorspace->importColor($this);
}

public function isFullyOpaque(): bool
{
return $this->alpha()->value() === 255;
Expand Down Expand Up @@ -197,14 +163,4 @@ public function isGreyscale(): bool

return count(array_unique($values, SORT_REGULAR)) === 1;
}

/**
* {@inheritdoc}
*
* @see ColorInterface::__toString()
*/
public function __toString(): string
{
return $this->toString();
}
}

0 comments on commit 0594322

Please sign in to comment.