1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kint\Object\Representation; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use LogicException; |
7
|
|
|
|
8
|
|
|
class ColorRepresentation extends Representation |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
const COLOR_NAME = 1; |
11
|
|
|
const COLOR_HEX_3 = 2; |
12
|
|
|
const COLOR_HEX_6 = 3; |
13
|
|
|
const COLOR_RGB = 4; |
14
|
|
|
const COLOR_RGBA = 5; |
15
|
|
|
const COLOR_HSL = 6; |
16
|
|
|
const COLOR_HSLA = 7; |
17
|
|
|
const COLOR_HEX_4 = 8; |
18
|
|
|
const COLOR_HEX_8 = 9; |
19
|
|
|
|
20
|
|
|
public static $color_map = array( |
|
|
|
|
21
|
|
|
'aliceblue' => 'f0f8ff', |
22
|
|
|
'antiquewhite' => 'faebd7', |
23
|
|
|
'aqua' => '00ffff', |
24
|
|
|
'aquamarine' => '7fffd4', |
25
|
|
|
'azure' => 'f0ffff', |
26
|
|
|
'beige' => 'f5f5dc', |
27
|
|
|
'bisque' => 'ffe4c4', |
28
|
|
|
'black' => '000000', |
29
|
|
|
'blanchedalmond' => 'ffebcd', |
30
|
|
|
'blue' => '0000ff', |
31
|
|
|
'blueviolet' => '8a2be2', |
32
|
|
|
'brown' => 'a52a2a', |
33
|
|
|
'burlywood' => 'deb887', |
34
|
|
|
'cadetblue' => '5f9ea0', |
35
|
|
|
'chartreuse' => '7fff00', |
36
|
|
|
'chocolate' => 'd2691e', |
37
|
|
|
'coral' => 'ff7f50', |
38
|
|
|
'cornflowerblue' => '6495ed', |
39
|
|
|
'cornsilk' => 'fff8dc', |
40
|
|
|
'crimson' => 'dc143c', |
41
|
|
|
'cyan' => '00ffff', |
42
|
|
|
'darkblue' => '00008b', |
43
|
|
|
'darkcyan' => '008b8b', |
44
|
|
|
'darkgoldenrod' => 'b8860b', |
45
|
|
|
'darkgray' => 'a9a9a9', |
46
|
|
|
'darkgreen' => '006400', |
47
|
|
|
'darkgrey' => 'a9a9a9', |
48
|
|
|
'darkkhaki' => 'bdb76b', |
49
|
|
|
'darkmagenta' => '8b008b', |
50
|
|
|
'darkolivegreen' => '556b2f', |
51
|
|
|
'darkorange' => 'ff8c00', |
52
|
|
|
'darkorchid' => '9932cc', |
53
|
|
|
'darkred' => '8b0000', |
54
|
|
|
'darksalmon' => 'e9967a', |
55
|
|
|
'darkseagreen' => '8fbc8f', |
56
|
|
|
'darkslateblue' => '483d8b', |
57
|
|
|
'darkslategray' => '2f4f4f', |
58
|
|
|
'darkslategrey' => '2f4f4f', |
59
|
|
|
'darkturquoise' => '00ced1', |
60
|
|
|
'darkviolet' => '9400d3', |
61
|
|
|
'deeppink' => 'ff1493', |
62
|
|
|
'deepskyblue' => '00bfff', |
63
|
|
|
'dimgray' => '696969', |
64
|
|
|
'dimgrey' => '696969', |
65
|
|
|
'dodgerblue' => '1e90ff', |
66
|
|
|
'firebrick' => 'b22222', |
67
|
|
|
'floralwhite' => 'fffaf0', |
68
|
|
|
'forestgreen' => '228b22', |
69
|
|
|
'fuchsia' => 'ff00ff', |
70
|
|
|
'gainsboro' => 'dcdcdc', |
71
|
|
|
'ghostwhite' => 'f8f8ff', |
72
|
|
|
'gold' => 'ffd700', |
73
|
|
|
'goldenrod' => 'daa520', |
74
|
|
|
'gray' => '808080', |
75
|
|
|
'green' => '008000', |
76
|
|
|
'greenyellow' => 'adff2f', |
77
|
|
|
'grey' => '808080', |
78
|
|
|
'honeydew' => 'f0fff0', |
79
|
|
|
'hotpink' => 'ff69b4', |
80
|
|
|
'indianred' => 'cd5c5c', |
81
|
|
|
'indigo' => '4b0082', |
82
|
|
|
'ivory' => 'fffff0', |
83
|
|
|
'khaki' => 'f0e68c', |
84
|
|
|
'lavender' => 'e6e6fa', |
85
|
|
|
'lavenderblush' => 'fff0f5', |
86
|
|
|
'lawngreen' => '7cfc00', |
87
|
|
|
'lemonchiffon' => 'fffacd', |
88
|
|
|
'lightblue' => 'add8e6', |
89
|
|
|
'lightcoral' => 'f08080', |
90
|
|
|
'lightcyan' => 'e0ffff', |
91
|
|
|
'lightgoldenrodyellow' => 'fafad2', |
92
|
|
|
'lightgray' => 'd3d3d3', |
93
|
|
|
'lightgreen' => '90ee90', |
94
|
|
|
'lightgrey' => 'd3d3d3', |
95
|
|
|
'lightpink' => 'ffb6c1', |
96
|
|
|
'lightsalmon' => 'ffa07a', |
97
|
|
|
'lightseagreen' => '20b2aa', |
98
|
|
|
'lightskyblue' => '87cefa', |
99
|
|
|
'lightslategray' => '778899', |
100
|
|
|
'lightslategrey' => '778899', |
101
|
|
|
'lightsteelblue' => 'b0c4de', |
102
|
|
|
'lightyellow' => 'ffffe0', |
103
|
|
|
'lime' => '00ff00', |
104
|
|
|
'limegreen' => '32cd32', |
105
|
|
|
'linen' => 'faf0e6', |
106
|
|
|
'magenta' => 'ff00ff', |
107
|
|
|
'maroon' => '800000', |
108
|
|
|
'mediumaquamarine' => '66cdaa', |
109
|
|
|
'mediumblue' => '0000cd', |
110
|
|
|
'mediumorchid' => 'ba55d3', |
111
|
|
|
'mediumpurple' => '9370db', |
112
|
|
|
'mediumseagreen' => '3cb371', |
113
|
|
|
'mediumslateblue' => '7b68ee', |
114
|
|
|
'mediumspringgreen' => '00fa9a', |
115
|
|
|
'mediumturquoise' => '48d1cc', |
116
|
|
|
'mediumvioletred' => 'c71585', |
117
|
|
|
'midnightblue' => '191970', |
118
|
|
|
'mintcream' => 'f5fffa', |
119
|
|
|
'mistyrose' => 'ffe4e1', |
120
|
|
|
'moccasin' => 'ffe4b5', |
121
|
|
|
'navajowhite' => 'ffdead', |
122
|
|
|
'navy' => '000080', |
123
|
|
|
'oldlace' => 'fdf5e6', |
124
|
|
|
'olive' => '808000', |
125
|
|
|
'olivedrab' => '6b8e23', |
126
|
|
|
'orange' => 'ffa500', |
127
|
|
|
'orangered' => 'ff4500', |
128
|
|
|
'orchid' => 'da70d6', |
129
|
|
|
'palegoldenrod' => 'eee8aa', |
130
|
|
|
'palegreen' => '98fb98', |
131
|
|
|
'paleturquoise' => 'afeeee', |
132
|
|
|
'palevioletred' => 'db7093', |
133
|
|
|
'papayawhip' => 'ffefd5', |
134
|
|
|
'peachpuff' => 'ffdab9', |
135
|
|
|
'peru' => 'cd853f', |
136
|
|
|
'pink' => 'ffc0cb', |
137
|
|
|
'plum' => 'dda0dd', |
138
|
|
|
'powderblue' => 'b0e0e6', |
139
|
|
|
'purple' => '800080', |
140
|
|
|
'rebeccapurple' => '663399', |
141
|
|
|
'red' => 'ff0000', |
142
|
|
|
'rosybrown' => 'bc8f8f', |
143
|
|
|
'royalblue' => '4169e1', |
144
|
|
|
'saddlebrown' => '8b4513', |
145
|
|
|
'salmon' => 'fa8072', |
146
|
|
|
'sandybrown' => 'f4a460', |
147
|
|
|
'seagreen' => '2e8b57', |
148
|
|
|
'seashell' => 'fff5ee', |
149
|
|
|
'sienna' => 'a0522d', |
150
|
|
|
'silver' => 'c0c0c0', |
151
|
|
|
'skyblue' => '87ceeb', |
152
|
|
|
'slateblue' => '6a5acd', |
153
|
|
|
'slategray' => '708090', |
154
|
|
|
'slategrey' => '708090', |
155
|
|
|
'snow' => 'fffafa', |
156
|
|
|
'springgreen' => '00ff7f', |
157
|
|
|
'steelblue' => '4682b4', |
158
|
|
|
'tan' => 'd2b48c', |
159
|
|
|
'teal' => '008080', |
160
|
|
|
'thistle' => 'd8bfd8', |
161
|
|
|
'tomato' => 'ff6347', |
162
|
|
|
'turquoise' => '40e0d0', |
163
|
|
|
'violet' => 'ee82ee', |
164
|
|
|
'wheat' => 'f5deb3', |
165
|
|
|
'white' => 'ffffff', |
166
|
|
|
'whitesmoke' => 'f5f5f5', |
167
|
|
|
'yellow' => 'ffff00', |
168
|
|
|
'yellowgreen' => '9acd32', |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
public $r = 0; |
|
|
|
|
172
|
|
|
public $g = 0; |
|
|
|
|
173
|
|
|
public $b = 0; |
|
|
|
|
174
|
|
|
public $a = 1; |
|
|
|
|
175
|
|
|
public $variant = null; |
176
|
|
|
public $implicit_label = true; |
|
|
|
|
177
|
|
|
public $hints = array('color'); |
178
|
|
|
|
179
|
|
|
public function getColor($variant = null) |
180
|
|
|
{ |
181
|
|
|
switch ($variant) { |
182
|
|
|
case self::COLOR_NAME: |
183
|
|
|
$hex = sprintf('%02x%02x%02x', $this->r, $this->g, $this->b); |
184
|
|
|
|
185
|
|
|
return array_search($hex, self::$color_map); |
186
|
|
|
case self::COLOR_HEX_3: |
187
|
|
|
if ($this->r % 0x11 === 0 && $this->g % 0x11 === 0 && $this->b % 0x11 === 0) { |
188
|
|
|
return sprintf( |
189
|
|
|
'#%1X%1X%1X', |
190
|
|
|
round($this->r / 0x11), |
191
|
|
|
round($this->g / 0x11), |
192
|
|
|
round($this->b / 0x11) |
193
|
|
|
); |
194
|
|
|
} else { |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
case self::COLOR_HEX_6: |
198
|
|
|
return sprintf('#%02X%02X%02X', $this->r, $this->g, $this->b); |
199
|
|
|
case self::COLOR_RGB: |
200
|
|
|
return sprintf('rgb(%d, %d, %d)', $this->r, $this->g, $this->b); |
201
|
|
View Code Duplication |
case self::COLOR_RGBA: |
|
|
|
|
202
|
|
|
return sprintf('rgba(%d, %d, %d, %s)', $this->r, $this->g, $this->b, round($this->a, 4)); |
203
|
|
|
case self::COLOR_HSL: |
204
|
|
|
$val = self::rgbToHsl($this->r, $this->g, $this->b); |
205
|
|
|
|
206
|
|
|
return vsprintf('hsl(%d, %d%%, %d%%)', $val); |
207
|
|
|
case self::COLOR_HSLA: |
208
|
|
|
$val = self::rgbToHsl($this->r, $this->g, $this->b); |
209
|
|
|
|
210
|
|
|
return sprintf('hsla(%d, %d%%, %d%%, %s)', $val[0], $val[1], $val[2], round($this->a, 4)); |
211
|
|
|
case self::COLOR_HEX_4: |
212
|
|
|
if ($this->r % 0x11 === 0 && $this->g % 0x11 === 0 && $this->b % 0x11 === 0 && ($this->a * 255) % 0x11 === 0) { |
213
|
|
|
return sprintf( |
214
|
|
|
'#%1X%1X%1X%1X', |
215
|
|
|
round($this->r / 0x11), |
216
|
|
|
round($this->g / 0x11), |
217
|
|
|
round($this->b / 0x11), |
218
|
|
|
round($this->a * 0xF) |
219
|
|
|
); |
220
|
|
|
} else { |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
View Code Duplication |
case self::COLOR_HEX_8: |
|
|
|
|
224
|
|
|
return sprintf('#%02X%02X%02X%02X', $this->r, $this->g, $this->b, round($this->a * 0xFF)); |
225
|
|
|
case null: |
226
|
|
|
return $this->contents; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return false; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function __construct($value) |
233
|
|
|
{ |
234
|
|
|
parent::__construct('Color'); |
235
|
|
|
|
236
|
|
|
$this->contents = $value; |
237
|
|
|
$this->setValues($value); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function hasAlpha($variant = null) |
241
|
|
|
{ |
242
|
|
|
if ($variant === null) { |
243
|
|
|
$variant = $this->variant; |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
switch ($variant) { |
247
|
|
|
case self::COLOR_NAME: |
248
|
|
|
return $this->a !== 1; |
249
|
|
|
case self::COLOR_RGBA: |
250
|
|
|
case self::COLOR_HSLA: |
251
|
|
|
case self::COLOR_HEX_4: |
252
|
|
|
case self::COLOR_HEX_8: |
253
|
|
|
return true; |
254
|
|
|
default: |
255
|
|
|
return false; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
protected function setValues($value) |
260
|
|
|
{ |
261
|
|
|
$value = strtolower(trim($value)); |
|
|
|
|
262
|
|
|
// Find out which variant of color input it is |
263
|
|
|
if (isset(self::$color_map[$value])) { |
264
|
|
|
$variant = self::COLOR_NAME; |
265
|
|
|
} elseif ($value[0] === '#') { |
266
|
|
|
$value = substr($value, 1); |
|
|
|
|
267
|
|
|
|
268
|
|
|
if (dechex(hexdec($value)) !== $value) { |
269
|
|
|
return; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
View Code Duplication |
switch (strlen($value)) { |
|
|
|
|
273
|
|
|
case 3: |
274
|
|
|
$variant = self::COLOR_HEX_3; |
275
|
|
|
break; |
276
|
|
|
case 6: |
277
|
|
|
$variant = self::COLOR_HEX_6; |
278
|
|
|
break; |
279
|
|
|
case 4: |
280
|
|
|
$variant = self::COLOR_HEX_4; |
281
|
|
|
break; |
282
|
|
|
case 8: |
283
|
|
|
$variant = self::COLOR_HEX_8; |
284
|
|
|
break; |
285
|
|
|
default: |
286
|
|
|
return; |
287
|
|
|
} |
288
|
|
|
} else { |
289
|
|
|
if (!preg_match('/^((?:rgb|hsl)a?)\s*\(([0-9\.%,\s]+)\)$/i', $value, $match)) { |
290
|
|
|
return; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
View Code Duplication |
switch ($match[1]) { |
|
|
|
|
294
|
|
|
case 'rgb': |
295
|
|
|
$variant = self::COLOR_RGB; |
296
|
|
|
break; |
297
|
|
|
case 'rgba': |
298
|
|
|
$variant = self::COLOR_RGBA; |
299
|
|
|
break; |
300
|
|
|
case 'hsl': |
301
|
|
|
$variant = self::COLOR_HSL; |
302
|
|
|
break; |
303
|
|
|
case 'hsla': |
304
|
|
|
$variant = self::COLOR_HSLA; |
305
|
|
|
break; |
306
|
|
|
default: |
307
|
|
|
return; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$value = explode(',', $match[2]); |
|
|
|
|
311
|
|
|
|
312
|
|
|
if ($this->hasAlpha($variant)) { |
313
|
|
|
if (count($value) !== 4) { |
314
|
|
|
return; |
315
|
|
|
} |
316
|
|
|
} elseif (count($value) !== 3) { |
317
|
|
|
return; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
foreach ($value as $i => &$color) { |
321
|
|
|
$color = trim($color); |
322
|
|
|
|
323
|
|
|
if (strpos($color, '%') !== false) { |
324
|
|
|
$color = str_replace('%', '', $color); |
325
|
|
|
|
326
|
|
|
if ($i === 3) { |
327
|
|
|
$color = $color / 100; |
328
|
|
|
} elseif (in_array($variant, array(self::COLOR_RGB, self::COLOR_RGBA))) { |
329
|
|
|
$color = round($color / 100 * 255); |
330
|
|
|
} elseif ($i === 0 && in_array($variant, array(self::COLOR_HSL, self::COLOR_HSLA))) { |
331
|
|
|
$color = $color / 100 * 360; |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
if ($i === 0 && in_array($variant, array(self::COLOR_HSL, self::COLOR_HSLA))) { |
336
|
|
|
$color = ($color % 360 + 360) % 360; |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
// Assign the correct properties based on the variant |
342
|
|
|
switch ($variant) { |
343
|
|
|
case self::COLOR_HEX_4: |
344
|
|
|
$this->a = hexdec($value[3]) / 0xF; |
|
|
|
|
345
|
|
|
// Fallthrough |
346
|
|
|
case self::COLOR_HEX_3: |
347
|
|
|
$this->r = hexdec($value[0]) * 0x11; |
|
|
|
|
348
|
|
|
$this->g = hexdec($value[1]) * 0x11; |
|
|
|
|
349
|
|
|
$this->b = hexdec($value[2]) * 0x11; |
|
|
|
|
350
|
|
|
break; |
351
|
|
|
case self::COLOR_NAME: |
352
|
|
|
$value = self::$color_map[$value].'FF'; |
|
|
|
|
353
|
|
|
// Fallthrough |
354
|
|
|
case self::COLOR_HEX_8: |
355
|
|
|
$this->a = hexdec(substr($value, 6, 2)) / 0xFF; |
|
|
|
|
356
|
|
|
// Fallthrough |
357
|
|
|
case self::COLOR_HEX_6: |
358
|
|
|
$value = str_split($value, 2); |
|
|
|
|
359
|
|
|
$this->r = hexdec($value[0]); |
|
|
|
|
360
|
|
|
$this->g = hexdec($value[1]); |
|
|
|
|
361
|
|
|
$this->b = hexdec($value[2]); |
|
|
|
|
362
|
|
|
break; |
363
|
|
|
case self::COLOR_RGBA: |
364
|
|
|
$this->a = $value[3]; |
365
|
|
|
// Fallthrough |
366
|
|
|
case self::COLOR_RGB: |
367
|
|
|
list($this->r, $this->g, $this->b) = $value; |
368
|
|
|
break; |
369
|
|
|
case self::COLOR_HSLA: |
370
|
|
|
$this->a = $value[3]; |
371
|
|
|
// Fallthrough |
372
|
|
|
case self::COLOR_HSL: |
373
|
|
|
if (min($value) < 0 || $value[0] > 360 || max($value[1], $value[2]) > 100) { |
374
|
|
|
return; |
375
|
|
|
} |
376
|
|
|
$value = self::hslToRgb($value[0], $value[1], $value[2]); |
|
|
|
|
377
|
|
|
list($this->r, $this->g, $this->b) = $value; |
378
|
|
|
break; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
// If something has gone horribly wrong |
382
|
|
|
if ($this->r > 0xFF || $this->g > 0xFF || $this->b > 0xFF || $this->a > 1) { |
383
|
|
|
$this->variant = null; |
384
|
|
|
} else { |
385
|
|
|
$this->variant = $variant; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Turns HSL color to RGB. Black magic. |
391
|
|
|
* |
392
|
|
|
* @param float $h Hue |
393
|
|
|
* @param float $s Saturation |
394
|
|
|
* @param float $l Lightness |
395
|
|
|
* |
396
|
|
|
* @return array RGB array |
|
|
|
|
397
|
|
|
*/ |
398
|
|
|
public static function hslToRgb($h, $s, $l) |
|
|
|
|
399
|
|
|
{ |
400
|
|
|
if (min($h, $s, $l) < 0) { |
401
|
|
|
throw new InvalidArgumentException('The parameters for hslToRgb should be no less than 0'); |
402
|
|
|
} elseif ($h > 360 || max($s, $l) > 100) { |
403
|
|
|
throw new InvalidArgumentException('The parameters for hslToRgb should be no more than 360, 100, and 100 respectively'); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
$h /= 360; |
407
|
|
|
$s /= 100; |
408
|
|
|
$l /= 100; |
409
|
|
|
|
410
|
|
|
$m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l * $s; |
|
|
|
|
411
|
|
|
$m1 = $l * 2 - $m2; |
|
|
|
|
412
|
|
|
|
413
|
|
|
$out = array( |
414
|
|
|
round(self::hueToRgb($m1, $m2, $h + 1 / 3) * 255), |
415
|
|
|
round(self::hueToRgb($m1, $m2, $h) * 255), |
416
|
|
|
round(self::hueToRgb($m1, $m2, $h - 1 / 3) * 255), |
417
|
|
|
); |
418
|
|
|
|
419
|
|
|
if (max($out) > 255) { |
420
|
|
|
throw new LogicException('hslToRbg return value should not contain values above 255'); |
421
|
|
|
} else { |
422
|
|
|
return $out; |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Helper function for hslToRgb. Even blacker magic. |
428
|
|
|
* |
429
|
|
|
* @return float Color value |
430
|
|
|
*/ |
431
|
|
|
private static function hueToRgb($m1, $m2, $hue) |
|
|
|
|
432
|
|
|
{ |
433
|
|
|
$hue = ($hue < 0) ? $hue + 1 : (($hue > 1) ? $hue - 1 : $hue); |
|
|
|
|
434
|
|
View Code Duplication |
if ($hue * 6 < 1) { |
|
|
|
|
435
|
|
|
return $m1 + ($m2 - $m1) * $hue * 6; |
436
|
|
|
} |
437
|
|
|
if ($hue * 2 < 1) { |
438
|
|
|
return $m2; |
439
|
|
|
} |
440
|
|
View Code Duplication |
if ($hue * 3 < 2) { |
|
|
|
|
441
|
|
|
return $m1 + ($m2 - $m1) * (0.66666 - $hue) * 6; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
return $m1; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Converts RGB to HSL. Color inversion of previous black magic is white magic? |
449
|
|
|
* |
450
|
|
|
* @param float $red Red |
451
|
|
|
* @param float $green Green |
452
|
|
|
* @param float $blue Blue |
453
|
|
|
* |
454
|
|
|
* @return array HSL array |
|
|
|
|
455
|
|
|
*/ |
456
|
|
|
public static function rgbToHsl($red, $green, $blue) |
457
|
|
|
{ |
458
|
|
|
if (min($red, $green, $blue) < 0) { |
459
|
|
|
throw new InvalidArgumentException('The parameters for rgbToHsl should be no less than 0'); |
460
|
|
|
} elseif (max($red, $green, $blue) > 255) { |
461
|
|
|
throw new InvalidArgumentException('The parameters for rgbToHsl should be no more than 255'); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
$clrMin = min($red, $green, $blue); |
465
|
|
|
$clrMax = max($red, $green, $blue); |
466
|
|
|
$deltaMax = $clrMax - $clrMin; |
467
|
|
|
|
468
|
|
|
$L = ($clrMax + $clrMin) / 510; |
|
|
|
|
469
|
|
|
|
470
|
|
|
if (0 == $deltaMax) { |
471
|
|
|
$H = 0; |
|
|
|
|
472
|
|
|
$S = 0; |
|
|
|
|
473
|
|
|
} else { |
474
|
|
|
if (0.5 > $L) { |
|
|
|
|
475
|
|
|
$S = $deltaMax / ($clrMax + $clrMin); |
|
|
|
|
476
|
|
|
} else { |
477
|
|
|
$S = $deltaMax / (510 - $clrMax - $clrMin); |
|
|
|
|
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
if ($clrMax == $red) { |
481
|
|
|
$H = ($green - $blue) / (6.0 * $deltaMax); |
|
|
|
|
482
|
|
|
} elseif ($clrMax == $green) { |
483
|
|
|
$H = 1 / 3 + ($blue - $red) / (6.0 * $deltaMax); |
|
|
|
|
484
|
|
|
} else { |
485
|
|
|
$H = 2 / 3 + ($red - $green) / (6.0 * $deltaMax); |
|
|
|
|
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
if (0 > $H) { |
|
|
|
|
489
|
|
|
$H += 1; |
|
|
|
|
490
|
|
|
} |
491
|
|
|
if (1 < $H) { |
|
|
|
|
492
|
|
|
$H -= 1; |
|
|
|
|
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
return array( |
497
|
|
|
(round($H * 360) % 360 + 360) % 360, |
|
|
|
|
498
|
|
|
round($S * 100), |
|
|
|
|
499
|
|
|
round($L * 100), |
|
|
|
|
500
|
|
|
); |
501
|
|
|
} |
502
|
|
|
} |
503
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.