Skip to content

Commit

Permalink
Merge pull request #44 from NikarashiHatsu/to-cmyk
Browse files Browse the repository at this point in the history
Add CMYK
  • Loading branch information
ozdemirburak authored Oct 6, 2023
2 parents 09f5d5a + 2d2a700 commit 4d01791
Show file tree
Hide file tree
Showing 21 changed files with 585 additions and 150 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ $hexa = $rgba->toHexa(); // \OzdemirBurak\Iris\Color\Hexa('a7add154')
echo $rgba; // rgba(127,127,127,0.5)
```

### CMYK
```php
use OzdemirBurak\Iris\Color\Cmyk;

$cmyk = new Cmyk('cmyk(0,100,0,0');

echo $cmyk->cyan(); // 0
echo $cmyk->magenta(); // 100
echo $cmyk->yellow(); // 0
echo $cmyk->black(); // 0

$values = $cmyk->values(); // [0, 100, 0, 0]
$hex = $cmyk->toHex(); // OzdemirBurak\Iris\Color\Hex('ff00ff')
$hexa = $cmyk->toHexa(); // OzdemirBurak\Iris\Color\Hexa('ff00ffff')
$hsl = $cmyk->toHsl(); // OzdemirBurak\Iris\Color\Hsl('300,100,50')
$hsla = $cmyk->toHsla(); // OzdemirBurak\Iris\Color\Hsla('300,100,50,1.0')
$hsv = $cmyk->toHsv(); // OzdemirBurak\Iris\Color\Hsv('300,100,100')
$rgb = $cmyk->toRgb(); // OzdemirBurak\Iris\Color\Rgb('255,0,255')
$rgba = $cmyk->toRgba(); // OzdemirBurak\Iris\Color\Rgba('255,0,255,1.0')

echo $cmyk; // cmyk(0,100,0,0)
```

### Via Factory

If you do not know what the color string will be (for example, you're getting it from a group of rows from a database),
Expand Down
5 changes: 5 additions & 0 deletions src/BaseColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ abstract public function toHsv(): Color\Hsv;
*/
abstract public function toRgb(): Color\Rgb;

/**
* @return \OzdemirBurak\Iris\Color\Cmyk
*/
abstract public function toCmyk(): Color\Cmyk;

/**
* @return \OzdemirBurak\Iris\Color\Rgba
*/
Expand Down
137 changes: 137 additions & 0 deletions src/Color/Cmyk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace OzdemirBurak\Iris\Color;

use OzdemirBurak\Iris\BaseColor;
use OzdemirBurak\Iris\Helpers\DefinedColor;
use OzdemirBurak\Iris\Traits\CmykTrait;

class Cmyk extends BaseColor
{
use CmykTrait;

/**
* @param string $code
*
* @return string|bool
*/
protected function validate(string $code): bool|string
{
$color = str_replace(['cmyk', '(', ')', ' ', '%'], '', DefinedColor::find($code, 4));
if (preg_match('/^(\d{1,3}),(\d{1,3}),(\d{1,3}),(\d{1,3})$/', $color, $matches)) {
if ($matches[1] > 100 || $matches[2] > 100 || $matches[3] > 100 || $matches[4] > 100) {
return false;
}
return $color;
}
return false;
}

/**
* @param string $color
*
* @return array
*/
protected function initialize(string $color): array
{
return [$this->cyan, $this->magenta, $this->yellow, $this->black] = explode(',', $color);
}

/**
* @return array
*/
public function values(): array
{
return [
$this->cyan(),
$this->magenta(),
$this->yellow(),
$this->black(),
];
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hex
*/
public function toHex(): Hex
{
return $this->toRgb()->toHex();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hexa
*/
public function toHexa(): Hexa
{
return $this->toHex()->toHexa();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hsl
*/
public function toHsl(): Hsl
{
return $this->toRgb()->toHsl();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hsla
*/
public function toHsla(): Hsla
{
return $this->toRgb()->toHsla();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Hsv
*/
public function toHsv(): Hsv
{
return $this->toRgb()->toHsv();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Rgb
*/
public function toRgb(): Rgb
{
[$c, $m, $y, $k] = $this->values();
$r = 255 * (1 - ($c / 100)) * (1 - ($k / 100));
$g = 255 * (1 - ($m / 100)) * (1 - ($k / 100));
$b = 255 * (1 - ($y / 100)) * (1 - ($k / 100));
$code = implode(',', [$r, $g, $b]);
return new Rgb($code);
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Cmyk
*/
public function toCmyk(): Cmyk
{
return $this;
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Rgba
*/
public function toRgba(): Rgba
{
return $this->toRgb()->toRgba();
}

/**
* @return string
*/
public function __toString(): string
{
return 'cmyk(' . implode(',', $this->values()) . ')';
}
}
5 changes: 4 additions & 1 deletion src/Color/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ public static function init(string $color): BaseColor
{
$color = str_replace(' ', '', $color);
// Definitive types
if (preg_match('/^(?P<type>(rgba?|hsla?|hsv))/i', $color, $match)) {
if (preg_match('/^(?P<type>(rgba?|hsla?|hsv|cmyk))/i', $color, $match)) {
$class = self::resolveClass($match['type']);
return new $class($color);
}
// Best guess
if (preg_match('/^\d{1,3},\d{1,3},\d{1,3},\d{1,3}[^.]$/', $color)) {
return new Cmyk($color);
}
if (preg_match('/^#?[a-f0-9]{8}$/i', $color)) {
return new Hexa($color);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Color/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ public function toRgba(): Rgba
return $this->toRgb()->toRgba();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Cmyk
*/
public function toCmyk(): Cmyk
{
return $this->toRgb()->toCmyk();
}

/**
* @return string
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Color/Hexa.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ public function toRgba(): Rgba
return $this->toRgb()->toRgba()->alpha($this->alpha());
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Cmyk
*/
public function toCmyk(): Cmyk
{
return $this->toRgb()->toCmyk();
}

/**
* @return string
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Color/Hsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public function toRgb(): Rgb
return $this->convertToRgb();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Cmyk
*/
public function toCmyk(): Cmyk
{
return $this->toRgb()->toCmyk();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Rgba
Expand Down
9 changes: 9 additions & 0 deletions src/Color/Hsla.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ public function toHexa(): Hexa
return $this->toHex()->toHexa()->alpha($this->alpha());
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Cmyk
*/
public function toCmyk(): Cmyk
{
return $this->toRgb()->toCmyk();
}

/**
* @return string
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Color/Hsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ public function toRgba(): Rgba
return $this->toRgb()->toRgba();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Cmyk
*/
public function toCmyk(): Cmyk
{
return $this->toRgb()->toCmyk();
}

/**
* @return string
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Color/Rgb.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ public function toRgba(): Rgba
return new Rgba(implode(',', array_merge($this->values(), ['1.0'])));
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Cmyk
*/
public function toCmyk(): Cmyk
{
[$r, $g, $b] = $this->values();
$k = 1 - (max($r, $g, $b) / 255);
$c = (1 - $r / 255 - $k) / (1 - $k);
$m = (1 - $g / 255 - $k) / (1 - $k);
$y = (1 - $b / 255 - $k) / (1 - $k);
$code = implode(',', [$c * 100, $m * 100, $y * 100, $k * 100]);
return new Cmyk($code);
}

/**
* @return string
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Color/Rgba.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ public function toHsv(): Hsv
return $this->toRgb()->toHsv();
}

/**
* @throws \OzdemirBurak\Iris\Exceptions\InvalidColorException
* @return \OzdemirBurak\Iris\Color\Cmyk
*/
public function toCmyk(): Cmyk
{
return $this->toRgb()->toCmyk();
}

/**
* @return string
*/
Expand Down
Loading

0 comments on commit 4d01791

Please sign in to comment.