/
colours.js
144 lines (108 loc) · 3.44 KB
/
colours.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import min from '../colours';
function isEqual(input, output) {
return () => expect(min(input)).toBe(output);
}
test('should lowercase keywords', isEqual('RED', 'red'));
test('should convert shorthand hex to keyword', isEqual('#f00', 'red'));
test('should convert longhand hex to keyword', isEqual('#ff0000', 'red'));
test('should convert rgb to keyword', isEqual('rgb(255,0,0)', 'red'));
test(
'should convert fully opaque rgb to keyword',
isEqual('rgba(255, 0, 0, 1)', 'red')
);
test('should convert hsl to keyword', isEqual('hsl(0, 100%, 50%)', 'red'));
test(
'should convert fully oqaque hsl to keyword',
isEqual('hsla(0, 100%, 50%, 1)', 'red')
);
test(
'should convert translucent hsla to rgba',
isEqual('hsla(0, 100%, 50%, .5)', 'rgba(255, 0, 0, 0.5)')
);
test(
'should convert longhand hex to shorthand, case insensitive',
isEqual('#FFFFFF', '#fff')
);
test(
'should convert keyword to hex, case insensitive',
isEqual('WHiTE', '#fff')
);
test('should convert keyword to hex', isEqual('yellow', '#ff0'));
test('should convert rgb to hex', isEqual('rgb(12, 134, 29)', '#0c861d'));
test('should convert hsl to hex', isEqual('hsl(230, 50%, 40%)', '#349'));
test(
'should convert another longhand hex to keyword',
isEqual('#000080', 'navy')
);
test(
'should convert rgba to hsla when shorter',
isEqual('rgba(221, 221, 221, 0.5)', 'hsla(0, 0%, 87%, 0.5)')
);
test(
'should convert this specific rgba value to "transparent"',
isEqual('rgba(0,0,0,0)', 'transparent')
);
test(
'should convert this specific hsla value to "transparent"',
isEqual('hsla(0,0%,0%,0)', 'transparent')
);
test(
'should convert hsla values with 0 saturation & 0 lightness to "transparent"',
isEqual('hsla(200,0%,0%,0)', 'transparent')
);
test(
'should leave transparent as it is',
isEqual('transparent', 'transparent')
);
test(
'should prefer to output hex rather than keywords when they are the same length',
isEqual('#696969', '#696969')
);
test('should cap values at their maximum', isEqual('rgb(400,400,400)', '#fff'));
test(
'should continue hsl value rotation',
isEqual('hsl(400, 400%, 50%)', '#fa0')
);
test(
'should convert signed numbers',
isEqual('rgba(-100,0,-100,.5)', 'rgba(0, 0, 0, 0.5)')
);
test(
'should convert signed numbers (2)',
isEqual('hsla(-400,50%,10%,.5)', 'rgba(38, 13, 30, 0.5)')
);
test(
'should convert percentage based rgb values',
isEqual('rgb(100%,100%,100%)', '#fff')
);
test(
'should convert percentage based rgba values (2)',
isEqual('rgba(50%,50%,50%,0.5)', 'hsla(0, 0%, 50%, 0.5)')
);
test(
'should convert percentage based rgba values (3)',
isEqual('rgb(100%,100%,100%)', '#fff')
);
test(
'should convert percentage based rgba values (4)',
isEqual('rgba(100%,100%,100%,0.5)', 'hsla(0, 0%, 100%, 0.5)')
);
test(
'should convert percentage based rgba values (5)',
isEqual('rgba(100%,64.7%,0%,.5)', 'rgba(255, 165, 0, 0.5)')
);
test(
'should pass through on invalid rgb functions',
isEqual('rgb(50%,23,54)', 'rgb(50%,23,54)')
);
test('should convert darkgray to a hex', isEqual('darkgray', '#a9a9a9'));
test('should convert 8 character hex codes', isEqual('#000000FF', '#000'));
test('should convert 4 character hex codes', isEqual('#000F', '#000'));
test(
'should pass through 8 character hex codes',
isEqual('#00000004', '#00000004')
);
test('should pass through if not recognised', () => {
expect(min('Unrecognised')).toBe('Unrecognised');
expect(min('inherit')).toBe('inherit');
});