Die Testst für die Standardfarben sind erfolgreich.

This commit is contained in:
2021-03-02 18:52:55 +01:00
parent e85c57c628
commit f4b78d05e0
2 changed files with 138 additions and 0 deletions
@@ -5,6 +5,7 @@ namespace TorstenHettstedt\Colors\Transformer;
use TorstenHettstedt\Colors\ColorModels\CyanMagentaYellowKey;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
use TorstenHettstedt\Colors\ColorModels\RedGreenBlue;
class CyanMagentaYellowKeyTransformer implements ColorModelTransformInterface
@@ -42,4 +43,10 @@ class CyanMagentaYellowKeyTransformer implements ColorModelTransformInterface
{
return clone $this->color;
}
public function toHueSaturationValue(): HueSaturationValue
{
$utility = new Utilities\CyanMagentaYellowKeyToHueSaturationValue($this->color);
return $utility->convert();
}
}
@@ -0,0 +1,131 @@
<?php
namespace TorstenHettstedt\Colors\Transformer\Utilities;
use TorstenHettstedt\Colors\ColorModels\CyanMagentaYellowKey;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
class CyanMagentaYellowKeyToHueSaturationValue
{
const PROPERTY_CYAN = 'cyan';
const PROPERTY_MAGENTA = 'magenta';
const PROPERTY_YELLOW = 'yellow';
const PROPERTY_KEY = 'key';
// Daten für CMYK
/** @var float : Definition des Cyan-Anteil */
protected float $cyan;
/** @var float : Definition des Magenta-Anteil */
protected float $magenta;
/** @var float : Definition des Gelbanteil */
protected float $yellow;
/** @var float : Definition des Schwarzanteil */
protected float $key;
/** @var float : Der größte CMY-Farbwert */
protected float $maxCmyProperty;
/** @var float : Der kleinste CMY-Farbwert */
protected float $minCmyProperty;
// Daten für HSV
/** @var float : Definition des Farbton als Anteil eines Vollkreises '$Hue * π' */
protected float $hue = 0.0;
/** @var float : Definition der Sättigung der Farbe */
protected float $saturation = 0.0;
/** @var float : Definition der Helligkeit der Farbe */
protected float $value = 1.0;
/**
* CyanMagentaYellowKeyToHueSaturationValue constructor.
* @param CyanMagentaYellowKey $from_color
* @noinspection PhpPureAttributeCanBeAddedInspection
*/
public function __construct(CyanMagentaYellowKey $from_color)
{
$this->cyan = $from_color->getCyan();
$this->magenta = $from_color->getMagenta();
$this->yellow = $from_color->getYellow();
$this->key = $from_color->getKey();
}
protected function processConvert(): void
{
$this->maxCmyProperty = max($this->cyan, $this->magenta, $this->yellow);
$this->minCmyProperty = min($this->cyan, $this->magenta, $this->yellow);
$this->hue = round($this->buildHueAmount(), 3);
$this->hue = $this->buildHueAmount();
$this->saturation = $this->buildSaturationAmount();
$this->value = 1 - $this->key;
}
public function convert(): HueSaturationValue
{
$this->processConvert();
return (new HueSaturationValue())
->setHue($this->hue)
->setSaturation($this->saturation)
->setValue($this->value);
}
protected function buildHueAmount(): float
{
if ($this->maxCmyProperty === 0) {
return 0.0;
}
return match ($this->maxCmyProperty) {
$this->cyan => $this->hueAmount(self::PROPERTY_CYAN),
$this->magenta => $this->hueAmount(self::PROPERTY_MAGENTA),
default => $this->hueAmount(self::PROPERTY_YELLOW),
};
}
protected function buildSaturationAmount(): float
{
if ($this->maxCmyProperty === 0.0) {
return 0.0;
}
return ($this->maxCmyProperty - $this->minCmyProperty) / $this->maxCmyProperty;
}
/**
* @param string $property : Name der CMYK-Eigenschaft
* @return float
*/
private function hueAmount(string $property): float
{
if ($this->maxCmyProperty === 0.0) {
return 0.0;
}
static $amount_share = 2.0;
$divisor = $this->maxCmyProperty - $this->minCmyProperty;
switch ($property) {
case self::PROPERTY_YELLOW:
$amount = 0; // 0.167
$dividend = $this->cyan - $this->magenta;
break;
case self::PROPERTY_CYAN:
$amount = 1; // 0.500
$dividend = $this->magenta -$this->yellow;
break;
case self::PROPERTY_MAGENTA:
$amount = 2; // 0.833
$dividend = $this->yellow - $this->cyan;
break;
default:
return 0;
}
$start_amount = ($amount * $amount_share) + 1.0;
$expansion_amount = $dividend / $divisor;
return ($start_amount + $expansion_amount) / 6.0;
}
}