From f4b78d05e0b26311d1785dd0fb2e177dfab5b10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20L=C3=BCcke?= Date: Tue, 2 Mar 2021 18:52:55 +0100 Subject: [PATCH] =?UTF-8?q?Die=20Testst=20f=C3=BCr=20die=20Standardfarben?= =?UTF-8?q?=20sind=20erfolgreich.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CyanMagentaYellowKeyTransformer.php | 7 + ...anMagentaYellowKeyToHueSaturationValue.php | 131 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/Transformer/Utilities/CyanMagentaYellowKeyToHueSaturationValue.php diff --git a/src/Transformer/CyanMagentaYellowKeyTransformer.php b/src/Transformer/CyanMagentaYellowKeyTransformer.php index 2b9ef9f..4b79b7c 100644 --- a/src/Transformer/CyanMagentaYellowKeyTransformer.php +++ b/src/Transformer/CyanMagentaYellowKeyTransformer.php @@ -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(); + } } \ No newline at end of file diff --git a/src/Transformer/Utilities/CyanMagentaYellowKeyToHueSaturationValue.php b/src/Transformer/Utilities/CyanMagentaYellowKeyToHueSaturationValue.php new file mode 100644 index 0000000..33ae3d1 --- /dev/null +++ b/src/Transformer/Utilities/CyanMagentaYellowKeyToHueSaturationValue.php @@ -0,0 +1,131 @@ +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; + } + +} \ No newline at end of file