-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
parseToRgb.test.js
171 lines (157 loc) · 4.02 KB
/
parseToRgb.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import parseToRgb from '../parseToRgb'
describe('parseToRgb', () => {
it('should parse a hex color representation', () => {
expect(parseToRgb('#Ff43AE')).toEqual({
blue: 174,
green: 67,
red: 255,
})
})
it('should parse an 8-digit hex color representation', () => {
expect(parseToRgb('#Ff43AEFF')).toEqual({
alpha: 1,
blue: 174,
green: 67,
red: 255,
})
})
it('should parse an 4-digit hex color representation', () => {
expect(parseToRgb('#0f08')).toEqual({
alpha: 0.53,
blue: 0,
green: 255,
red: 0,
})
})
it('should parse a reduced hex color representation', () => {
expect(parseToRgb('#45a')).toEqual({
blue: 170,
green: 85,
red: 68,
})
})
it('should parse a rgba color representation', () => {
expect(parseToRgb('rgba(174,67,255,0.6)')).toEqual({
alpha: 0.6,
blue: 255,
green: 67,
red: 174,
})
expect(parseToRgb('rgba(174 67 255 / 0.6)')).toEqual({
alpha: 0.6,
blue: 255,
green: 67,
red: 174,
})
expect(parseToRgb('rgb(174 67 255 / 60%)')).toEqual({
alpha: 0.6,
blue: 255,
green: 67,
red: 174,
})
})
it('should parse a rgb color representation', () => {
expect(parseToRgb('rgb(174,67,255)')).toEqual({
blue: 255,
green: 67,
red: 174,
})
expect(parseToRgb('rgb(174 67 255)')).toEqual({
blue: 255,
green: 67,
red: 174,
})
})
it('should parse a hsl color representation', () => {
expect(parseToRgb('hsl(210,10%,4%)')).toEqual({
blue: 11,
green: 10,
red: 9,
})
expect(parseToRgb('hsl(210 10% 4%)')).toEqual({
blue: 11,
green: 10,
red: 9,
})
})
it('should parse a hsl color representation with decimal values', () => {
expect(parseToRgb('hsl(210,16.4%,13.2%)')).toEqual({
blue: 38,
green: 33,
red: 28,
})
expect(parseToRgb('hsl(210 16.4% 13.2%)')).toEqual({
blue: 38,
green: 33,
red: 28,
})
})
it('should parse a hsla color representation', () => {
expect(parseToRgb('hsla(210,10%,40%,0.75)')).toEqual({
alpha: 0.75,
blue: 112,
green: 102,
red: 92,
})
expect(parseToRgb('hsla(210 10% 40% / 0.75)')).toEqual({
alpha: 0.75,
blue: 112,
green: 102,
red: 92,
})
expect(parseToRgb('hsl(210 10% 40% / 75%)')).toEqual({
alpha: 0.75,
blue: 112,
green: 102,
red: 92,
})
})
it('should parse a hsla color representation with decimal values', () => {
expect(parseToRgb('hsla(210,0.5%,0.5%,1.0)')).toEqual({
alpha: 1,
blue: 0,
green: 0,
red: 0,
})
expect(parseToRgb('hsla(210 0.5% 0.5% / 1.0)')).toEqual({
alpha: 1,
blue: 0,
green: 0,
red: 0,
})
expect(parseToRgb('hsl(210 0.5% 0.5% / 100%)')).toEqual({
alpha: 1,
blue: 0,
green: 0,
red: 0,
})
})
it('should throw an error if an invalid color string is provided', () => {
expect(() => {
parseToRgb('(174,67,255)')
}).toThrow(
"Couldn't parse the color string. Please provide the color as a string in hex, rgb, rgba, hsl or hsla notation.",
)
})
it('should throw an error if an invalid color string is provided', () => {
expect(() => {
parseToRgb(12345)
}).toThrow(
'Passed an incorrect argument to a color function, please pass a string representation of a color.',
)
})
it('should throw an error if an invalid hsl string is provided', () => {
expect(() => {
parseToRgb('hsl(210,120%,4%)')
}).toThrow(
`Couldn't generate valid rgb string from ${'hsl(210,120%,4%)'}, it returned ${'rgb(-2,10,22)'}.`,
)
})
it('should throw an error if an unparsable hsla string is provided', () => {
expect(() => {
parseToRgb('hsla(210,120%,4%,0.7)')
}).toThrow(
`Couldn't generate valid rgb string from ${'hsla(210,120%,4%,0.7)'}, it returned ${'rgb(-2,10,22)'}.`,
)
})
})