Da bei allen Transformationen vom HSV-Farbmodell ausgegangen wird, ist es von Vorteil diese Klassen in einer abstrakten Klasse zusammenzufassen.

This commit is contained in:
2021-03-23 14:52:32 +01:00
parent 0e426b96e3
commit 5288425e8d
3 changed files with 61 additions and 76 deletions
@@ -0,0 +1,56 @@
<?php
namespace TorstenHettstedt\Colors\Transformer\Utilities;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
abstract class AbstractHueSaturationValueToUtility
{
/** @var int : Größe des Intervals in 1/100 Grad */
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 */
protected float $value;
/**
* HueSaturationValueToRedGreenBlue constructor.
* @param HueSaturationValue $from_color
* @noinspection PhpPureAttributeCanBeAddedInspection
*/
public function __construct(HueSaturationValue $from_color)
{
$this->hue = $from_color->getHue();
$this->hueAsPercentGrad = intval($this->hue * self::INTERVAL * 6.0);
$this->saturation = $from_color->getSaturation();
$this->value = $from_color->getValue();
}
protected function processConvert(): void
{
if ($this->saturation > 0.0) {
$this->splitColor();
}
}
abstract protected function splitColor(): void;
protected function calcColorValue($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);
}
}
@@ -5,23 +5,9 @@ namespace TorstenHettstedt\Colors\Transformer\Utilities;
use TorstenHettstedt\Colors\ColorModels\CyanMagentaYellowKey;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
class HueSaturationValueToCyanMagentaYellowKey
class HueSaturationValueToCyanMagentaYellowKey extends AbstractHueSaturationValueToUtility
{
/** @var int : Größe des Intervals in 1/100 Grad */
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 */
protected float $value;
// Daten für CMYK
/** @var float : Definition des Cyan-Anteil */
protected float $cyan = 0.0;
@@ -32,19 +18,6 @@ class HueSaturationValueToCyanMagentaYellowKey
/** @var float : Definition des Schwarzanteil */
protected float $key = 0.0;
/**
* HueSaturationValueToRedGreenBlue constructor.
* @param HueSaturationValue $from_color
* @noinspection PhpPureAttributeCanBeAddedInspection
*/
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();
}
public function convert(): CyanMagentaYellowKey
{
$this->processConvert();
@@ -60,9 +33,7 @@ class HueSaturationValueToCyanMagentaYellowKey
protected function processConvert(): void
{
$this->key = 1 - $this->value;
if ($this->saturation !== 0.0) {
$this->splitColor();
}
parent::processConvert();
}
protected function splitColor(): void
@@ -98,13 +69,4 @@ class HueSaturationValueToCyanMagentaYellowKey
}
}
protected function calcColorValue($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);
}
}
@@ -4,24 +4,10 @@
namespace TorstenHettstedt\Colors\Transformer\Utilities;
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue;
use TorstenHettstedt\Colors\ColorModels\RedGreenBlue;
class HueSaturationValueToRedGreenBlue
class HueSaturationValueToRedGreenBlue extends AbstractHueSaturationValueToUtility
{
/** @var int : Größe des Intervals in 1/100 Grad */
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 */
protected float $value;
// Daten für RGB
/** @var float : Definition des Rotanteil */
protected float $red = 1.0;
@@ -30,19 +16,6 @@ class HueSaturationValueToRedGreenBlue
/** @var float : Definition des Blauanteil */
protected float $blue = 1.0;
/**
* HueSaturationValueToRedGreenBlue constructor.
* @param HueSaturationValue $from_color
* @noinspection PhpPureAttributeCanBeAddedInspection
*/
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();
}
public function convert(): RedGreenBlue
{
$this->processConvert();
@@ -57,9 +30,7 @@ class HueSaturationValueToRedGreenBlue
protected function processConvert(): void
{
$this->red = $this->green = $this->blue = $this->value;
if ($this->saturation > 0.0) {
$this->splitColor();
}
parent::processConvert();
}
protected function splitColor(): void
@@ -99,12 +70,8 @@ class HueSaturationValueToRedGreenBlue
protected function calcColorValue($reciprocal = false): float
{
// Rest-Anteil am Vollkreis in 1/100 Grad
$remainder_grad = $this->hueAsPercentGrad % self::INTERVAL;
// Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]'
$remainder = $remainder_grad / self::INTERVAL;
$color_value = $this->value;
$color_value *= 1 - ($this->saturation * ($reciprocal ? (1 - $remainder) : $remainder));
$color_value *= 1 - parent::calcColorValue($reciprocal);
return $color_value;
}