From 039d780d8517ebf43aaea725140228b10ab09b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20L=C3=BCcke?= Date: Tue, 1 Mar 2022 20:55:52 +0100 Subject: [PATCH] =?UTF-8?q?feature:=20nat=C3=BCrlichen=20Werte-Klassen=20h?= =?UTF-8?q?inzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractColorModel.php | 71 +++++++++++++ .../CyanMagentaYellowKey.php | 99 +++++++++++++++++++ .../HueSaturationValue.php | 82 +++++++++++++++ src/NaturallyColorModels/RedGreenBlue.php | 84 ++++++++++++++++ 4 files changed, 336 insertions(+) create mode 100644 src/NaturallyColorModels/AbstractColorModel.php create mode 100644 src/NaturallyColorModels/CyanMagentaYellowKey.php create mode 100644 src/NaturallyColorModels/HueSaturationValue.php create mode 100644 src/NaturallyColorModels/RedGreenBlue.php diff --git a/src/NaturallyColorModels/AbstractColorModel.php b/src/NaturallyColorModels/AbstractColorModel.php new file mode 100644 index 0000000..faf7af4 --- /dev/null +++ b/src/NaturallyColorModels/AbstractColorModel.php @@ -0,0 +1,71 @@ + 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.'); + } + } + +} \ No newline at end of file diff --git a/src/NaturallyColorModels/CyanMagentaYellowKey.php b/src/NaturallyColorModels/CyanMagentaYellowKey.php new file mode 100644 index 0000000..44a5f38 --- /dev/null +++ b/src/NaturallyColorModels/CyanMagentaYellowKey.php @@ -0,0 +1,99 @@ +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; + } + +} \ No newline at end of file diff --git a/src/NaturallyColorModels/HueSaturationValue.php b/src/NaturallyColorModels/HueSaturationValue.php new file mode 100644 index 0000000..9c30a63 --- /dev/null +++ b/src/NaturallyColorModels/HueSaturationValue.php @@ -0,0 +1,82 @@ +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; + } + + +} \ No newline at end of file diff --git a/src/NaturallyColorModels/RedGreenBlue.php b/src/NaturallyColorModels/RedGreenBlue.php new file mode 100644 index 0000000..9769489 --- /dev/null +++ b/src/NaturallyColorModels/RedGreenBlue.php @@ -0,0 +1,84 @@ +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()); + } + +} \ No newline at end of file