Skip to content

Commit bee4d68

Browse files
committed
chore(parsergb): fix support for percentage based opacity
1 parent 22b4305 commit bee4d68

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "polished",
3-
"version": "4.2.0",
3+
"version": "4.2.1",
44
"description": "A lightweight toolset for writing styles in Javascript.",
55
"license": "MIT",
66
"author": "Brian Hough <hello@brianhough.co> (https://polished.js.org)",

src/color/parseToRgb.js

Lines changed: 10 additions & 4 deletions
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]+)\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
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]+)\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
1616

1717
/**
1818
* Returns an RgbColor or RgbaColor object. This utility function is only useful
@@ -80,7 +80,10 @@ export default function parseToRgb(color: string): RgbColor | RgbaColor {
8080
red: parseInt(`${rgbaMatched[1]}`, 10),
8181
green: parseInt(`${rgbaMatched[2]}`, 10),
8282
blue: parseInt(`${rgbaMatched[3]}`, 10),
83-
alpha: parseFloat(`${rgbaMatched[4]}`),
83+
alpha:
84+
parseFloat(`${rgbaMatched[4]}`) > 1
85+
? parseFloat(`${rgbaMatched[4]}`) / 100
86+
: parseFloat(`${rgbaMatched[4]}`),
8487
}
8588
}
8689
const hslMatched = hslRegex.exec(normalizedColor)
@@ -113,7 +116,10 @@ export default function parseToRgb(color: string): RgbColor | RgbaColor {
113116
red: parseInt(`${hslRgbMatched[1]}`, 10),
114117
green: parseInt(`${hslRgbMatched[2]}`, 10),
115118
blue: parseInt(`${hslRgbMatched[3]}`, 10),
116-
alpha: parseFloat(`${hslaMatched[4]}`),
119+
alpha:
120+
parseFloat(`${hslaMatched[4]}`) > 1
121+
? parseFloat(`${hslaMatched[4]}`) / 100
122+
: parseFloat(`${hslaMatched[4]}`),
117123
}
118124
}
119125
throw new PolishedError(5)

src/color/test/parseToHsl.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('parseToHsl', () => {
4848
lightness: 0.6313725490196078,
4949
saturation: 1,
5050
})
51-
expect(parseToHsl('rgb(174 67 255 / 0.6)')).toEqual({
51+
expect(parseToHsl('rgb(174 67 255 / 60%)')).toEqual({
5252
alpha: 0.6,
5353
hue: 274.1489361702128,
5454
lightness: 0.6313725490196078,
@@ -134,7 +134,7 @@ describe('parseToHsl', () => {
134134
lightness: 0.4,
135135
saturation: 0.09803921568627451,
136136
})
137-
expect(parseToHsl('hsl(210deg 10% 40% / 0.75)')).toEqual({
137+
expect(parseToHsl('hsl(210deg 10% 40% / 75%)')).toEqual({
138138
alpha: 0.75,
139139
hue: 209.99999999999997,
140140
lightness: 0.4,
@@ -155,7 +155,7 @@ describe('parseToHsl', () => {
155155
lightness: 0.4,
156156
saturation: 0.09803921568627451,
157157
})
158-
expect(parseToHsl('hsl(210.99deg 10% 40% / 0.75)')).toEqual({
158+
expect(parseToHsl('hsl(210.99deg 10% 40% / 75%)')).toEqual({
159159
alpha: 0.75,
160160
hue: 209.99999999999997,
161161
lightness: 0.4,
@@ -176,7 +176,7 @@ describe('parseToHsl', () => {
176176
lightness: 0.4,
177177
saturation: 0.09803921568627451,
178178
})
179-
expect(parseToHsl('hsl(210deg 10% 40% / 0.75)')).toEqual({
179+
expect(parseToHsl('hsl(210deg 10% 40% / 75%)')).toEqual({
180180
alpha: 0.75,
181181
hue: 209.99999999999997,
182182
lightness: 0.4,
@@ -197,7 +197,7 @@ describe('parseToHsl', () => {
197197
lightness: 0.4,
198198
saturation: 0.09803921568627451,
199199
})
200-
expect(parseToHsl('hsl(210.99deg 10% 40% / 0.75)')).toEqual({
200+
expect(parseToHsl('hsl(210.99deg 10% 40% / 75%)')).toEqual({
201201
alpha: 0.75,
202202
hue: 209.99999999999997,
203203
lightness: 0.4,

src/color/test/parseToRgb.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('parseToRgb', () => {
4848
green: 67,
4949
red: 174,
5050
})
51-
expect(parseToRgb('rgb(174 67 255 / 0.6)')).toEqual({
51+
expect(parseToRgb('rgb(174 67 255 / 60%)')).toEqual({
5252
alpha: 0.6,
5353
blue: 255,
5454
green: 67,
@@ -108,7 +108,7 @@ describe('parseToRgb', () => {
108108
green: 102,
109109
red: 92,
110110
})
111-
expect(parseToRgb('hsl(210 10% 40% / 0.75)')).toEqual({
111+
expect(parseToRgb('hsl(210 10% 40% / 75%)')).toEqual({
112112
alpha: 0.75,
113113
blue: 112,
114114
green: 102,
@@ -129,7 +129,7 @@ describe('parseToRgb', () => {
129129
green: 0,
130130
red: 0,
131131
})
132-
expect(parseToRgb('hsl(210 0.5% 0.5% / 1.0)')).toEqual({
132+
expect(parseToRgb('hsl(210 0.5% 0.5% / 100%)')).toEqual({
133133
alpha: 1,
134134
blue: 0,
135135
green: 0,

0 commit comments

Comments
 (0)