|
| 1 | +<?php |
| 2 | +namespace PackageFactory\ColorHelper\Eel; |
| 3 | + |
| 4 | +use Neos\Eel\ProtectedContextAwareInterface; |
| 5 | +use PackageFactory\ColorHelper\Library\Mexitek\PHPColors\Color as ColorLibrary; |
| 6 | + |
| 7 | +class Color implements ProtectedContextAwareInterface |
| 8 | +{ |
| 9 | + |
| 10 | + /** |
| 11 | + * Default adjustments for color manipulations |
| 12 | + */ |
| 13 | + const DEFAULT_ADJUST = 10; |
| 14 | + |
| 15 | + private $red; |
| 16 | + private $green; |
| 17 | + private $blue; |
| 18 | + |
| 19 | + private $hue; |
| 20 | + private $saturation; |
| 21 | + private $lightness; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param int $hue |
| 25 | + * @param int $saturation |
| 26 | + * @param int $lightness |
| 27 | + * @return Color |
| 28 | + * @throws \Exception |
| 29 | + */ |
| 30 | + public static function createFromHSL(int $hue, int $saturation, int $lightness): self |
| 31 | + { |
| 32 | + $hue = $hue % 360; |
| 33 | + if ($hue < 0) { |
| 34 | + $hue += 360; |
| 35 | + } |
| 36 | + |
| 37 | + $saturation = self::limitNumber($saturation, 0, 100); |
| 38 | + $lightness = self::limitNumber($lightness, 0, 100); |
| 39 | + |
| 40 | + $hsl = [ |
| 41 | + 'H' => $hue, |
| 42 | + 'S' => $saturation / 100, |
| 43 | + 'L' => $lightness / 100 |
| 44 | + ]; |
| 45 | + $hex = ColorLibrary::hslToHex($hsl); |
| 46 | + $rgb = ColorLibrary::hexToRgb($hex); |
| 47 | + |
| 48 | + $color = new static(); |
| 49 | + $color->red = $rgb['R']; |
| 50 | + $color->green = $rgb['G']; |
| 51 | + $color->blue = $rgb['B']; |
| 52 | + $color->hue = $hue; |
| 53 | + $color->saturation = $saturation; |
| 54 | + $color->lightness = $lightness; |
| 55 | + |
| 56 | + return $color; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param int $red |
| 61 | + * @param int $green |
| 62 | + * @param int $blue |
| 63 | + * @return Color |
| 64 | + * @throws \Exception |
| 65 | + */ |
| 66 | + public static function createFromRgb(int $red, int $green, int $blue): self |
| 67 | + { |
| 68 | + $red = self::limitNumber($red, 0, 255); |
| 69 | + $green = self::limitNumber($green, 0, 255); |
| 70 | + $blue = self::limitNumber($blue, 0, 255); |
| 71 | + |
| 72 | + $rgb = [ |
| 73 | + 'R' => $red, |
| 74 | + 'G' => $green, |
| 75 | + 'B' => $blue |
| 76 | + ]; |
| 77 | + $hex = ColorLibrary::rgbToHex($rgb); |
| 78 | + $hsl = ColorLibrary::hexToHsl($hex); |
| 79 | + |
| 80 | + $color = new static(); |
| 81 | + $color->red = $rgb['R']; |
| 82 | + $color->green = $rgb['G']; |
| 83 | + $color->blue = $rgb['B']; |
| 84 | + $color->hue = round($hsl['H']); |
| 85 | + $color->saturation = round($hsl['S'] * 100); |
| 86 | + $color->lightness = round($hsl['L'] * 100); |
| 87 | + |
| 88 | + return $color; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param string $hex |
| 93 | + * @return Color |
| 94 | + */ |
| 95 | + public static function createFromHex(string $hex): self |
| 96 | + { |
| 97 | + $hsl = ColorLibrary::hexToHsl($hex); |
| 98 | + $rgb = ColorLibrary::hexToRgb($hex); |
| 99 | + |
| 100 | + $color = new static(); |
| 101 | + $color->red = $rgb['R']; |
| 102 | + $color->green = $rgb['G']; |
| 103 | + $color->blue = $rgb['B']; |
| 104 | + $color->hue = round($hsl['H']); |
| 105 | + $color->saturation = round($hsl['S'] * 100); |
| 106 | + $color->lightness = round($hsl['L'] * 100); |
| 107 | + |
| 108 | + return $color; |
| 109 | + } |
| 110 | + |
| 111 | + public function __toString() |
| 112 | + { |
| 113 | + return '#' . ColorLibrary::rgbToHex([ |
| 114 | + 'R' => $this->red, |
| 115 | + 'G' => $this->green, |
| 116 | + 'B' => $this->blue |
| 117 | + ]); |
| 118 | + } |
| 119 | + |
| 120 | + public function getRgb(): array |
| 121 | + { |
| 122 | + return [$this->red, $this->green, $this->blue]; |
| 123 | + } |
| 124 | + |
| 125 | + public function getHsl(): array |
| 126 | + { |
| 127 | + return [$this->hue, $this->saturation, $this->lightness]; |
| 128 | + } |
| 129 | + |
| 130 | + public function getHex(): array |
| 131 | + { |
| 132 | + return $this->__toString(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @param Color $colorA |
| 137 | + * @param Color $colorB |
| 138 | + * @param int $weight between 0 and 100 |
| 139 | + * @return string |
| 140 | + */ |
| 141 | + public function mix(Color $color, int $weight = 50): Color |
| 142 | + { |
| 143 | + list($red, $green, $blue) = $color->getRgb(); |
| 144 | + $weight = $this->limitNumber($weight, 0, 100); |
| 145 | + $factorA = $weight / 100; |
| 146 | + $factorB = 1 - $factorA; |
| 147 | + |
| 148 | + return self::createFromRgb( |
| 149 | + round(($this->red * $factorA) + ($red * $factorB)), |
| 150 | + round(($this->green * $factorA) + ($green * $factorB)), |
| 151 | + round(($this->blue * $factorA) + ($blue * $factorB)), |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @param int $amount between 0 and 100 |
| 157 | + * @return string |
| 158 | + */ |
| 159 | + public function lighten(int $amount = self::DEFAULT_ADJUST ): self |
| 160 | + { |
| 161 | + return self::createFromHSL( |
| 162 | + $this->hue, |
| 163 | + $this->saturation, |
| 164 | + $this->lightness + $amount |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @param int $amount between 0 and 100 |
| 170 | + * @return string |
| 171 | + */ |
| 172 | + public function darken(int $amount = self::DEFAULT_ADJUST ): self |
| 173 | + { |
| 174 | + return self::createFromHSL( |
| 175 | + $this->hue, |
| 176 | + $this->saturation, |
| 177 | + $this->lightness - $amount |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + /** |
| 183 | + * Adjust the value by rotating the hue angle of a color in either direction. |
| 184 | + * |
| 185 | + * @param string $color |
| 186 | + * @param int $amount degrees to rotate the color |
| 187 | + * @return string |
| 188 | + */ |
| 189 | + public function spin(int $amount): self |
| 190 | + { |
| 191 | + return self::createFromHSL( |
| 192 | + $this->hue + $amount, |
| 193 | + $this->saturation, |
| 194 | + $this->lightness |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * @param int $amount to saturate the color |
| 200 | + * @return string |
| 201 | + */ |
| 202 | + public function saturate(int $amount = self::DEFAULT_ADJUST): self |
| 203 | + { |
| 204 | + return self::createFromHSL( |
| 205 | + $this->hue, |
| 206 | + self::limitNumber($this->saturation + $amount, 0, 100), |
| 207 | + $this->lightness |
| 208 | + ); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * @param string $color |
| 213 | + * @param int $amount to desaturate the color |
| 214 | + * @return string |
| 215 | + */ |
| 216 | + public function desaturate(int $amount = self::DEFAULT_ADJUST): self |
| 217 | + { |
| 218 | + return self::createFromHSL( |
| 219 | + $this->hue, |
| 220 | + self::limitNumber($this->saturation - $amount, 0, 100), |
| 221 | + $this->lightness |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * @param int|float|double $value |
| 227 | + * @param int $min |
| 228 | + * @param int $max |
| 229 | + * @return int |
| 230 | + */ |
| 231 | + protected static function limitNumber($value, int $min = 0, int $max = 1) { |
| 232 | + if ($value < $min) { |
| 233 | + return $min; |
| 234 | + } elseif ($value > $max) { |
| 235 | + return $max; |
| 236 | + } else { |
| 237 | + return $value; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * @param string $methodName |
| 243 | + * @return bool |
| 244 | + */ |
| 245 | + public function allowsCallOfMethod($methodName): bool |
| 246 | + { |
| 247 | + return true; |
| 248 | + } |
| 249 | + |
| 250 | +} |
0 commit comments