|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace PackageFactory\ColorHelper\Domain\ValueObject; |
| 5 | + |
| 6 | +class RgbaColor implements ColorInterface |
| 7 | +{ |
| 8 | + |
| 9 | + /** |
| 10 | + * @var int |
| 11 | + */ |
| 12 | + private $red; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var int |
| 16 | + */ |
| 17 | + private $green; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var int |
| 21 | + */ |
| 22 | + private $blue; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var int |
| 26 | + */ |
| 27 | + private $alpha; |
| 28 | + |
| 29 | + /** |
| 30 | + * RgbColor constructor. |
| 31 | + * @param int $red |
| 32 | + * @param int $green |
| 33 | + * @param int $blue |
| 34 | + * @param int $alpha |
| 35 | + */ |
| 36 | + public function __construct(int $red, int $green, int $blue, int $alpha = 255) |
| 37 | + { |
| 38 | + if ($red < 0 || $red > 255 ) { |
| 39 | + throw new \InvalidArgumentException('argument red has to be an integer between 0 and 255'); |
| 40 | + } |
| 41 | + if ($green < 0 || $green > 255 ) { |
| 42 | + throw new \InvalidArgumentException('argument green has to be an integer between 0 and 255'); |
| 43 | + } |
| 44 | + if ($blue < 0 || $blue > 255 ) { |
| 45 | + throw new \InvalidArgumentException('argument blue has to be an integer between 0 and 255'); |
| 46 | + } |
| 47 | + if ($alpha < 0 || $alpha > 255 ) { |
| 48 | + throw new \InvalidArgumentException('argument alpha has to be an integer between 0 and 255'); |
| 49 | + } |
| 50 | + |
| 51 | + $this->red = $red; |
| 52 | + $this->green = $green; |
| 53 | + $this->blue = $blue; |
| 54 | + $this->alpha = $alpha; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return int |
| 59 | + */ |
| 60 | + public function getRed(): int |
| 61 | + { |
| 62 | + return $this->red; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return int |
| 67 | + */ |
| 68 | + public function getGreen(): int |
| 69 | + { |
| 70 | + return $this->green; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return int |
| 75 | + */ |
| 76 | + public function getBlue(): int |
| 77 | + { |
| 78 | + return $this->blue; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return int |
| 83 | + */ |
| 84 | + public function getAlpha():int |
| 85 | + { |
| 86 | + return $this->alpha; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return string |
| 91 | + */ |
| 92 | + public function getHex():string |
| 93 | + { |
| 94 | + if ($this->alpha == 255) { |
| 95 | + return '#' |
| 96 | + . str_pad(dechex($this->red), 2, '0') |
| 97 | + . str_pad(dechex($this->green), 2, '0') |
| 98 | + . str_pad(dechex($this->blue), 2, '0'); |
| 99 | + } else { |
| 100 | + return '#' |
| 101 | + . str_pad(dechex($this->red), 2, '0') |
| 102 | + . str_pad(dechex($this->green), 2, '0') |
| 103 | + . str_pad(dechex($this->blue), 2, '0') |
| 104 | + . str_pad(dechex($this->alpha), 2, '0'); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return RgbaColor |
| 110 | + */ |
| 111 | + public function asRgba(): RgbaColor |
| 112 | + { |
| 113 | + return $this; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return HslaColor |
| 118 | + */ |
| 119 | + public function asHsla(): HslaColor |
| 120 | + { |
| 121 | + $r = $this->red / 255; |
| 122 | + $g = $this->green / 255; |
| 123 | + $b = $this->blue / 255; |
| 124 | + $Cmax = max($r, $g, $b); |
| 125 | + $Cmin = min($r, $g, $b); |
| 126 | + $lambda = $Cmax - $Cmin; |
| 127 | + $l = ($Cmax + $Cmin) / 2 ; |
| 128 | + |
| 129 | + if ($lambda == 0) { |
| 130 | + $hue = 0; |
| 131 | + } elseif ($Cmax == $r) { |
| 132 | + $hue = 60 * ((($g-$b) / $lambda) % 6); |
| 133 | + } elseif ($Cmax == $g) { |
| 134 | + $hue = 60 * ((($b-$r) / $lambda) + 2); |
| 135 | + } elseif ($Cmax == $b) { |
| 136 | + $hue = 60 * ((($r-$g) / $lambda) + 4); |
| 137 | + } else { |
| 138 | + throw new \UnexpectedValueException('this should never be thrown'); |
| 139 | + } |
| 140 | + |
| 141 | + if ($hue < 0) $hue += 360; |
| 142 | + if ($hue > 359) $hue -= 360; |
| 143 | + |
| 144 | + if ($lambda == 0) { |
| 145 | + $s = 0; |
| 146 | + } else { |
| 147 | + $s = $lambda / ( 1 - abs((2 * $l) - 1)); |
| 148 | + } |
| 149 | + |
| 150 | + $lightness = $l * 100; |
| 151 | + $saturation = $s * 100; |
| 152 | + |
| 153 | + return new HslaColor( |
| 154 | + (int)round($hue), |
| 155 | + (int)round($saturation), |
| 156 | + (int)round($lightness), |
| 157 | + $this->alpha |
| 158 | + ); |
| 159 | + } |
| 160 | +} |
0 commit comments