From 720b78b687dedc5c6bc34ef3d76a6249c5c00f13 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 12 Nov 2024 18:52:24 +0900 Subject: [PATCH] Add CIE XYZ classes for D50/D65 whitespace before clean up --- .../Convert/Color/Coordinates/XYZD50.php | 128 ++++++++++++++++++ .../Convert/Color/Coordinates/XYZD65.php | 54 +++++--- 2 files changed, 161 insertions(+), 21 deletions(-) create mode 100644 www/lib/CoreLibs/Convert/Color/Coordinates/XYZD50.php diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/XYZD50.php b/www/lib/CoreLibs/Convert/Color/Coordinates/XYZD50.php new file mode 100644 index 00000000..349bb5cd --- /dev/null +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/XYZD50.php @@ -0,0 +1,128 @@ + allowed colorspaces */ + private const COLORSPACES = ['CIEXYZ']; + + /** @var float X coordinate */ + private float $X = 0.0; + /** @var float Y coordinate (Luminance) */ + private float $Y = 0.0; + /** @var float Z coordinate (blue) */ + private float $Z = 0.0; + + /** @var string color space: either ok or cie */ + private string $colorspace = ''; + + /** + * Color Coordinate Lch + * for oklch + */ + public function __construct() + { + } + + /** + * set from array + * where 0: X, 1: Y, 2: Z + * + * @param array{0:float,1:float,2:float} $colors + * @param string $colorspace + * @return self + */ + public static function __constructFromArray(array $colors, string $colorspace = 'CieXYZ'): self + { + return (new XYZD50())->setColorspace($colorspace)->setFromArray($colors); + } + + /** + * set color + * + * @param string $name + * @param float $value + * @return void + */ + public function __set(string $name, float $value): void + { + if (!property_exists($this, $name)) { + throw new \ErrorException('Creation of dynamic property is not allowed', 0); + } + // if ($value < 0 || $value > 255) { + // throw new \LengthException('Argument value ' . $value . ' for color ' . $name + // . ' is not in the range of 0 to 255', 1); + // } + $this->$name = $value; + } + + /** + * get color + * + * @param string $name + * @return float + */ + public function __get(string $name): float + { + if (!property_exists($this, $name)) { + throw new \ErrorException('Creation of dynamic property is not allowed', 0); + } + return $this->$name; + } + + /** + * set the colorspace + * + * @param string $colorspace + * @return self + */ + private function setColorspace(string $colorspace): self + { + if (!in_array($colorspace, $this::COLORSPACES)) { + throw new \InvalidArgumentException('Not allowed colorspace', 0); + } + $this->colorspace = $colorspace; + return $this; + } + + /** + * Returns the color as array + * where 0: X, 1: Y, 2: Z + * + * @return array{0:float,1:float,2:float} + */ + public function returnAsArray(): array + { + return [$this->X, $this->Y, $this->Z]; + } + + /** + * set color as array + * where 0: X, 1: Y, 2: Z + * + * @param array{0:float,1:float,2:float} $colors + * @return self + */ + public function setFromArray(array $colors): self + { + $this->__set('X', $colors[0]); + $this->__set('Y', $colors[1]); + $this->__set('Z', $colors[2]); + return $this; + } +} + +// __END__ diff --git a/www/lib/CoreLibs/Convert/Color/Coordinates/XYZD65.php b/www/lib/CoreLibs/Convert/Color/Coordinates/XYZD65.php index ebdf633d..76c96e00 100644 --- a/www/lib/CoreLibs/Convert/Color/Coordinates/XYZD65.php +++ b/www/lib/CoreLibs/Convert/Color/Coordinates/XYZD65.php @@ -16,10 +16,19 @@ namespace CoreLibs\Convert\Color\Coordinates; class XYZD65 { + /** @var array allowed colorspaces */ + private const COLORSPACES = ['XYZ']; + + /** @var float X coordinate */ private float $X = 0.0; + /** @var float Y coordinate */ private float $Y = 0.0; + /** @var float Y coordinate */ private float $Z = 0.0; + /** @var string color space: either ok or cie */ + private string $colorspace = ''; + /** * Color Coordinate Lch * for oklch @@ -28,29 +37,17 @@ class XYZD65 { } - /** - * set with each value as parameters - * - * @param float $X - * @param float $Y - * @param float $Z - * @return self - */ - public static function __constructFromSet(float $X, float $Y, float $Z): self - { - return (new XYZD65())->setAsArray([$X, $Y, $Z]); - } - /** * set from array * where 0: X, 1: Y, 2: Z * - * @param array{0:float,1:float,2:float} $xyzD65 + * @param array{0:float,1:float,2:float} $colors + * @param string $colorspace * @return self */ - public static function __constructFromArray(array $xyzD65): self + public static function __constructFromArray(array $colors, string $colorspace = 'XYZ'): self { - return (new XYZD65())->setAsArray($xyzD65); + return (new XYZD65())->setColorspace($colorspace)->setFromArray($colors); } /** @@ -86,6 +83,21 @@ class XYZD65 return $this->$name; } + /** + * set the colorspace + * + * @param string $colorspace + * @return self + */ + private function setColorspace(string $colorspace): self + { + if (!in_array($colorspace, $this::COLORSPACES)) { + throw new \InvalidArgumentException('Not allowed colorspace', 0); + } + $this->colorspace = $colorspace; + return $this; + } + /** * Returns the color as array * where 0: X, 1: Y, 2: Z @@ -101,14 +113,14 @@ class XYZD65 * set color as array * where 0: X, 1: Y, 2: Z * - * @param array{0:float,1:float,2:float} $xyzD65 + * @param array{0:float,1:float,2:float} $colors * @return self */ - public function setAsArray(array $xyzD65): self + public function setFromArray(array $colors): self { - $this->__set('X', $xyzD65[0]); - $this->__set('Y', $xyzD65[1]); - $this->__set('Z', $xyzD65[2]); + $this->__set('X', $colors[0]); + $this->__set('Y', $colors[1]); + $this->__set('Z', $colors[2]); return $this; } }