Die Umwandlung von RGB zu HSV ist erfolgreich. Dabei musste die Genauigkeit angepasst werden.

This commit is contained in:
2021-03-09 15:26:04 +01:00
parent f8868ddd37
commit 9bc53ed768
2 changed files with 60 additions and 46 deletions
@@ -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;
}
}