1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kint\Test\Object\Representation; |
4
|
|
|
|
5
|
|
|
use Kint\Object\Representation\ColorRepresentation; |
6
|
|
|
use Kint\Test\KintTestCase; |
7
|
|
|
|
8
|
|
|
class ColorRepresentationTest extends KintTestCase |
9
|
|
|
{ |
10
|
|
|
public function colorProvider() |
11
|
|
|
{ |
12
|
|
|
return array( |
13
|
|
|
'name 1' => array( |
14
|
|
|
'red', |
15
|
|
|
0xFF, |
16
|
|
|
0, |
17
|
|
|
0, |
18
|
|
|
1.0, |
19
|
|
|
ColorRepresentation::COLOR_NAME, |
20
|
|
|
), |
21
|
|
|
'name 2' => array( |
22
|
|
|
'fuchsia', |
23
|
|
|
0xFF, |
24
|
|
|
0, |
25
|
|
|
0xFF, |
26
|
|
|
1.0, |
27
|
|
|
ColorRepresentation::COLOR_NAME, |
28
|
|
|
), |
29
|
|
|
'name 3' => array( |
30
|
|
|
'BLUE', |
31
|
|
|
0, |
32
|
|
|
0, |
33
|
|
|
0xFF, |
34
|
|
|
1.0, |
35
|
|
|
ColorRepresentation::COLOR_NAME, |
36
|
|
|
), |
37
|
|
|
'name 4' => array( |
38
|
|
|
' white ', |
39
|
|
|
0xFF, |
40
|
|
|
0xFF, |
41
|
|
|
0xFF, |
42
|
|
|
1.0, |
43
|
|
|
ColorRepresentation::COLOR_NAME, |
44
|
|
|
), |
45
|
|
|
'name 5' => array( |
46
|
|
|
'transparent', |
47
|
|
|
0, |
48
|
|
|
0, |
49
|
|
|
0, |
50
|
|
|
0.0, |
51
|
|
|
ColorRepresentation::COLOR_NAME, |
52
|
|
|
), |
53
|
|
|
'hex-3' => array( |
54
|
|
|
'#FED', |
55
|
|
|
0xFF, |
56
|
|
|
0xEE, |
57
|
|
|
0xDD, |
58
|
|
|
1.0, |
59
|
|
|
ColorRepresentation::COLOR_HEX_3, |
60
|
|
|
), |
61
|
|
|
'hex-6' => array( |
62
|
|
|
'#F00BA8', |
63
|
|
|
0xF0, |
64
|
|
|
0x0B, |
65
|
|
|
0xA8, |
66
|
|
|
1.0, |
67
|
|
|
ColorRepresentation::COLOR_HEX_6, |
68
|
|
|
), |
69
|
|
|
'rgb' => array( |
70
|
|
|
'rgb(123, 45, 67)', |
71
|
|
|
123, |
72
|
|
|
45, |
73
|
|
|
67, |
74
|
|
|
1.0, |
75
|
|
|
ColorRepresentation::COLOR_RGB, |
76
|
|
|
), |
77
|
|
|
'rgb alpha' => array( |
78
|
|
|
'rgb(123, 45, 67, 0.5)', |
79
|
|
|
123, |
80
|
|
|
45, |
81
|
|
|
67, |
82
|
|
|
0.5, |
83
|
|
|
ColorRepresentation::COLOR_RGB, |
84
|
|
|
), |
85
|
|
|
'rgb alpha percent' => array( |
86
|
|
|
'rgb(50%, 60%, 70%, 50%)', |
87
|
|
|
128, |
88
|
|
|
153, |
89
|
|
|
179, |
90
|
|
|
0.5, |
91
|
|
|
ColorRepresentation::COLOR_RGB, |
92
|
|
|
), |
93
|
|
|
'rgb alpha whitespace' => array( |
94
|
|
|
' rgb ( 123 45 67 / 0.5 ) ', |
95
|
|
|
123, |
96
|
|
|
45, |
97
|
|
|
67, |
98
|
|
|
0.5, |
99
|
|
|
ColorRepresentation::COLOR_RGB, |
100
|
|
|
), |
101
|
|
|
'rgb uppercase' => array( |
102
|
|
|
'RGB(1, 2, 3)', |
103
|
|
|
1, |
104
|
|
|
2, |
105
|
|
|
3, |
106
|
|
|
1.0, |
107
|
|
|
ColorRepresentation::COLOR_RGB, |
108
|
|
|
), |
109
|
|
|
'rgba' => array( |
110
|
|
|
'rgba(123, 45, 67, 0.5)', |
111
|
|
|
123, |
112
|
|
|
45, |
113
|
|
|
67, |
114
|
|
|
0.5, |
115
|
|
|
ColorRepresentation::COLOR_RGBA, |
116
|
|
|
), |
117
|
|
|
'rgba without alpha' => array( |
118
|
|
|
'rgba(123, 45, 67)', |
119
|
|
|
123, |
120
|
|
|
45, |
121
|
|
|
67, |
122
|
|
|
1.0, |
123
|
|
|
ColorRepresentation::COLOR_RGBA, |
124
|
|
|
), |
125
|
|
|
'hsl' => array( |
126
|
|
|
'hsl(120, 100%, 50%)', |
127
|
|
|
0, |
128
|
|
|
0xFF, |
129
|
|
|
0, |
130
|
|
|
1.0, |
131
|
|
|
ColorRepresentation::COLOR_HSL, |
132
|
|
|
), |
133
|
|
|
'hsl alpha' => array( |
134
|
|
|
'hsl(120, 100%, 50%, 0.35)', |
135
|
|
|
0, |
136
|
|
|
0xFF, |
137
|
|
|
0, |
138
|
|
|
0.35, |
139
|
|
|
ColorRepresentation::COLOR_HSL, |
140
|
|
|
), |
141
|
|
|
'hsl alpha percent' => array( |
142
|
|
|
'hsl(120, 100%, 50%, 35%)', |
143
|
|
|
0, |
144
|
|
|
0xFF, |
145
|
|
|
0, |
146
|
|
|
0.35, |
147
|
|
|
ColorRepresentation::COLOR_HSL, |
148
|
|
|
), |
149
|
|
|
'hsl alpha whitespace' => array( |
150
|
|
|
' hsl ( 120 100% 50% / 0.35 ) ', |
151
|
|
|
0, |
152
|
|
|
0xFF, |
153
|
|
|
0, |
154
|
|
|
0.35, |
155
|
|
|
ColorRepresentation::COLOR_HSL, |
156
|
|
|
), |
157
|
|
|
'hsl hue overflow' => array( |
158
|
|
|
'hsl(480, 100%, 50%)', |
159
|
|
|
0, |
160
|
|
|
0xFF, |
161
|
|
|
0, |
162
|
|
|
1.0, |
163
|
|
|
ColorRepresentation::COLOR_HSL, |
164
|
|
|
), |
165
|
|
|
'hsl hue underflow' => array( |
166
|
|
|
'hsl(-240, 100%, 50%)', |
167
|
|
|
0, |
168
|
|
|
0xFF, |
169
|
|
|
0, |
170
|
|
|
1.0, |
171
|
|
|
ColorRepresentation::COLOR_HSL, |
172
|
|
|
), |
173
|
|
|
'hsla' => array( |
174
|
|
|
'hsla(120, 100%, 50%, 35%)', |
175
|
|
|
0, |
176
|
|
|
0xFF, |
177
|
|
|
0, |
178
|
|
|
0.35, |
179
|
|
|
ColorRepresentation::COLOR_HSLA, |
180
|
|
|
), |
181
|
|
|
'hsla without alpha' => array( |
182
|
|
|
'hsla(120, 100%, 50%)', |
183
|
|
|
0, |
184
|
|
|
0xFF, |
185
|
|
|
0, |
186
|
|
|
1.0, |
187
|
|
|
ColorRepresentation::COLOR_HSLA, |
188
|
|
|
), |
189
|
|
|
'hex-4' => array( |
190
|
|
|
'#FA84', |
191
|
|
|
0xFF, |
192
|
|
|
0xAA, |
193
|
|
|
0x88, |
194
|
|
|
0x4 / 0xF, |
195
|
|
|
ColorRepresentation::COLOR_HEX_4, |
196
|
|
|
), |
197
|
|
|
'hex-8' => array( |
198
|
|
|
'#FEDCBA98', |
199
|
|
|
0xFE, |
200
|
|
|
0xDC, |
201
|
|
|
0xBA, |
202
|
|
|
0x98 / 0xFF, |
203
|
|
|
ColorRepresentation::COLOR_HEX_8, |
204
|
|
|
), |
205
|
|
|
'invalid' => array( |
206
|
|
|
"Wait a minute... This isn't a color!", |
207
|
|
|
0, |
208
|
|
|
0, |
209
|
|
|
0, |
210
|
|
|
1.0, |
211
|
|
|
null, |
212
|
|
|
), |
213
|
|
|
'invalid hex' => array( |
214
|
|
|
'#colorsarecool', |
215
|
|
|
0, |
216
|
|
|
0, |
217
|
|
|
0, |
218
|
|
|
1.0, |
219
|
|
|
null, |
220
|
|
|
), |
221
|
|
|
'invalid func 1' => array( |
222
|
|
|
'rgb()', |
223
|
|
|
0, |
224
|
|
|
0, |
225
|
|
|
0, |
226
|
|
|
1.0, |
227
|
|
|
null, |
228
|
|
|
), |
229
|
|
|
'invalid func 2' => array( |
230
|
|
|
'rgb(1, 2, 3, 4, 5)', |
231
|
|
|
0, |
232
|
|
|
0, |
233
|
|
|
0, |
234
|
|
|
1.0, |
235
|
|
|
null, |
236
|
|
|
), |
237
|
|
|
'invalid range' => array( |
238
|
|
|
'rgb(300, 300, 300)', |
239
|
|
|
0, |
240
|
|
|
0, |
241
|
|
|
0, |
242
|
|
|
1.0, |
243
|
|
|
null, |
244
|
|
|
), |
245
|
|
|
'invalid alpha range' => array( |
246
|
|
|
'rgb(0, 0, 0, 2)', |
247
|
|
|
0, |
248
|
|
|
0, |
249
|
|
|
0, |
250
|
|
|
1.0, |
251
|
|
|
null, |
252
|
|
|
), |
253
|
|
|
'invalid hsl range' => array( |
254
|
|
|
'hsl(0, 120%, 120%)', |
255
|
|
|
0, |
256
|
|
|
0, |
257
|
|
|
0, |
258
|
|
|
1.0, |
259
|
|
|
null, |
260
|
|
|
), |
261
|
|
|
'invalid hsl alpha range' => array( |
262
|
|
|
'hsl(0, 0%, 0%, 2)', |
263
|
|
|
0, |
264
|
|
|
0, |
265
|
|
|
0, |
266
|
|
|
1.0, |
267
|
|
|
null, |
268
|
|
|
), |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::__construct |
274
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::setValues |
275
|
|
|
* @dataProvider colorProvider |
276
|
|
|
*/ |
277
|
|
|
public function testConstruct($input, $r, $g, $b, $a, $variant) |
|
|
|
|
278
|
|
|
{ |
279
|
|
|
$rep = new ColorRepresentation($input); |
280
|
|
|
$this->assertSame($r, $rep->r); |
281
|
|
|
$this->assertSame($g, $rep->g); |
282
|
|
|
$this->assertSame($b, $rep->b); |
283
|
|
|
$this->assertSame($a, $rep->a); |
284
|
|
|
$this->assertSame($variant, $rep->variant); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::getColor |
289
|
|
|
*/ |
290
|
|
|
public function testGetColor() |
291
|
|
|
{ |
292
|
|
|
$rep = new ColorRepresentation("This isn't a color"); |
293
|
|
|
$this->assertFalse($rep->getColor()); |
294
|
|
|
|
295
|
|
|
$rep = new ColorRepresentation('#FEDC'); |
296
|
|
|
$this->assertSame('#FEDC', $rep->getColor()); |
297
|
|
|
$this->assertFalse($rep->getColor(ColorRepresentation::COLOR_NAME)); |
298
|
|
|
$this->assertSame('#FED', $rep->getColor(ColorRepresentation::COLOR_HEX_3)); |
299
|
|
|
$this->assertSame('#FFEEDD', $rep->getColor(ColorRepresentation::COLOR_HEX_6)); |
300
|
|
|
$this->assertSame('rgb(255, 238, 221, 0.8)', $rep->getColor(ColorRepresentation::COLOR_RGB)); |
301
|
|
|
$this->assertSame('rgba(255, 238, 221, 0.8)', $rep->getColor(ColorRepresentation::COLOR_RGBA)); |
302
|
|
|
$this->assertSame('hsl(30, 100%, 93%, 0.8)', $rep->getColor(ColorRepresentation::COLOR_HSL)); |
303
|
|
|
$this->assertSame('hsla(30, 100%, 93%, 0.8)', $rep->getColor(ColorRepresentation::COLOR_HSLA)); |
304
|
|
|
$this->assertSame('#FEDC', $rep->getColor(ColorRepresentation::COLOR_HEX_4)); |
305
|
|
|
$this->assertSame('#FFEEDDCC', $rep->getColor(ColorRepresentation::COLOR_HEX_8)); |
306
|
|
|
|
307
|
|
|
$rep = new ColorRepresentation('#FED'); |
308
|
|
|
$this->assertSame('#FED', $rep->getColor()); |
309
|
|
|
$this->assertFalse($rep->getColor(ColorRepresentation::COLOR_NAME)); |
310
|
|
|
$this->assertSame('#FED', $rep->getColor(ColorRepresentation::COLOR_HEX_3)); |
311
|
|
|
$this->assertSame('#FFEEDD', $rep->getColor(ColorRepresentation::COLOR_HEX_6)); |
312
|
|
|
$this->assertSame('rgb(255, 238, 221)', $rep->getColor(ColorRepresentation::COLOR_RGB)); |
313
|
|
|
$this->assertSame('rgba(255, 238, 221, 1)', $rep->getColor(ColorRepresentation::COLOR_RGBA)); |
314
|
|
|
$this->assertSame('hsl(30, 100%, 93%)', $rep->getColor(ColorRepresentation::COLOR_HSL)); |
315
|
|
|
$this->assertSame('hsla(30, 100%, 93%, 1)', $rep->getColor(ColorRepresentation::COLOR_HSLA)); |
316
|
|
|
$this->assertSame('#FEDF', $rep->getColor(ColorRepresentation::COLOR_HEX_4)); |
317
|
|
|
$this->assertSame('#FFEEDDFF', $rep->getColor(ColorRepresentation::COLOR_HEX_8)); |
318
|
|
|
|
319
|
|
|
$rep = new ColorRepresentation('rgba(1, 2, 3, 0.4)'); |
320
|
|
|
$this->assertFalse($rep->getColor(ColorRepresentation::COLOR_HEX_3)); |
321
|
|
|
$this->assertFalse($rep->getColor(ColorRepresentation::COLOR_HEX_4)); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::hasAlpha |
326
|
|
|
*/ |
327
|
|
|
public function testHasAlpha() |
328
|
|
|
{ |
329
|
|
|
$rep = new ColorRepresentation('#FEDC'); |
330
|
|
|
$this->assertTrue($rep->hasAlpha()); |
331
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_NAME)); |
332
|
|
|
$this->assertFalse($rep->hasAlpha(ColorRepresentation::COLOR_HEX_3)); |
333
|
|
|
$this->assertFalse($rep->hasAlpha(ColorRepresentation::COLOR_HEX_6)); |
334
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_RGB)); |
335
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_HSL)); |
336
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_RGBA)); |
337
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_HSLA)); |
338
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_HEX_4)); |
339
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_HEX_8)); |
340
|
|
|
|
341
|
|
|
$rep = new ColorRepresentation('#FED'); |
342
|
|
|
$this->assertFalse($rep->hasAlpha()); |
343
|
|
|
$this->assertFalse($rep->hasAlpha(ColorRepresentation::COLOR_NAME)); |
344
|
|
|
$this->assertFalse($rep->hasAlpha(ColorRepresentation::COLOR_HEX_3)); |
345
|
|
|
$this->assertFalse($rep->hasAlpha(ColorRepresentation::COLOR_HEX_6)); |
346
|
|
|
$this->assertFalse($rep->hasAlpha(ColorRepresentation::COLOR_RGB)); |
347
|
|
|
$this->assertFalse($rep->hasAlpha(ColorRepresentation::COLOR_HSL)); |
348
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_RGBA)); |
349
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_HSLA)); |
350
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_HEX_4)); |
351
|
|
|
$this->assertTrue($rep->hasAlpha(ColorRepresentation::COLOR_HEX_8)); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::hslToRgb |
356
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::hueToRgb |
357
|
|
|
*/ |
358
|
|
View Code Duplication |
public function testHslToRgb() |
|
|
|
|
359
|
|
|
{ |
360
|
|
|
$this->assertSame(array(0, 255, 0), ColorRepresentation::hslToRgb(120, 100, 50)); |
361
|
|
|
$this->assertSame(array(255, 255, 255), ColorRepresentation::hslToRgb(120, 100, 100)); |
362
|
|
|
$this->assertSame(array(0, 0, 0), ColorRepresentation::hslToRgb(120, 100, 0)); |
363
|
|
|
$this->assertSame(array(255, 0, 0), ColorRepresentation::hslToRgb(0, 100, 50)); |
364
|
|
|
$this->assertSame(array(255, 128, 128), ColorRepresentation::hslToRgb(0, 100, 75)); |
365
|
|
|
$this->assertSame(array(255, 0, 0), ColorRepresentation::hslToRgb(360, 100, 50)); |
366
|
|
|
|
367
|
|
|
// Hue between 50% and 66% |
368
|
|
|
$this->assertSame(array(0, 170, 255), ColorRepresentation::hslToRgb(200, 100, 50)); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::rgbToHsl |
373
|
|
|
*/ |
374
|
|
View Code Duplication |
public function testRgbToHsl() |
|
|
|
|
375
|
|
|
{ |
376
|
|
|
$this->assertSame(array(0.0, 100.0, 50.0), ColorRepresentation::rgbToHsl(255, 0, 0)); |
377
|
|
|
$this->assertSame(array(120.0, 100.0, 50.0), ColorRepresentation::rgbToHsl(0, 255, 0)); |
378
|
|
|
$this->assertSame(array(240.0, 100.0, 50.0), ColorRepresentation::rgbToHsl(0, 0, 255)); |
379
|
|
|
$this->assertSame(array(0.0, 0.0, 0.0), ColorRepresentation::rgbToHsl(0, 0, 0)); |
380
|
|
|
$this->assertSame(array(0.0, 0.0, 100.0), ColorRepresentation::rgbToHsl(255, 255, 255)); |
381
|
|
|
|
382
|
|
|
// Lightness below half |
383
|
|
|
$this->assertSame(array(30.0, 100.0, 10.0), ColorRepresentation::rgbToHsl(51, 25.5, 0)); |
384
|
|
|
|
385
|
|
|
// Hue below 0 |
386
|
|
|
$this->assertSame(array(300.0, 100.0, 50.0), ColorRepresentation::rgbToHsl(255, 0, 255)); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::rgbToHsl |
391
|
|
|
* @expectedException \InvalidArgumentException |
392
|
|
|
*/ |
393
|
|
|
public function testRgbToHslInputLow() |
394
|
|
|
{ |
395
|
|
|
ColorRepresentation::rgbToHsl(0, 0, -1); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::rgbToHsl |
400
|
|
|
* @expectedException \InvalidArgumentException |
401
|
|
|
*/ |
402
|
|
|
public function testRgbToHslInputHigh() |
403
|
|
|
{ |
404
|
|
|
ColorRepresentation::rgbToHsl(0, 0, 256); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::hslToRgb |
409
|
|
|
* @expectedException \InvalidArgumentException |
410
|
|
|
*/ |
411
|
|
|
public function testHslToRgbHueHigh() |
412
|
|
|
{ |
413
|
|
|
ColorRepresentation::hslToRgb(361, 0, 0); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::hslToRgb |
418
|
|
|
* @expectedException \InvalidArgumentException |
419
|
|
|
*/ |
420
|
|
|
public function testHslToRgbSatLightHigh() |
421
|
|
|
{ |
422
|
|
|
ColorRepresentation::hslToRgb(0, 101, 101); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* @covers \Kint\Object\Representation\ColorRepresentation::hslToRgb |
427
|
|
|
* @expectedException \InvalidArgumentException |
428
|
|
|
*/ |
429
|
|
|
public function testHslToRgbInputLow() |
430
|
|
|
{ |
431
|
|
|
ColorRepresentation::hslToRgb(-1, 0, 0); |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.