Skip to content

Commit f35d317

Browse files
committed
fix(parsetorgb): add 4 space-separated CSS color value support to parsetoRGB
1 parent 58796b6 commit f35d317

File tree

4 files changed

+17
-27
lines changed

4 files changed

+17
-27
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "polished",
3-
"version": "4.1.5",
3+
"version": "4.2.0",
44
"description": "A lightweight toolset for writing styles in Javascript.",
55
"license": "MIT",
66
"author": "Brian Hough <[email protected]> (https://polished.js.org)",
@@ -93,7 +93,7 @@
9393
"jest": "^27.5.1",
9494
"lint-staged": "^12.3.7",
9595
"npm-watch": "^0.11.0",
96-
"prettier": "^2.6.1",
96+
"prettier": "^2.6.2",
9797
"pushstate-server": "^3.1.0",
9898
"ramda": "^0.28.0",
9999
"rollup": "^2.70.1",

src/color/parseToRgb.js

+2-3
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 = /^rgba\(\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]+)\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 = /^hsla\(\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]+)\s*\)$/i
1616

1717
/**
1818
* Returns an RgbColor or RgbaColor object. This utility function is only useful
@@ -85,7 +85,6 @@ export default function parseToRgb(color: string): RgbColor | RgbaColor {
8585
}
8686
const hslMatched = hslRegex.exec(normalizedColor)
8787
if (hslMatched) {
88-
console.log(hslMatched)
8988
const hue = parseInt(`${hslMatched[1]}`, 10)
9089
const saturation = parseInt(`${hslMatched[2]}`, 10) / 100
9190
const lightness = parseInt(`${hslMatched[3]}`, 10) / 100

src/color/test/parseToRgb.test.js

+9-18
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,22 @@ describe('parseToRgb', () => {
4242
green: 67,
4343
red: 174,
4444
})
45-
expect(parseToRgb('rgba( 174 , 67 , 255 , 0.6 )')).toEqual({
45+
expect(parseToRgb('rgba(174, 67, 255 / 0.6)')).toEqual({
4646
alpha: 0.6,
4747
blue: 255,
4848
green: 67,
4949
red: 174,
5050
})
51-
})
52-
53-
it('should parse a rgb color representation', () => {
54-
expect(parseToRgb('rgb(174,67,255)')).toEqual({
51+
expect(parseToRgb('rgb(174,67,255 / 0.6)')).toEqual({
52+
alpha: 0.6,
5553
blue: 255,
5654
green: 67,
5755
red: 174,
5856
})
59-
expect(parseToRgb('rgb( 174 , 67 , 255 )')).toEqual({
57+
})
58+
59+
it('should parse a rgb color representation', () => {
60+
expect(parseToRgb('rgb(174,67,255)')).toEqual({
6061
blue: 255,
6162
green: 67,
6263
red: 174,
@@ -69,11 +70,6 @@ describe('parseToRgb', () => {
6970
green: 10,
7071
red: 9,
7172
})
72-
expect(parseToRgb('hsl( 210 , 10% , 4% )')).toEqual({
73-
blue: 11,
74-
green: 10,
75-
red: 9,
76-
})
7773
})
7874

7975
it('should parse a hsl color representation with decimal values', () => {
@@ -82,11 +78,6 @@ describe('parseToRgb', () => {
8278
green: 33,
8379
red: 28,
8480
})
85-
expect(parseToRgb('hsl( 210 , 16.4%, 13.2% )')).toEqual({
86-
blue: 38,
87-
green: 33,
88-
red: 28,
89-
})
9081
})
9182

9283
it('should parse a hsla color representation', () => {
@@ -96,7 +87,7 @@ describe('parseToRgb', () => {
9687
green: 102,
9788
red: 92,
9889
})
99-
expect(parseToRgb('hsla( 210 , 10% , 40% , 0.75 )')).toEqual({
90+
expect(parseToRgb('hsla(210, 10%, 40% / 0.75)')).toEqual({
10091
alpha: 0.75,
10192
blue: 112,
10293
green: 102,
@@ -111,7 +102,7 @@ describe('parseToRgb', () => {
111102
green: 0,
112103
red: 0,
113104
})
114-
expect(parseToRgb('hsla( 210 , 0.5% , 0.5% , 1.0 )')).toEqual({
105+
expect(parseToRgb('hsla(210, 0.5%, 0.5% / 1.0)')).toEqual({
115106
alpha: 1,
116107
blue: 0,
117108
green: 0,

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -9607,10 +9607,10 @@ prettier@^1.5.2:
96079607
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
96089608
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==
96099609

9610-
prettier@^2.6.1:
9611-
version "2.6.1"
9612-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.6.1.tgz#d472797e0d7461605c1609808e27b80c0f9cfe17"
9613-
integrity sha512-8UVbTBYGwN37Bs9LERmxCPjdvPxlEowx2urIL6urHzdb3SDq4B/Z6xLFCblrSnE4iKWcS6ziJ3aOYrc1kz/E2A==
9610+
prettier@^2.6.2:
9611+
version "2.6.2"
9612+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.6.2.tgz#e26d71a18a74c3d0f0597f55f01fb6c06c206032"
9613+
integrity sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew==
96149614

96159615
pretty-format@^27.5.1:
96169616
version "27.5.1"

0 commit comments

Comments
 (0)