refactor: Helfer-Klassen ersetzten eine abstrakte Klasse

This commit is contained in:
2022-03-01 21:17:52 +01:00
parent 039d780d85
commit 9505be376a
7 changed files with 135 additions and 105 deletions
@@ -1,71 +0,0 @@
<?php
namespace TorstenHettstedt\Colors\NaturallyColorModels;
use InvalidArgumentException;
abstract class AbstractColorModel
{
protected function asPercentValue(float $value): int
{
return intval($value * 100);
}
protected function fromPercentValue(int $value): float
{
return $value / 100.0;
}
protected function checkPercentValue(int $value): void
{
if ($value > 100) {
throw new InvalidArgumentException('Ein Wert größer als 100% ist nicht definierbar.');
}
if ($value < 0) {
throw new InvalidArgumentException('Ein Wert kleiner als 0% ist nicht definierbar.');
}
}
protected function asGradValue(float $value): int
{
return intval($value * 360);
}
protected function fromGradValue(int $value): float
{
return $value / 360.0;
}
protected function checkGradValue(int $value): void
{
if ($value > 360) {
throw new InvalidArgumentException('Ein Wert größer als 360° ist nicht definierbar.');
}
if ($value < 0) {
throw new InvalidArgumentException('Ein Wert kleiner als 0° ist nicht definierbar.');
}
}
protected function asHexadecimalValue(float $value): int
{
return intval($value * 0xFF);
}
protected function fromHexadecimalValue(int $value): float
{
return $value / 0xFF;
}
protected function checkHexadecimalValue(int $value): void
{
if ($value > 0xFF) {
throw new InvalidArgumentException('Ein Wert größer als "0xFF" ist nicht definierbar.');
}
if ($value < 0) {
throw new InvalidArgumentException('Ein Wert kleiner als "0x00" ist nicht definierbar.');
}
}
}
@@ -10,14 +10,16 @@ use TorstenHettstedt\Colors\ColorModels\CyanMagentaYellowKey as PureCyanMagentaY
* Definition des allgemeinen CMYK-Farbraumes. Die Werte für die eigenschaften sind reelle Zahlen von 0 bis einschließlich 1.
* Ohne Änderung der Eigenschaften wird die Farbe *weiß* dargestellt.
*/
class CyanMagentaYellowKey extends AbstractColorModel
class CyanMagentaYellowKey
{
/** @var PureCyanMagentaYellowKey : Standard-Color-Modell */
protected PureCyanMagentaYellowKey $pureColor;
protected Helper\PercentValues $percentValuesHelper;
#[Pure] public function __construct()
{
$this->pureColor = new PureCyanMagentaYellowKey();
$this->percentValuesHelper = new Helper\PercentValues();
}
/**
@@ -25,7 +27,7 @@ class CyanMagentaYellowKey extends AbstractColorModel
*/
#[Pure] public function getCyan(): int
{
return $this->asPercentValue($this->pureColor->getCyan());
return $this->percentValuesHelper->asPercentValue($this->pureColor->getCyan());
}
/**
@@ -34,8 +36,8 @@ class CyanMagentaYellowKey extends AbstractColorModel
*/
public function setCyan(int $cyan): CyanMagentaYellowKey
{
$this->checkPercentValue($cyan);
$this->pureColor->setCyan($this->fromPercentValue($cyan));
$this->percentValuesHelper->checkPercentValue($cyan);
$this->pureColor->setCyan($this->percentValuesHelper->fromPercentValue($cyan));
return $this;
}
@@ -44,7 +46,7 @@ class CyanMagentaYellowKey extends AbstractColorModel
*/
#[Pure] public function getMagenta(): int
{
return $this->asPercentValue($this->pureColor->getMagenta());
return $this->percentValuesHelper->asPercentValue($this->pureColor->getMagenta());
}
/**
@@ -53,8 +55,8 @@ class CyanMagentaYellowKey extends AbstractColorModel
*/
public function setMagenta(int $magenta): CyanMagentaYellowKey
{
$this->checkPercentValue($magenta);
$this->pureColor->setMagenta($this->fromPercentValue($magenta));
$this->percentValuesHelper->checkPercentValue($magenta);
$this->pureColor->setMagenta($this->percentValuesHelper->fromPercentValue($magenta));
return $this;
}
@@ -63,7 +65,7 @@ class CyanMagentaYellowKey extends AbstractColorModel
*/
#[Pure] public function getYellow(): int
{
return $this->asPercentValue($this->pureColor->getYellow());
return $this->percentValuesHelper->asPercentValue($this->pureColor->getYellow());
}
/**
@@ -72,8 +74,8 @@ class CyanMagentaYellowKey extends AbstractColorModel
*/
public function setYellow(int $yellow): CyanMagentaYellowKey
{
$this->checkPercentValue($yellow);
$this->pureColor->setYellow($this->fromPercentValue($yellow));
$this->percentValuesHelper->checkPercentValue($yellow);
$this->pureColor->setYellow($this->percentValuesHelper->fromPercentValue($yellow));
return $this;
}
@@ -82,7 +84,7 @@ class CyanMagentaYellowKey extends AbstractColorModel
*/
#[Pure] public function getKey(): int
{
return $this->asPercentValue($this->pureColor->getKey());
return $this->percentValuesHelper->asPercentValue($this->pureColor->getKey());
}
/**
@@ -91,8 +93,8 @@ class CyanMagentaYellowKey extends AbstractColorModel
*/
public function setKey(int $key): CyanMagentaYellowKey
{
$this->checkPercentValue($key);
$this->pureColor->setKey($this->fromPercentValue($key));
$this->percentValuesHelper->checkPercentValue($key);
$this->pureColor->setKey($this->percentValuesHelper->fromPercentValue($key));
return $this;
}
@@ -0,0 +1,31 @@
<?php
namespace TorstenHettstedt\Colors\NaturallyColorModels\Helper;
use InvalidArgumentException;
class GradValues
{
public function asGradValue(float $value): int
{
return intval($value * 360);
}
public function fromGradValue(int $value): float
{
return $value / 360.0;
}
public function checkGradValue(int $value): void
{
if ($value > 360) {
throw new InvalidArgumentException('Ein Wert größer als 360° ist nicht definierbar.');
}
if ($value < 0) {
throw new InvalidArgumentException('Ein Wert kleiner als 0° ist nicht definierbar.');
}
}
}
@@ -0,0 +1,31 @@
<?php
namespace TorstenHettstedt\Colors\NaturallyColorModels\Helper;
use InvalidArgumentException;
class HexadecimalValues
{
public function asHexadecimalValue(float $value): int
{
return intval($value * 0xFF);
}
public function fromHexadecimalValue(int $value): float
{
return $value / 0xFF;
}
public function checkHexadecimalValue(int $value): void
{
if ($value > 0xFF) {
throw new InvalidArgumentException('Ein Wert größer als "0xFF" ist nicht definierbar.');
}
if ($value < 0) {
throw new InvalidArgumentException('Ein Wert kleiner als "0x00" ist nicht definierbar.');
}
}
}
@@ -0,0 +1,31 @@
<?php
namespace TorstenHettstedt\Colors\NaturallyColorModels\Helper;
use InvalidArgumentException;
class PercentValues
{
public function asPercentValue(float $value): int
{
return intval($value * 100);
}
public function fromPercentValue(int $value): float
{
return $value / 100.0;
}
public function checkPercentValue(int $value): void
{
if ($value > 100) {
throw new InvalidArgumentException('Ein Wert größer als 100% ist nicht definierbar.');
}
if ($value < 0) {
throw new InvalidArgumentException('Ein Wert kleiner als 0% ist nicht definierbar.');
}
}
}
+14 -10
View File
@@ -11,14 +11,18 @@ use TorstenHettstedt\Colors\ColorModels\HueSaturationValue as PureHueSaturationV
* Definition des allgemeinen HSV-Farbraumes. Die Werte für die Eigenschaften sind reelle Zahlen von 0 bis einschließlich 1.
* Ohne Änderung der Eigenschaften wird die Farbe *weiß* dargestellt.
*/
class HueSaturationValue extends AbstractColorModel
class HueSaturationValue
{
/** @var PureHueSaturationValue : Standard-Color-Modell */
protected PureHueSaturationValue $pureColor;
protected Helper\PercentValues $percentValuesHelper;
protected Helper\GradValues $gradValuesHelper;
#[Pure] public function __construct()
{
$this->pureColor = new PureHueSaturationValue();
$this->percentValuesHelper = new Helper\PercentValues();
$this->gradValuesHelper = new Helper\GradValues();
}
/**
@@ -26,7 +30,7 @@ class HueSaturationValue extends AbstractColorModel
*/
#[Pure] public function getHue(): int
{
return $this->asGradValue($this->pureColor->getHue());
return $this->gradValuesHelper->asGradValue($this->pureColor->getHue());
}
/**
@@ -35,8 +39,8 @@ class HueSaturationValue extends AbstractColorModel
*/
public function setHue(int $hue): HueSaturationValue
{
$this->checkGradValue($hue);
$this->pureColor->setHue($this->fromGradValue($hue));
$this->gradValuesHelper->checkGradValue($hue);
$this->pureColor->setHue($this->gradValuesHelper->fromGradValue($hue));
return $this;
}
@@ -45,7 +49,7 @@ class HueSaturationValue extends AbstractColorModel
*/
#[Pure] public function getSaturation(): int
{
return $this->asPercentValue($this->pureColor->getSaturation());
return $this->percentValuesHelper->asPercentValue($this->pureColor->getSaturation());
}
/**
@@ -54,8 +58,8 @@ class HueSaturationValue extends AbstractColorModel
*/
public function setSaturation(int $saturation): HueSaturationValue
{
$this->checkPercentValue($saturation);
$this->pureColor->setSaturation($this->fromPercentValue($saturation));
$this->percentValuesHelper->checkPercentValue($saturation);
$this->pureColor->setSaturation($this->percentValuesHelper->fromPercentValue($saturation));
return $this;
}
@@ -64,7 +68,7 @@ class HueSaturationValue extends AbstractColorModel
*/
#[Pure] public function getValue(): int
{
return $this->asPercentValue($this->pureColor->getValue());
return $this->percentValuesHelper->asPercentValue($this->pureColor->getValue());
}
/**
@@ -73,8 +77,8 @@ class HueSaturationValue extends AbstractColorModel
*/
public function setValue(int $value): HueSaturationValue
{
$this->checkPercentValue($value);
$this->pureColor->setValue($this->fromPercentValue($value));
$this->percentValuesHelper->checkPercentValue($value);
$this->pureColor->setValue($this->percentValuesHelper->fromPercentValue($value));
return $this;
}
+13 -11
View File
@@ -9,14 +9,16 @@ use TorstenHettstedt\Colors\ColorModels\RedGreenBlue as PureRedGreenBlue;
* Definition des allgemeinen RGB-Farbraumes. Die Werte für die eigenschaften sind reelle Zahlen von 0 bis einschließlich 1.
* Ohne Änderung der Eigenschaften wird die Farbe *weiß* dargestellt.
*/
class RedGreenBlue extends AbstractColorModel
class RedGreenBlue
{
/** @var PureRedGreenBlue : Standard-Color-Modell */
protected PureRedGreenBlue $pureColor;
protected PureRedGreenBlue $pureColor;
protected Helper\HexadecimalValues $hexadecimalValuesHelper;
#[Pure] public function __construct()
{
$this->pureColor = new PureRedGreenBlue();
$this->hexadecimalValuesHelper = new Helper\HexadecimalValues();
}
/**
@@ -24,7 +26,7 @@ class RedGreenBlue extends AbstractColorModel
*/
#[Pure] public function getRed(): int
{
return $this->asHexadecimalValue($this->pureColor->getRed());
return $this->hexadecimalValuesHelper->asHexadecimalValue($this->pureColor->getRed());
}
/**
@@ -33,8 +35,8 @@ class RedGreenBlue extends AbstractColorModel
*/
public function setRed(int $red): RedGreenBlue
{
$this->checkHexadecimalValue($red);
$this->pureColor->setRed($this->fromHexadecimalValue($red));
$this->hexadecimalValuesHelper->checkHexadecimalValue($red);
$this->pureColor->setRed($this->hexadecimalValuesHelper->fromHexadecimalValue($red));
return $this;
}
@@ -43,7 +45,7 @@ class RedGreenBlue extends AbstractColorModel
*/
#[Pure] public function getGreen(): int
{
return $this->asHexadecimalValue($this->pureColor->getGreen());
return $this->hexadecimalValuesHelper->asHexadecimalValue($this->pureColor->getGreen());
}
/**
@@ -52,8 +54,8 @@ class RedGreenBlue extends AbstractColorModel
*/
public function setGreen(int $green): RedGreenBlue
{
$this->checkHexadecimalValue($green);
$this->pureColor->setGreen($this->fromHexadecimalValue($green));
$this->hexadecimalValuesHelper->checkHexadecimalValue($green);
$this->pureColor->setGreen($this->hexadecimalValuesHelper->fromHexadecimalValue($green));
return $this;
}
@@ -62,7 +64,7 @@ class RedGreenBlue extends AbstractColorModel
*/
public function getBlue(): int
{
return $this->asHexadecimalValue($this->pureColor->getBlue());
return $this->hexadecimalValuesHelper->asHexadecimalValue($this->pureColor->getBlue());
}
/**
@@ -71,8 +73,8 @@ class RedGreenBlue extends AbstractColorModel
*/
public function setBlue(int $blue): RedGreenBlue
{
$this->checkHexadecimalValue($blue);
$this->pureColor->setBlue($this->fromHexadecimalValue($blue));
$this->hexadecimalValuesHelper->checkHexadecimalValue($blue);
$this->pureColor->setBlue($this->hexadecimalValuesHelper->fromHexadecimalValue($blue));
return $this;
}