Die Berechnung von CMYK und RGB wurde angeglichen, um die Lesbarkeit zu erhöhen. #3

Merged
TorstenHettstedt merged 1 commits from fixed/berechnungen-verbessern into master 2021-03-23 14:26:58 +01:00
2 changed files with 45 additions and 45 deletions
Showing only changes of commit 5ce7cf2552 - Show all commits
@@ -72,33 +72,33 @@ class HueSaturationValueToCyanMagentaYellowKey
switch ($interval_id) {
case 1: // Yellow
$this->cyan = $this->calcRemainder();
$this->cyan = $this->calcColorValue();
$this->yellow = $this->saturation;
break;
case 2:
$this->cyan = $this->saturation;
$this->yellow = $this->calcRemainder(true);
$this->yellow = $this->calcColorValue(true);
break;
case 3: // Cyan
$this->cyan = $this->saturation;
$this->magenta = $this->calcRemainder();
$this->magenta = $this->calcColorValue();
break;
case 4:
$this->cyan = $this->calcRemainder(true);
$this->cyan = $this->calcColorValue(true);
$this->magenta = $this->saturation;
break;
case 5: // Magenta
$this->magenta = $this->saturation;
$this->yellow = $this->calcRemainder();
$this->yellow = $this->calcColorValue();
break;
default:
$this->magenta = $this->calcRemainder(true);
$this->magenta = $this->calcColorValue(true);
$this->yellow = $this->saturation;
break;
}
}
protected function calcRemainder($reciprocal = false): float
protected function calcColorValue($reciprocal = false): float
{
// Rest-Anteil am Vollkreis des aktuellen Intervals als Wert '[0,1]'
$remainder_grad = $this->hueAsPercentGrad % self::INTERVAL;
@@ -10,11 +10,13 @@ use TorstenHettstedt\Colors\ColorModels\RedGreenBlue;
class HueSaturationValueToRedGreenBlue
{
/** @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 */
@@ -26,7 +28,7 @@ class HueSaturationValueToRedGreenBlue
/** @var float : Definition des Grünanteil */
protected float $green = 1.0;
/** @var float : Definition des Blauanteil */
protected float $blu = 1.0;
protected float $blue = 1.0;
/**
* HueSaturationValueToRedGreenBlue constructor.
@@ -36,6 +38,7 @@ class HueSaturationValueToRedGreenBlue
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();
}
@@ -47,65 +50,62 @@ class HueSaturationValueToRedGreenBlue
return (new RedGreenBlue())
->setRed($this->red)
->setGreen($this->green)
->setBlue($this->blu);
->setBlue($this->blue);
}
protected function processConvert(): void
{
if ($this->saturation === 0.0) {
$this->red = $this->green = $this->blu = $this->value;
} else {
$this->red = $this->green = $this->blue = $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)),
];
$interval_id = intdiv($this->hueAsPercentGrad , self::INTERVAL);
// Ist der Wert der entgegen gesetzten Farbe.
$opposite_color_value = $this->value * ( 1 - $this->saturation);
switch ($interval_id) {
case 1:
$this->red = $help_values[2];
$this->green = $help_values[0];
$this->blu = $help_values[1];
$this->red = $this->calcColorValue();
$this->blue = $opposite_color_value;
break;
case 2:
$this->red = $help_values[1];
$this->green = $help_values[0];
$this->blu = $help_values[3];
case 2: // Green
$this->red = $opposite_color_value;
$this->blue = $this->calcColorValue(true);
break;
case 3:
$this->red = $help_values[1];
$this->green = $help_values[2];
$this->blu = $help_values[0];
$this->red = $opposite_color_value;
$this->green = $this->calcColorValue();
break;
case 4:
$this->red = $help_values[3];
$this->green = $help_values[1];
$this->blu = $help_values[0];
case 4: // Blue
$this->red = $this->calcColorValue(true);
$this->green = $opposite_color_value;
break;
case 5:
$this->red = $help_values[0];
$this->green = $help_values[1];
$this->blu = $help_values[2];
$this->green = $opposite_color_value;
$this->blue = $this->calcColorValue();
break;
default:
$this->red = $help_values[0];
$this->green = $help_values[3];
$this->blu = $help_values[1];
default: // Red
$this->green = $this->calcColorValue(true);
$this->blue = $opposite_color_value;
break;
}
}
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));
return $color_value;
}
}