From ad2a7ddd1642bedaa97915fcbd5d3a52fb809ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20L=C3=BCcke?= Date: Thu, 18 Mar 2021 08:34:54 +0100 Subject: [PATCH] Die Umwandlung von HSV zu CMYK ist erfolgreich. --- ...eSaturationValueToCyanMagentaYellowKey.php | 92 +++++++++---------- 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/src/Transformer/Utilities/HueSaturationValueToCyanMagentaYellowKey.php b/src/Transformer/Utilities/HueSaturationValueToCyanMagentaYellowKey.php index eb9bac5..6913fd7 100644 --- a/src/Transformer/Utilities/HueSaturationValueToCyanMagentaYellowKey.php +++ b/src/Transformer/Utilities/HueSaturationValueToCyanMagentaYellowKey.php @@ -10,11 +10,13 @@ use TorstenHettstedt\Colors\ColorModels\HueSaturationValue; class HueSaturationValueToCyanMagentaYellowKey { /** @var int : Größe des Intervals in 1/100 Grad */ - protected const INTERVAL = 60 * 100; + protected const INTERVAL = 60.0 * 100.0; // Daten für HSV /** @var float : Definition des Farbton als Anteil eines Vollkreises '$Hue * π' */ protected float $hue; + /** @var float : Anteil am Vollkreis in 1/100 Grad */ + protected float $hueAsPercentGrad; /** @var float : Definition der Sättigung der Farbe */ protected float $saturation; /** @var float : Definition der Helligkeit der Farbe */ @@ -38,6 +40,7 @@ class HueSaturationValueToCyanMagentaYellowKey public function __construct(HueSaturationValue $from_color) { $this->hue = $from_color->getHue(); + $this->hueAsPercentGrad = intval($this->hue * self::INTERVAL * 6); $this->saturation = $from_color->getSaturation(); $this->value = $from_color->getValue(); } @@ -56,59 +59,52 @@ class HueSaturationValueToCyanMagentaYellowKey protected function processConvert(): void { - if ($this->saturation === 0.0) { - $this->key = $this->value; - } else { + $this->key = 1 - $this->value; + if ($this->saturation !== 0.0) { $this->splitColor(); } } protected function splitColor(): void { -// // Anteil am Vollkreis in 1/100 Grad -// $percent_grad = intval($this->hue * 360 * 100); -// // ID des zu nutzenden Interval -// $interval_id = intdiv($percent_grad , self::INTERVAL); -// // Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]' -// $remainder = (($percent_grad % self::INTERVAL) / self::INTERVAL); -// $help_values = [ -// $this->value, -// $this->value * ( 1 - $this->saturation), -// $this->value * ( 1 - $this->saturation * $remainder), -// $this->value * ( 1 - $this->saturation * (1 - $remainder)), -// ]; -// switch ($interval_id) { -// case 1: -// $this->red = $help_values[2]; -// $this->green = $help_values[0]; -// $this->blu = $help_values[1]; -// break; -// case 2: -// $this->red = $help_values[1]; -// $this->green = $help_values[0]; -// $this->blu = $help_values[3]; -// break; -// case 3: -// $this->red = $help_values[1]; -// $this->green = $help_values[2]; -// $this->blu = $help_values[0]; -// break; -// case 4: -// $this->red = $help_values[3]; -// $this->green = $help_values[1]; -// $this->blu = $help_values[0]; -// break; -// case 5: -// $this->red = $help_values[0]; -// $this->green = $help_values[1]; -// $this->blu = $help_values[2]; -// break; -// default: -// $this->red = $help_values[0]; -// $this->green = $help_values[3]; -// $this->blu = $help_values[1]; -// break; -// } + // ID des zu nutzenden Interval + $interval_id = intdiv($this->hueAsPercentGrad , self::INTERVAL); + + switch ($interval_id) { + case 1: // Yellow + $this->cyan = $this->calcRemainder(); + $this->yellow = $this->saturation; + break; + case 2: + $this->cyan = $this->saturation; + $this->yellow = $this->calcRemainder(true); + break; + case 3: // Cyan + $this->cyan = $this->saturation; + $this->magenta = $this->calcRemainder(); + break; + case 4: + $this->cyan = $this->calcRemainder(true); + $this->magenta = $this->saturation; + break; + case 5: // Magenta + $this->magenta = $this->saturation; + $this->yellow = $this->calcRemainder(); + break; + default: + $this->magenta = $this->calcRemainder(true); + $this->yellow = $this->saturation; + break; + } + } + + protected function calcRemainder($reciprocal = false): float + { + // Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]' + $remainder_grad = $this->hueAsPercentGrad % self::INTERVAL; + // Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]' + $remainder = $remainder_grad / self::INTERVAL; + return $this->saturation * ($reciprocal ? (1 - $remainder) : $remainder); } } \ No newline at end of file