feature: natürlichen Werte-Klassen hinzufügen
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?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.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace TorstenHettstedt\Colors\NaturallyColorModels;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use TorstenHettstedt\Colors\ColorModels\CyanMagentaYellowKey as PureCyanMagentaYellowKey;
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
/** @var PureCyanMagentaYellowKey : Standard-Color-Modell */
|
||||
protected PureCyanMagentaYellowKey $pureColor;
|
||||
|
||||
#[Pure] public function __construct()
|
||||
{
|
||||
$this->pureColor = new PureCyanMagentaYellowKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getCyan(): int
|
||||
{
|
||||
return $this->asPercentValue($this->pureColor->getCyan());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $cyan
|
||||
* @return CyanMagentaYellowKey
|
||||
*/
|
||||
public function setCyan(int $cyan): CyanMagentaYellowKey
|
||||
{
|
||||
$this->checkPercentValue($cyan);
|
||||
$this->pureColor->setCyan($this->fromPercentValue($cyan));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getMagenta(): int
|
||||
{
|
||||
return $this->asPercentValue($this->pureColor->getMagenta());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $magenta
|
||||
* @return CyanMagentaYellowKey
|
||||
*/
|
||||
public function setMagenta(int $magenta): CyanMagentaYellowKey
|
||||
{
|
||||
$this->checkPercentValue($magenta);
|
||||
$this->pureColor->setMagenta($this->fromPercentValue($magenta));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getYellow(): int
|
||||
{
|
||||
return $this->asPercentValue($this->pureColor->getYellow());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $yellow
|
||||
* @return CyanMagentaYellowKey
|
||||
*/
|
||||
public function setYellow(int $yellow): CyanMagentaYellowKey
|
||||
{
|
||||
$this->checkPercentValue($yellow);
|
||||
$this->pureColor->setYellow($this->fromPercentValue($yellow));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getKey(): int
|
||||
{
|
||||
return $this->asPercentValue($this->pureColor->getKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $key
|
||||
* @return CyanMagentaYellowKey
|
||||
*/
|
||||
public function setKey(int $key): CyanMagentaYellowKey
|
||||
{
|
||||
$this->checkPercentValue($key);
|
||||
$this->pureColor->setKey($this->fromPercentValue($key));
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace TorstenHettstedt\Colors\NaturallyColorModels;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use TorstenHettstedt\Colors\ColorModels\HueSaturationValue as PureHueSaturationValue;
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
/** @var PureHueSaturationValue : Standard-Color-Modell */
|
||||
protected PureHueSaturationValue $pureColor;
|
||||
|
||||
#[Pure] public function __construct()
|
||||
{
|
||||
$this->pureColor = new PureHueSaturationValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getHue(): int
|
||||
{
|
||||
return $this->asGradValue($this->pureColor->getHue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $hue
|
||||
* @return HueSaturationValue
|
||||
*/
|
||||
public function setHue(int $hue): HueSaturationValue
|
||||
{
|
||||
$this->checkGradValue($hue);
|
||||
$this->pureColor->setHue($this->fromGradValue($hue));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getSaturation(): int
|
||||
{
|
||||
return $this->asPercentValue($this->pureColor->getSaturation());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $saturation
|
||||
* @return HueSaturationValue
|
||||
*/
|
||||
public function setSaturation(int $saturation): HueSaturationValue
|
||||
{
|
||||
$this->checkPercentValue($saturation);
|
||||
$this->pureColor->setSaturation($this->fromPercentValue($saturation));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getValue(): int
|
||||
{
|
||||
return $this->asPercentValue($this->pureColor->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
* @return HueSaturationValue
|
||||
*/
|
||||
public function setValue(int $value): HueSaturationValue
|
||||
{
|
||||
$this->checkPercentValue($value);
|
||||
$this->pureColor->setValue($this->fromPercentValue($value));
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\Colors\NaturallyColorModels;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
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
|
||||
{
|
||||
/** @var PureRedGreenBlue : Standard-Color-Modell */
|
||||
protected PureRedGreenBlue $pureColor;
|
||||
|
||||
#[Pure] public function __construct()
|
||||
{
|
||||
$this->pureColor = new PureRedGreenBlue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getRed(): int
|
||||
{
|
||||
return $this->asHexadecimalValue($this->pureColor->getRed());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $red
|
||||
* @return RedGreenBlue
|
||||
*/
|
||||
public function setRed(int $red): RedGreenBlue
|
||||
{
|
||||
$this->checkHexadecimalValue($red);
|
||||
$this->pureColor->setRed($this->fromHexadecimalValue($red));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getGreen(): int
|
||||
{
|
||||
return $this->asHexadecimalValue($this->pureColor->getGreen());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $green
|
||||
* @return RedGreenBlue
|
||||
*/
|
||||
public function setGreen(int $green): RedGreenBlue
|
||||
{
|
||||
$this->checkHexadecimalValue($green);
|
||||
$this->pureColor->setGreen($this->fromHexadecimalValue($green));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getBlue(): int
|
||||
{
|
||||
return $this->asHexadecimalValue($this->pureColor->getBlue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $blue
|
||||
* @return RedGreenBlue
|
||||
*/
|
||||
public function setBlue(int $blue): RedGreenBlue
|
||||
{
|
||||
$this->checkHexadecimalValue($blue);
|
||||
$this->pureColor->setBlue($this->fromHexadecimalValue($blue));
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function asHtmlNotation(): string
|
||||
{
|
||||
return sprintf('#%X%X%X', $this->getRed(), $this->getGreen(), $this->getBlue());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user