Skip to content

Commit b928cdb

Browse files
levymetalbhough
authored andcommitted
fix(parsetorgb): fix rgba/hsla alpha precision
1 parent bee4d68 commit b928cdb

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

src/color/parseToRgb.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const hexRgbaRegex = /^#[a-fA-F0-9]{8}$/
1010
const reducedHexRegex = /^#[a-fA-F0-9]{3}$/
1111
const reducedRgbaHexRegex = /^#[a-fA-F0-9]{4}$/
1212
const rgbRegex = /^rgb\(\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*\)$/i
13-
const rgbaRegex = /^rgb(?:a)?\(\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,|\/)\s*([-+]?[0-9]*[.]?[0-9]?[0-9]?[%]?)\s*\)$/i
13+
const rgbaRegex = /^rgb(?:a)?\(\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,|\/)\s*([-+]?\d*[.]?\d+[%]?)\s*\)$/i
1414
const hslRegex = /^hsl\(\s*(\d{0,3}[.]?[0-9]+(?:deg)?)\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*\)$/i
15-
const hslaRegex = /^hsl(?:a)?\(\s*(\d{0,3}[.]?[0-9]+(?:deg)?)\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*(?:,|\/)\s*([-+]?[0-9]*[.]?[0-9]?[0-9]?[%]?)\s*\)$/i
15+
const hslaRegex = /^hsl(?:a)?\(\s*(\d{0,3}[.]?[0-9]+(?:deg)?)\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*(?:,|\/)\s*([-+]?\d*[.]?\d+[%]?)\s*\)$/i
1616

1717
/**
1818
* Returns an RgbColor or RgbaColor object. This utility function is only useful

src/color/test/parseToHsl.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ describe('parseToHsl', () => {
5656
})
5757
})
5858

59+
it('should parse a rgba color representation with a precise alpha', () => {
60+
expect(parseToHsl('rgba(174,67,255,.12345)')).toEqual({
61+
alpha: 0.12345,
62+
hue: 274.1489361702128,
63+
lightness: 0.6313725490196078,
64+
saturation: 1,
65+
})
66+
expect(parseToHsl('rgba(174,67,255,12.345%)')).toEqual({
67+
alpha: 0.12345,
68+
hue: 274.1489361702128,
69+
lightness: 0.6313725490196078,
70+
saturation: 1,
71+
})
72+
})
73+
5974
it('should parse a rgb color representation', () => {
6075
expect(parseToHsl('rgb(174,67,255)')).toEqual({
6176
hue: 274.1489361702128,
@@ -163,6 +178,21 @@ describe('parseToHsl', () => {
163178
})
164179
})
165180

181+
it('should parse a hsla color representation with a precise alpha', () => {
182+
expect(parseToHsl('hsla(210,10%,40%,.12345)')).toEqual({
183+
alpha: 0.12345,
184+
hue: 209.99999999999997,
185+
lightness: 0.4,
186+
saturation: 0.09803921568627451,
187+
})
188+
expect(parseToHsl('hsla(210,10%,40%,12.345%)')).toEqual({
189+
alpha: 0.12345,
190+
hue: 209.99999999999997,
191+
lightness: 0.4,
192+
saturation: 0.09803921568627451,
193+
})
194+
})
195+
166196
it('should parse a hsla 4 space-separated color representation', () => {
167197
expect(parseToHsl('hsla(210 10% 40% / 0.75)')).toEqual({
168198
alpha: 0.75,

src/color/test/parseToRgb.test.js

+62
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ describe('parseToRgb', () => {
5656
})
5757
})
5858

59+
it('should parse a rgba color representation with a precise alpha', () => {
60+
expect(parseToRgb('rgba(174,67,255,.12345)')).toEqual({
61+
alpha: 0.12345,
62+
blue: 255,
63+
green: 67,
64+
red: 174,
65+
})
66+
expect(parseToRgb('rgba(174,67,255,12.345%)')).toEqual({
67+
alpha: 0.12345,
68+
blue: 255,
69+
green: 67,
70+
red: 174,
71+
})
72+
})
73+
5974
it('should parse a rgb color representation', () => {
6075
expect(parseToRgb('rgb(174,67,255)')).toEqual({
6176
blue: 255,
@@ -137,6 +152,21 @@ describe('parseToRgb', () => {
137152
})
138153
})
139154

155+
it('should parse a hsla color representation with a precise alpha', () => {
156+
expect(parseToRgb('hsla(210,10%,40%,.12345)')).toEqual({
157+
alpha: 0.12345,
158+
blue: 112,
159+
green: 102,
160+
red: 92,
161+
})
162+
expect(parseToRgb('hsla(210,10%,40%,12.345%)')).toEqual({
163+
alpha: 0.12345,
164+
blue: 112,
165+
green: 102,
166+
red: 92,
167+
})
168+
})
169+
140170
it('should throw an error if an invalid color string is provided', () => {
141171
expect(() => {
142172
parseToRgb('(174,67,255)')
@@ -153,6 +183,38 @@ describe('parseToRgb', () => {
153183
)
154184
})
155185

186+
it('should throw an error if an invalid rgba string is provided', () => {
187+
const colors = [
188+
'rgba(174,67,255,)',
189+
'rgba(174,67,255,%)',
190+
'rgba(174,67,255,.)',
191+
'rgba(174,67,255,1.)',
192+
]
193+
colors.forEach(color => {
194+
expect(() => {
195+
parseToRgb(color)
196+
}).toThrow(
197+
"Couldn't parse the color string. Please provide the color as a string in hex, rgb, rgba, hsl or hsla notation.",
198+
)
199+
})
200+
})
201+
202+
it('should throw an error if an invalid hsla string is provided', () => {
203+
const colors = [
204+
'hsla(210,10%,40%,)',
205+
'hsla(210,10%,40%,%)',
206+
'hsla(210,10%,40%,.)',
207+
'hsla(210,10%,40%,1.)',
208+
]
209+
colors.forEach(color => {
210+
expect(() => {
211+
parseToRgb(color)
212+
}).toThrow(
213+
"Couldn't parse the color string. Please provide the color as a string in hex, rgb, rgba, hsl or hsla notation.",
214+
)
215+
})
216+
})
217+
156218
it('should throw an error if an invalid hsl string is provided', () => {
157219
expect(() => {
158220
parseToRgb('hsl(210,120%,4%)')

0 commit comments

Comments
 (0)