diff --git a/src/Transformer/Utilities/RedGreenBlueToHueSaturationValue.php b/src/Transformer/Utilities/RedGreenBlueToHueSaturationValue.php index 865b36b..4b06a31 100644 --- a/src/Transformer/Utilities/RedGreenBlueToHueSaturationValue.php +++ b/src/Transformer/Utilities/RedGreenBlueToHueSaturationValue.php @@ -48,12 +48,20 @@ class RedGreenBlueToHueSaturationValue protected function processConvert(): void { -// $this->maxCmyProperty = max($this->cyan, $this->magenta, $this->yellow); -// $this->minCmyProperty = min($this->cyan, $this->magenta, $this->yellow); -// -// $this->hue = $this->buildHueAmount(); -// $this->saturation = $this->maxCmyProperty; -// $this->value = 1 - $this->key; + $this->maxRgbProperty = max($this->red, $this->green, $this->blu); + $this->minRgbProperty = min($this->red, $this->green, $this->blu); + + $this->hue = $this->buildHueAmount(); + $this->saturation = $this->buildSaturation(); + $this->value = $this->maxRgbProperty; + + while($this->hue < 0.0) { + $this->hue += 1.0; + } + + while($this->hue > 1.0) { + $this->hue -= 1.0; + } } public function convert(): HueSaturationValue @@ -68,16 +76,26 @@ class RedGreenBlueToHueSaturationValue protected function buildHueAmount(): float { - return 0.0; -// 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), -// }; + if ($this->maxRgbProperty === $this->minRgbProperty) { + return 0.0; + } + + return match ($this->maxRgbProperty) { + $this->red => $this->hueAmount(self::PROPERTY_RED), + $this->green => $this->hueAmount(self::PROPERTY_GREEN), + default => $this->hueAmount(self::PROPERTY_BLUE), + }; + } + + protected function buildSaturation(): float + { + if ($this->maxRgbProperty === 0.0) { + return 0.0; + } + + $dividend = $this->maxRgbProperty - $this->minRgbProperty; + + return $dividend / $this->maxRgbProperty; } /** @@ -87,34 +105,30 @@ class RedGreenBlueToHueSaturationValue */ 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; + static $amount_share = 2.0; + + $divisor = $this->maxRgbProperty - $this->minRgbProperty; + + switch ($property) { + case self::PROPERTY_RED: + $amount = 0; // 0.0 + $dividend = $this->green - $this->blu; + break; + case self::PROPERTY_GREEN: + $amount = 1; // 0.333 + $dividend = $this->blu -$this->red; + break; + case self::PROPERTY_BLUE: + $amount = 2; // 0.999 + $dividend = $this->red - $this->green; + break; + default: + return 0; + } + + $start_amount = $amount * $amount_share; + $expansion_amount = $dividend / $divisor; + return ($start_amount + $expansion_amount) / 6.0; } } \ No newline at end of file diff --git a/tests/unit/Transformer/ValidColorValuesProvider.php b/tests/unit/Transformer/ValidColorValuesProvider.php index de9bbb5..8b1d0a3 100644 --- a/tests/unit/Transformer/ValidColorValuesProvider.php +++ b/tests/unit/Transformer/ValidColorValuesProvider.php @@ -123,7 +123,7 @@ class ValidColorValuesProvider 'violet' => [ self::CMYK => [ 'cyan' => 0.19, - 'magenta' => 0.32, + 'magenta' => 0.321, 'yellow' => 0.0, 'key' => 0.29, ], @@ -134,7 +134,7 @@ class ValidColorValuesProvider ], self::HSV => [ 'hue' => 0.734, - 'saturation' => 0.32, + 'saturation' => 0.321, 'value' => 0.71, ], ],