1 | import * as assert from 'assert';
|
2 | import { color, hsl, hsla, rgb, rgba } from '../src/color';
|
3 |
|
4 | const cyan = rgb(0, 255, 255);
|
5 | const blue = rgb(0, 0, 255);
|
6 | const green = rgb(0, 128, 0);
|
7 | const maroon = rgb(128, 0, 0);
|
8 | const black = rgb(0, 0, 0);
|
9 | const white = rgb(255, 255, 255);
|
10 | const red = rgb(255, 0, 0);
|
11 | const transparent = rgba(0, 0, 0, 0);
|
12 | const purple = rgb(128, 0, 128);
|
13 |
|
14 | describe('color', () => {
|
15 | describe('toString()', () => {
|
16 | it('handles named colors', () => {
|
17 | const c1 = red.toString();
|
18 | assert.equal(c1, 'rgb(255, 0, 0)');
|
19 | });
|
20 |
|
21 | it('handles transparency', () => {
|
22 | const c1 = transparent.toString();
|
23 | assert.equal(c1, 'rgba(0, 0, 0, 0)');
|
24 | });
|
25 |
|
26 | it('truncates alpha channel values to the 5th decimal', () => {
|
27 | const c1 = rgba(255, 242, 216, 0.49);
|
28 | assert.equal(c1.toString(), 'rgba(255, 242, 216, 0.49)');
|
29 | });
|
30 |
|
31 | it('truncates alpha channel repeating values to the 5th decimal', () => {
|
32 | const c1 = rgba(255, 242, 216, 1 / 3);
|
33 | assert.equal(c1.toString(), 'rgba(255, 242, 216, 0.33333)');
|
34 | });
|
35 | });
|
36 |
|
37 | describe('rgb()', () => {
|
38 | it('handles rgb with numbers', () => {
|
39 | const color = rgb(255, 0, 0).toString();
|
40 | assert.equal(color, 'rgb(255, 0, 0)');
|
41 | });
|
42 |
|
43 | it('rounds channels to the nearest integer', () => {
|
44 | const color = rgb(100.5, 100.3, -1).toString();
|
45 | assert.equal(color, 'rgb(101, 100, 0)');
|
46 | });
|
47 |
|
48 | it('uses the alpha channel if specified.', () => {
|
49 | const color = rgb(255, 0, 0, 0.5).toString();
|
50 | assert.equal(color, 'rgba(255, 0, 0, 0.5)');
|
51 | });
|
52 | });
|
53 |
|
54 | describe('rgba()', () => {
|
55 | it('handles rgba with numbers', () => {
|
56 | const color = rgba(255, 0, 0, 1).toString();
|
57 | assert.equal(color, 'rgba(255, 0, 0, 1)');
|
58 | });
|
59 |
|
60 | it('handles rgba with percent string', () => {
|
61 | const color = rgba(255, 0, 0, '80%').toString();
|
62 | assert.equal(color, 'rgba(255, 0, 0, 0.8)');
|
63 | });
|
64 | it('rounds channels to the nearest integer', () => {
|
65 | const color = rgba(100.5, 100.3, -1, 0.5).toString();
|
66 | assert.equal(color, 'rgba(101, 100, 0, 0.5)');
|
67 | });
|
68 | });
|
69 |
|
70 | describe('hsl()', () => {
|
71 | it('handles negative hues', () => {
|
72 | const color1 = hsl(-180, 1, 0.5).toString();
|
73 | const color2 = hsl(180, 1, 0.5).toString();
|
74 | assert.equal(color1, color2);
|
75 | });
|
76 |
|
77 | it('handles out of range hues', () => {
|
78 | const color1 = hsl(90, 1, 0.5).toString();
|
79 | const color2 = hsl(360 + 90, 1, 0.5).toString();
|
80 | const color3 = hsl(-360 - 270, 1, 0.5).toString();
|
81 | assert.equal(color1, color2);
|
82 | assert.equal(color1, color3);
|
83 | });
|
84 |
|
85 | it('handles hsl with percent strings', () => {
|
86 | const color = hsl(0, '100%', '50%').toString();
|
87 | assert.equal(color, 'hsl(0, 100%, 50%)');
|
88 | });
|
89 |
|
90 | it('handles hsl with percent numbers', () => {
|
91 | const color = hsl(0, 1, 0.5).toString();
|
92 | assert.equal(color, 'hsl(0, 100%, 50%)');
|
93 | });
|
94 | it('rounds hue to the nearest integer', () => {
|
95 | const color = hsl(100.5, 0.5, 0.5).toString();
|
96 | assert.equal(color, 'hsl(101, 50%, 50%)');
|
97 | });
|
98 | it('uses the alpha channel if specified.', () => {
|
99 | const color = hsl(100.5, 0.5, 0.5, 0.5).toString();
|
100 | assert.equal(color, 'hsla(101, 50%, 50%, 0.5)');
|
101 | });
|
102 | });
|
103 |
|
104 | describe('hsla()', () => {
|
105 | it('handles hsla with percent numbers', () => {
|
106 | const color = hsla(0, 1, 0.5, 0.1).toString();
|
107 | assert.equal(color, 'hsla(0, 100%, 50%, 0.1)');
|
108 | });
|
109 |
|
110 | it('handles hsla with percent strings', () => {
|
111 | const color = hsla(0, '100%', '50%', 0.1).toString();
|
112 | assert.equal(color, 'hsla(0, 100%, 50%, 0.1)');
|
113 | });
|
114 |
|
115 | it('rounds hue to the nearest integer', () => {
|
116 | const color = hsla(100.5, 0.5, 0.5, 0).toString();
|
117 | assert.equal(color, 'hsla(101, 50%, 50%, 0)');
|
118 | });
|
119 | });
|
120 |
|
121 | it('converts from a named color to rgb', () => {
|
122 | const color1 = red.toString();
|
123 | const color2 = rgb(255, 0, 0).toString();
|
124 | assert.equal(color1, color2);
|
125 | });
|
126 |
|
127 | describe('color()', () => {
|
128 | it('converts from a hex color (3 digit) to rgb', () => {
|
129 | const color1 = color('#FF0000').toString();
|
130 | const color2 = rgb(255, 0, 0).toString();
|
131 | assert.equal(color1, color2);
|
132 | });
|
133 |
|
134 | it('converts from a hex color (6 digit) to rgb', () => {
|
135 | const color1 = color('#F00').toString();
|
136 | const color2 = rgb(255, 0, 0).toString();
|
137 | assert.equal(color1, color2);
|
138 | });
|
139 |
|
140 | it('converts from a rgb color', () => {
|
141 | const color1 = color('rgb(255,0,0)').toString();
|
142 | const color2 = rgb(255, 0, 0).toString();
|
143 | assert.equal(color1, color2);
|
144 | });
|
145 |
|
146 | it('converts from a rgba color', () => {
|
147 | const color1 = color('rgba(0,0,255,.1)').toString();
|
148 | const color2 = rgba(0, 0, 255, 0.1).toString();
|
149 | assert.equal(color1, color2);
|
150 | });
|
151 |
|
152 | it('converts from a hsl color', () => {
|
153 | const color1 = color('hsl(345,100%,5%)').toString();
|
154 | const color2 = hsl(345, 1, 0.05).toString();
|
155 | assert.equal(color1, color2);
|
156 | });
|
157 |
|
158 | it('converts from a hsla color', () => {
|
159 | const color1 = color('hsla(10, 10%, 60%, .85)').toString();
|
160 | const color2 = hsla(10, 0.1, 0.6, 0.85).toString();
|
161 | assert.equal(color1, color2);
|
162 | });
|
163 | });
|
164 |
|
165 | describe('toHexString()', () => {
|
166 | it('converts white to #FFFFFF', () => {
|
167 | const color = white.toHexString();
|
168 | assert.equal(color, '#FFFFFF');
|
169 | });
|
170 | it('converts black to #000000', () => {
|
171 | const color = black.toHexString();
|
172 | assert.equal(color, '#000000');
|
173 | });
|
174 | it('converts red to #FF0000', () => {
|
175 | const color = red.toHexString();
|
176 | assert.equal(color, '#FF0000');
|
177 | });
|
178 | it('converts green to #008000', () => {
|
179 | const color = green.toHexString();
|
180 | assert.equal(color, '#008000');
|
181 | });
|
182 | it('converts blue to #0000FF', () => {
|
183 | const color = blue.toHexString();
|
184 | assert.equal(color, '#0000FF');
|
185 | });
|
186 | it('converts rgb(0, 127.5, 0) to #009900', () => {
|
187 | const color = rgb(0, 127.5, 0).toHexString();
|
188 | assert.equal(color, '#008000');
|
189 | });
|
190 |
|
191 | it('converts to a # and a six digit hex code', () => {
|
192 | const c = color('#16A085')
|
193 | .darken('10%')
|
194 | .toHexString();
|
195 | assert.equal(c.length, 7);
|
196 | });
|
197 | });
|
198 |
|
199 | describe('toHSL()', () => {
|
200 | it('converts from a named color to hsl', () => {
|
201 | const color1 = red.toHSL().toString();
|
202 | const color2 = hsl(0, 1, 0.5).toString();
|
203 | assert.equal(color1, color2);
|
204 | });
|
205 |
|
206 | it('converts from rgb to hsl', () => {
|
207 | const color1 = rgb(255, 0, 0)
|
208 | .toHSL()
|
209 | .toString();
|
210 | const color2 = hsl(0, 1, 0.5).toString();
|
211 | assert.equal(color1, color2);
|
212 | });
|
213 | });
|
214 |
|
215 | describe('toHSLA()', () => {
|
216 | it('converts from a named color to hsla', () => {
|
217 | const color1 = red.toHSLA().toString();
|
218 | const color2 = hsla(0, 1, 0.5, 1).toString();
|
219 | assert.equal(color1, color2);
|
220 | });
|
221 |
|
222 | it('converts from rgb to hsla', () => {
|
223 | const color1 = rgb(255, 0, 0)
|
224 | .toHSLA()
|
225 | .toString();
|
226 | const color2 = hsla(0, 1, 0.5, 1).toString();
|
227 | assert.equal(color1, color2);
|
228 | });
|
229 |
|
230 | it('converts from rgba to hsla', () => {
|
231 | const color1 = rgba(255, 0, 0, 0.5)
|
232 | .toHSLA()
|
233 | .toString();
|
234 | const color2 = hsla(0, 1, 0.5, 0.5).toString();
|
235 | assert.equal(color1, color2);
|
236 | });
|
237 | });
|
238 |
|
239 | describe('toRGB()', () => {
|
240 | it('converts from a named color to rgba', () => {
|
241 | const color1 = red.toRGBA().toString();
|
242 | const color2 = rgba(255, 0, 0, 1).toString();
|
243 | assert.equal(color1, color2);
|
244 | });
|
245 |
|
246 | it('converts from hsl to rgb', () => {
|
247 | const color1 = hsl(0, 1, 0.5)
|
248 | .toRGB()
|
249 | .toString();
|
250 | const color2 = rgb(255, 0, 0).toString();
|
251 | assert.equal(color1, color2);
|
252 | });
|
253 |
|
254 | it('converts from hsla to rgb', () => {
|
255 | const color1 = hsla(0, 1, 0.5, 0.5)
|
256 | .toRGB()
|
257 | .toString();
|
258 | const color2 = rgb(255, 0, 0).toString();
|
259 | assert.equal(color1, color2);
|
260 | });
|
261 | });
|
262 |
|
263 | describe('toRGBA()', () => {
|
264 | it('converts from hsla to rgba', () => {
|
265 | const color1 = hsla(0, 1, 0.5, 0.5)
|
266 | .toRGBA()
|
267 | .toString();
|
268 | const color2 = rgba(255, 0, 0, 0.5).toString();
|
269 | assert.equal(color1, color2);
|
270 | });
|
271 | });
|
272 |
|
273 | describe('red()', () => {
|
274 | it('returns the red channel from rgb', () => {
|
275 | const color1 = rgb(255, 0, 0);
|
276 | assert.equal(255, color1.red());
|
277 | });
|
278 |
|
279 | it('returns the red channel from rgba', () => {
|
280 | const color1 = rgba(255, 0, 0, 0.5);
|
281 | assert.equal(255, color1.red());
|
282 | });
|
283 | });
|
284 |
|
285 | describe('green()', () => {
|
286 | it('returns the green channel from rgb', () => {
|
287 | const color1 = rgb(0, 255, 0);
|
288 | assert.equal(255, color1.green());
|
289 | });
|
290 |
|
291 | it('returns the green channel from rgba', () => {
|
292 | const color1 = rgba(0, 255, 0, 0.5);
|
293 | assert.equal(255, color1.green());
|
294 | });
|
295 | });
|
296 |
|
297 | describe('blue()', () => {
|
298 | it('returns the blue channel from rgb', () => {
|
299 | const color1 = rgb(0, 0, 255);
|
300 | assert.equal(255, color1.blue());
|
301 | });
|
302 | it('returns the blue channel from rgba', () => {
|
303 | const color1 = rgba(0, 0, 255, 0.5);
|
304 | assert.equal(255, color1.blue());
|
305 | });
|
306 | });
|
307 |
|
308 | describe('opacity()', () => {
|
309 | it('returns the alpha channel from rgb', () => {
|
310 | const color1 = rgb(0, 0, 0);
|
311 | assert.equal(1, color1.alpha());
|
312 | assert.equal(1, color1.opacity());
|
313 | });
|
314 |
|
315 | it('returns the alpha channel from rgba', () => {
|
316 | const color1 = rgba(0, 0, 0, 0.5);
|
317 | assert.equal(0.5, color1.alpha());
|
318 | assert.equal(0.5, color1.opacity());
|
319 | });
|
320 | });
|
321 |
|
322 | describe('invert()', () => {
|
323 | it('inverts rgb', () => {
|
324 | const color1 = rgb(255, 0, 0)
|
325 | .invert()
|
326 | .toString();
|
327 | const color2 = cyan.toString();
|
328 | assert.equal(color1, color2);
|
329 | });
|
330 |
|
331 | it('inverts rgba', () => {
|
332 | const color1 = rgba(255, 0, 0, 0.5)
|
333 | .invert()
|
334 | .toString();
|
335 | const color2 = cyan.fade(0.5).toString();
|
336 | assert.equal(color1, color2);
|
337 | });
|
338 |
|
339 | it('inverts hsl', () => {
|
340 | const color1 = hsl(0, 1, 0.5)
|
341 | .invert()
|
342 | .toString();
|
343 | const color2 = cyan.toHSL().toString();
|
344 | assert.equal(color1, color2);
|
345 | });
|
346 |
|
347 | it('inverts hsla', () => {
|
348 | const color1 = hsla(0, 1, 0.5, 1)
|
349 | .invert()
|
350 | .toString();
|
351 | const color2 = cyan.toHSLA().toString();
|
352 | assert.equal(color1, color2);
|
353 | });
|
354 | });
|
355 |
|
356 | describe('lighten()', () => {
|
357 | it('lightens black to white', () => {
|
358 | const color1 = black.lighten(1).toString();
|
359 | const color2 = white.toString();
|
360 | assert.equal(color1, color2);
|
361 | });
|
362 | it('lightens black to gray', () => {
|
363 | const color1 = black.lighten(0.5).toString();
|
364 | const color2 = rgb(128, 128, 128).toString();
|
365 | assert.equal(color1, color2);
|
366 | });
|
367 | it('lightens from the absolute max', () => {
|
368 | const color1 = hsl(25, 1, 0.2)
|
369 | .lighten(0.3)
|
370 | .toString();
|
371 | const color2 = hsl(25, 1, 0.5).toString();
|
372 | assert.equal(color1, color2);
|
373 | });
|
374 | it('lightens from the current color', () => {
|
375 | const color1 = hsl(25, 1, 0.2).lighten(0.3, true);
|
376 | const color2 = hsl(25, 1, 0.44);
|
377 | assert.equal(color1.lightness(), color2.lightness());
|
378 | });
|
379 | });
|
380 |
|
381 | describe('darken()', () => {
|
382 | it('changes white to black', () => {
|
383 | const color1 = white.darken(1).toString();
|
384 | const color2 = black.toString();
|
385 | assert.equal(color1, color2);
|
386 | });
|
387 |
|
388 | it('changes black to gray', () => {
|
389 | const color1 = white
|
390 | .darken(0.5)
|
391 | .toHSL()
|
392 | .toString();
|
393 | const color2 = hsl(0, 0, 0.5).toString();
|
394 | assert.equal(color1, color2);
|
395 | });
|
396 |
|
397 | it('keeps the color format it started with', () => {
|
398 | const color1 = rgb(255, 0, 0)
|
399 | .darken(0.5)
|
400 | .toString();
|
401 | const color2 = rgb(0, 0, 0).toString();
|
402 | assert.equal(color1, color2);
|
403 | });
|
404 |
|
405 | it('keeps its alpha channel', () => {
|
406 | const color1 = rgba(69, 86, 100, '70%').darken(0.3);
|
407 | assert.equal(color1.opacity() - 0.7 < 0.0001, true);
|
408 | });
|
409 |
|
410 | it('darkens from the absolute max', () => {
|
411 | const color1 = hsl(25, 1, 0.8)
|
412 | .darken(0.3)
|
413 | .toString();
|
414 | const color2 = hsl(25, 1, 0.5).toString();
|
415 | assert.equal(color1, color2);
|
416 | });
|
417 |
|
418 | it('darkens from the current color', () => {
|
419 | const color1 = hsl(25, 1, 0.8)
|
420 | .darken(0.3, true)
|
421 | .toString();
|
422 | const color2 = hsl(25, 1, 0.56).toString();
|
423 | assert.equal(color1, color2);
|
424 | });
|
425 | });
|
426 |
|
427 | describe('saturate()', () => {
|
428 | it('changes white to midred', () => {
|
429 | const color1 = hsl(0, 0, 1)
|
430 | .saturate(0.5)
|
431 | .toString();
|
432 | const color2 = hsl(0, 0.5, 1).toString();
|
433 | assert.equal(color1, color2);
|
434 | });
|
435 |
|
436 | it('changes white to red', () => {
|
437 | const color1 = hsl(0, 0, 1)
|
438 | .saturate(1)
|
439 | .toString();
|
440 | const color2 = hsl(0, 1, 1).toString();
|
441 | assert.equal(color1, color2);
|
442 | });
|
443 | it('saturates from the absolute max', () => {
|
444 | const color1 = hsl(25, 0.2, 0.5)
|
445 | .saturate(0.3)
|
446 | .toString();
|
447 | const color2 = hsl(25, 0.5, 0.5).toString();
|
448 | assert.equal(color1, color2);
|
449 | });
|
450 |
|
451 | it('saturates from the current color', () => {
|
452 | const color1 = hsl(25, 0.2, 0.5)
|
453 | .saturate(0.3, true)
|
454 | .toString();
|
455 | const color2 = hsl(25, 0.44, 0.5).toString();
|
456 | assert.equal(color1, color2);
|
457 | });
|
458 | });
|
459 |
|
460 | describe('desaturate()', () => {
|
461 | it('changes red to midred', () => {
|
462 | const color1 = hsl(0, 1, 1)
|
463 | .desaturate(0.5)
|
464 | .toString();
|
465 | const color2 = hsl(0, 0.5, 1).toString();
|
466 | assert.equal(color1, color2);
|
467 | });
|
468 |
|
469 | it('changes red to white', () => {
|
470 | const color1 = red.desaturate(1).toString();
|
471 | const color2 = rgb(128, 128, 128).toString();
|
472 | assert.equal(color1, color2);
|
473 | });
|
474 |
|
475 | it('desaturates from the absolute max', () => {
|
476 | const color1 = hsl(25, 0.8, 0.5)
|
477 | .desaturate(0.3)
|
478 | .toString();
|
479 | const color2 = hsl(25, 0.5, 0.5).toString();
|
480 | assert.equal(color1, color2);
|
481 | });
|
482 |
|
483 | it('desaturates from the current color', () => {
|
484 | const color1 = hsl(25, 0.8, 0.5)
|
485 | .desaturate(0.3, true)
|
486 | .toString();
|
487 | const color2 = hsl(25, 0.56, 0.5).toString();
|
488 | assert.equal(color1, color2);
|
489 | });
|
490 | });
|
491 |
|
492 | describe('grayscale()', () => {
|
493 | it('handles red', () => {
|
494 | const color1 = red.grayscale().toHSL();
|
495 | assert.equal(color1.hue(), 0);
|
496 | assert.equal(color1.saturation(), 0);
|
497 | assert.equal(Math.round(color1.lightness() * 100), 50);
|
498 | });
|
499 |
|
500 | it('handles green', () => {
|
501 | const color1 = green.grayscale().toHSL();
|
502 | assert.equal(color1.hue(), 0);
|
503 | assert.equal(color1.saturation(), 0);
|
504 | assert.equal(Math.round(color1.lightness() * 100), 25);
|
505 | });
|
506 |
|
507 | it('handles blue', () => {
|
508 | const color1 = blue.grayscale().toHSL();
|
509 | assert.equal(color1.hue(), 0);
|
510 | assert.equal(color1.saturation(), 0);
|
511 | assert.equal(Math.round(color1.lightness() * 100), 50);
|
512 | });
|
513 |
|
514 | it('handles white', () => {
|
515 | const color1 = white.grayscale().toString();
|
516 | const color2 = white.toString();
|
517 | assert.equal(color1, color2);
|
518 | });
|
519 |
|
520 | it('handles black', () => {
|
521 | const color1 = black.grayscale().toString();
|
522 | const color2 = rgb(0, 0, 0).toString();
|
523 | assert.equal(color1, color2);
|
524 | });
|
525 | });
|
526 |
|
527 | describe('fade()', () => {
|
528 | it('changes rgb to rgba', () => {
|
529 | const color1 = rgb(255, 255, 255)
|
530 | .fade(1)
|
531 | .toString();
|
532 | const color2 = rgba(255, 255, 255, 1).toString();
|
533 | assert.equal(color1, color2);
|
534 | });
|
535 |
|
536 | it('changes hsl to hsla', () => {
|
537 | const color1 = hsl(0, 0, 1)
|
538 | .fade(1)
|
539 | .toString();
|
540 | const color2 = hsla(0, 0, 1, 1).toString();
|
541 | assert.equal(color1, color2);
|
542 | });
|
543 |
|
544 | it('clamps < 0 at 0% opacity', () => {
|
545 | const color1 = rgba(255, 0, 0, 0)
|
546 | .fade(-0.5)
|
547 | .toString();
|
548 | const color2 = rgba(255, 0, 0, 0).toString();
|
549 | assert.equal(color1, color2);
|
550 | });
|
551 |
|
552 | it('sets 0 to 0% opacity', () => {
|
553 | const color1 = rgba(255, 0, 0, 0)
|
554 | .fade(0)
|
555 | .toString();
|
556 | const color2 = rgba(255, 0, 0, 0).toString();
|
557 | assert.equal(color1, color2);
|
558 | });
|
559 |
|
560 | it('sets .5 to 50% opacity', () => {
|
561 | const color1 = rgba(255, 0, 0, 0)
|
562 | .fade(0.5)
|
563 | .toString();
|
564 | const color2 = rgba(255, 0, 0, 0.5).toString();
|
565 | assert.equal(color1, color2);
|
566 | });
|
567 |
|
568 | it('sets 1 to 100% opacity', () => {
|
569 | const color1 = rgba(255, 0, 0, 0)
|
570 | .fade(1)
|
571 | .toString();
|
572 | const color2 = rgba(255, 0, 0, 1).toString();
|
573 | assert.equal(color1, color2);
|
574 | });
|
575 |
|
576 | it('clamps > 1 to 100% opacity', () => {
|
577 | const color1 = rgba(255, 0, 0, 0)
|
578 | .fade(1.5)
|
579 | .toString();
|
580 | const color2 = rgba(255, 0, 0, 1).toString();
|
581 | assert.equal(color1, color2);
|
582 | });
|
583 | });
|
584 |
|
585 | describe('fadeOut', () => {
|
586 | it('clamps at 0%', () => {
|
587 | const color1 = rgba(255, 0, 0, 0)
|
588 | .fadeOut(0.25)
|
589 | .toString();
|
590 | const color2 = rgba(255, 0, 0, 0).toString();
|
591 | assert.equal(color1, color2);
|
592 | });
|
593 |
|
594 | it('.75 = 1 - .25', () => {
|
595 | const color1 = rgba(255, 0, 0, 1)
|
596 | .fadeOut(0.25)
|
597 | .toString();
|
598 | const color2 = rgba(255, 0, 0, 0.75).toString();
|
599 | assert.equal(color1, color2);
|
600 | });
|
601 |
|
602 | it('.5 = .75 - .25', () => {
|
603 | const color1 = rgba(255, 0, 0, 0.75)
|
604 | .fadeOut(0.25)
|
605 | .toString();
|
606 | const color2 = rgba(255, 0, 0, 0.5).toString();
|
607 | assert.equal(color1, color2);
|
608 | });
|
609 |
|
610 | it('.25 = .5 - .25', () => {
|
611 | const color1 = rgba(255, 0, 0, 0.5)
|
612 | .fadeOut(0.25)
|
613 | .toString();
|
614 | const color2 = rgba(255, 0, 0, 0.25).toString();
|
615 | assert.equal(color1, color2);
|
616 | });
|
617 |
|
618 | it('0 = .25 - .25', () => {
|
619 | const color1 = rgba(255, 0, 0, 0.25)
|
620 | .fadeOut(0.25)
|
621 | .toString();
|
622 | const color2 = rgba(255, 0, 0, 0).toString();
|
623 | assert.equal(color1, color2);
|
624 | });
|
625 |
|
626 | it('fades out from the absolute max', () => {
|
627 | const color1 = rgba(255, 0, 0, 0.8)
|
628 | .fadeOut(0.3)
|
629 | .toString();
|
630 | const color2 = rgba(255, 0, 0, 0.5).toString();
|
631 | assert.equal(color1, color2);
|
632 | });
|
633 |
|
634 | it('fades out from the current opacity', () => {
|
635 | const color1 = rgba(255, 0, 0, 0.8)
|
636 | .fadeOut(0.3, true)
|
637 | .toString();
|
638 | const color2 = rgba(255, 0, 0, 0.56).toString();
|
639 | assert.equal(color1, color2);
|
640 | });
|
641 | });
|
642 |
|
643 | describe('fadeIn', () => {
|
644 | it('.25 = 0 - .25', () => {
|
645 | const color1 = rgba(255, 0, 0, 0)
|
646 | .fadeIn(0.25)
|
647 | .toString();
|
648 | const color2 = rgba(255, 0, 0, 0.25).toString();
|
649 | assert.equal(color1, color2);
|
650 | });
|
651 |
|
652 | it('.5 = .25 - .25', () => {
|
653 | const color1 = rgba(255, 0, 0, 0.25)
|
654 | .fadeIn(0.25)
|
655 | .toString();
|
656 | const color2 = rgba(255, 0, 0, 0.5).toString();
|
657 | assert.equal(color1, color2);
|
658 | });
|
659 |
|
660 | it('.75 = .5 - .25', () => {
|
661 | const color1 = rgba(255, 0, 0, 0.5)
|
662 | .fadeIn(0.25)
|
663 | .toString();
|
664 | const color2 = rgba(255, 0, 0, 0.75).toString();
|
665 | assert.equal(color1, color2);
|
666 | });
|
667 |
|
668 | it('1 = .75 - .25', () => {
|
669 | const color1 = rgba(255, 0, 0, 0.75)
|
670 | .fadeIn(0.25)
|
671 | .toString();
|
672 | const color2 = rgba(255, 0, 0, 1).toString();
|
673 | assert.equal(color1, color2);
|
674 | });
|
675 |
|
676 | it('clamps at 100%', () => {
|
677 | const color1 = rgba(255, 0, 0, 1)
|
678 | .fadeIn(0.25)
|
679 | .toString();
|
680 | const color2 = rgba(255, 0, 0, 1).toString();
|
681 | assert.equal(color1, color2);
|
682 | });
|
683 |
|
684 | it('fades in from the absolute max', () => {
|
685 | const color1 = rgba(255, 0, 0, 0.2)
|
686 | .fadeIn(0.3)
|
687 | .toString();
|
688 | const color2 = rgba(255, 0, 0, 0.5).toString();
|
689 | assert.equal(color1, color2);
|
690 | });
|
691 |
|
692 | it('fades in from the current opacity', () => {
|
693 | const color1 = rgba(255, 0, 0, 0.2).fadeIn(0.3, true);
|
694 | const color2 = rgba(255, 0, 0, 0.44);
|
695 | assert.equal(color1.lightness(), color2.lightness());
|
696 | });
|
697 | });
|
698 |
|
699 | describe('mix()', () => {
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 | it('red + green', () => {
|
707 |
|
708 |
|
709 | const color1 = red.mix(green).toString();
|
710 | const color2 = rgb(128, 64, 0).toString();
|
711 | assert.equal(color1, color2);
|
712 | });
|
713 |
|
714 | it('red + blue', () => {
|
715 | const color1 = red.mix(blue).toString();
|
716 | const color2 = purple.toString();
|
717 | assert.equal(color1, color2);
|
718 | });
|
719 |
|
720 | it('red + white', () => {
|
721 | const color1 = red.mix(white).toString();
|
722 | const color2 = rgb(255, 128, 128).toString();
|
723 | assert.equal(color1, color2);
|
724 | });
|
725 |
|
726 | it('red + transparent', () => {
|
727 | const color1 = red.mix(transparent).toString();
|
728 | const color2 = rgba(255, 0, 0, 0.5).toString();
|
729 | assert.equal(color1, color2);
|
730 | });
|
731 |
|
732 | it('red + black', () => {
|
733 | const color1 = red.mix(black).toString();
|
734 | const color2 = maroon.toString();
|
735 | assert.equal(color1, color2);
|
736 | });
|
737 | });
|
738 |
|
739 | describe('tint()', () => {
|
740 | it('changed red to a lighter red', () => {
|
741 | const color1 = red.tint(0.3).toString();
|
742 |
|
743 | const color2 = rgb(255, 77, 77).toString();
|
744 | assert.equal(color1, color2);
|
745 | });
|
746 | });
|
747 |
|
748 | describe('shade()', () => {
|
749 | it('changed red to a darker red', () => {
|
750 | const color1 = red.shade(0.3).toString();
|
751 |
|
752 | const color2 = rgb(179, 0, 0).toString();
|
753 | assert.equal(color1, color2);
|
754 | });
|
755 | });
|
756 |
|
757 | describe('spin()', () => {
|
758 | it('spinning 360 degrees returns same color', () => {
|
759 | const color1 = red.toString();
|
760 | const color2 = red.spin(360).toString();
|
761 | assert.equal(color1, color2);
|
762 | });
|
763 |
|
764 | it('spinning -360 degrees returns same color', () => {
|
765 | const color1 = red.toString();
|
766 | const color2 = red.spin(-360).toString();
|
767 | assert.equal(color1, color2);
|
768 | });
|
769 |
|
770 | it('spinning -120 degrees from red returns blue', () => {
|
771 | const color1 = red.spin(-120).toHexString();
|
772 | assert.equal('#0000FF', color1);
|
773 | });
|
774 |
|
775 | it('spinning 120 degrees from red returns yellow', () => {
|
776 | const color1 = red.spin(120).toHexString();
|
777 | assert.equal('#00FF00', color1);
|
778 | });
|
779 | });
|
780 | });
|