-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathhsi.test.js
125 lines (108 loc) · 2.65 KB
/
hsi.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import test from 'node:test';
import assert from 'node:assert';
import { hsi, rgb, formatCss } from '../src/index.js';
test('rgb() converts from HSI to RGB', t => {
assert.deepEqual(
rgb(hsi({ h: 0, s: 0, i: 0 })),
{ r: 0, g: 0, b: 0, mode: 'rgb' },
'lightness 0 should yield black'
);
assert.deepEqual(
rgb(hsi({ h: 60, s: 0.25, i: 0 })),
{ r: 0, g: 0, b: 0, mode: 'rgb' },
'...regardless of hue and saturation'
);
assert.deepEqual(
rgb(hsi({ h: 0, s: 0, i: 0.5 })),
{ r: 0.5, g: 0.5, b: 0.5, mode: 'rgb' },
'saturation 0 should yield gray'
);
assert.deepEqual(
rgb(hsi({ h: 60, s: 0, i: 0.25 })),
{ r: 0.25, g: 0.25, b: 0.25, mode: 'rgb' },
'...regardless of the hue'
);
assert.deepEqual(
rgb(hsi({ h: 100, s: 0, i: 0.5 })),
{ r: 0.5, g: 0.5, b: 0.5, mode: 'rgb' },
'...or the lightness'
);
});
test('hsi() converts RGB to HSI', t => {
assert.deepEqual(
hsi(rgb({ r: 0, g: 0, b: 0 })),
{ s: 0, i: 0, mode: 'hsi' },
'black'
);
assert.deepEqual(
hsi(rgb({ r: 0.25, g: 0.25, b: 0.25 })),
{ s: 0, i: 0.25, mode: 'hsi' },
'R = G = B yields undefined hue'
);
assert.deepEqual(
hsi(rgb({ r: 0.6, g: 0.6, b: 0.6 })),
{ s: 0, i: 0.6, mode: 'hsi' },
'R = G = B yields zero saturation'
);
assert.deepEqual(
hsi(rgb({ r: 1, g: 0, b: 0 })),
{ h: 0, s: 1, i: 0.3333333333333333, mode: 'hsi' },
'red'
);
assert.deepEqual(
hsi(rgb({ r: 1, g: 1, b: 0 })),
{ h: 60, s: 1, i: 0.6666666666666666, mode: 'hsi' },
'yellow'
);
assert.deepEqual(
hsi(rgb({ r: 0, g: 1, b: 0 })),
{ h: 120, s: 1, i: 0.3333333333333333, mode: 'hsi' },
'green'
);
assert.deepEqual(
hsi(rgb({ r: 0, g: 1, b: 1 })),
{ h: 180, s: 1, i: 0.6666666666666666, mode: 'hsi' },
'cyan'
);
assert.deepEqual(
hsi(rgb({ r: 0, g: 0, b: 1 })),
{ h: 240, s: 1, i: 0.3333333333333333, mode: 'hsi' },
'blue'
);
assert.deepEqual(
hsi(rgb({ r: 1, g: 0, b: 1 })),
{ h: 300, s: 1, i: 0.6666666666666666, mode: 'hsi' },
'magenta'
);
});
test('color(--hsi)', t => {
assert.deepEqual(hsi('color(--hsi 30 0.5 1 / 0.25)'), {
h: 30,
s: 0.5,
i: 1,
alpha: 0.25,
mode: 'hsi'
});
assert.deepEqual(hsi('color(--hsi 0 50% 0.5 / 25%)'), {
h: 0,
s: 0.5,
i: 0.5,
alpha: 0.25,
mode: 'hsi'
});
});
test('formatCss', t => {
assert.equal(
formatCss('color(--hsi 0 50% 0.5 / 25%)'),
'color(--hsi 0 0.5 0.5 / 0.25)'
);
});
test('missing components', t => {
assert.ok(rgb('color(--hsi none 0.5 none)'), 'hsi to rgb is ok');
assert.deepEqual(
rgb('color(--hsi none 0.5 none)'),
rgb('color(--hsi 0 0.5 0')
);
assert.ok(hsi('rgb(none 100 20)'), 'rgb to hsi is ok');
assert.deepEqual(hsi('rgb(none 100 20)'), hsi('rgb(0 100 20)'));
});