diff --git a/docs/assets/polished.js b/docs/assets/polished.js index 7e740c54..a21b33ee 100644 --- a/docs/assets/polished.js +++ b/docs/assets/polished.js @@ -2296,10 +2296,10 @@ var hexRgbaRegex = /^#[a-fA-F0-9]{8}$/; var reducedHexRegex = /^#[a-fA-F0-9]{3}$/; var reducedRgbaHexRegex = /^#[a-fA-F0-9]{4}$/; - var rgbRegex = /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/i; - var rgbaRegex = /^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*([-+]?[0-9]*[.]?[0-9]+)\s*\)$/i; - var hslRegex = /^hsl\(\s*(\d{0,3}[.]?[0-9]+)\s*,\s*(\d{1,3}[.]?[0-9]?)%\s*,\s*(\d{1,3}[.]?[0-9]?)%\s*\)$/i; - var hslaRegex = /^hsla\(\s*(\d{0,3}[.]?[0-9]+)\s*,\s*(\d{1,3}[.]?[0-9]?)%\s*,\s*(\d{1,3}[.]?[0-9]?)%\s*,\s*([-+]?[0-9]*[.]?[0-9]+)\s*\)$/i; + var rgbRegex = /^rgb\(\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*\)$/i; + var rgbaRegex = /^rgb(?:a)?\(\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,|\/)\s*([-+]?\d*[.]?\d+[%]?)\s*\)$/i; + var 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; + var 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; /** * Returns an RgbColor or RgbaColor object. This utility function is only useful * if want to extract a color component. With the color util `toColorString` you @@ -2373,7 +2373,7 @@ red: parseInt("" + rgbaMatched[1], 10), green: parseInt("" + rgbaMatched[2], 10), blue: parseInt("" + rgbaMatched[3], 10), - alpha: parseFloat("" + rgbaMatched[4]) + alpha: parseFloat("" + rgbaMatched[4]) > 1 ? parseFloat("" + rgbaMatched[4]) / 100 : parseFloat("" + rgbaMatched[4]) }; } @@ -2418,7 +2418,7 @@ red: parseInt("" + _hslRgbMatched[1], 10), green: parseInt("" + _hslRgbMatched[2], 10), blue: parseInt("" + _hslRgbMatched[3], 10), - alpha: parseFloat("" + hslaMatched[4]) + alpha: parseFloat("" + hslaMatched[4]) > 1 ? parseFloat("" + hslaMatched[4]) / 100 : parseFloat("" + hslaMatched[4]) }; } diff --git a/package.json b/package.json index 486b9766..33996b14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "polished", - "version": "4.2.1", + "version": "4.2.2", "description": "A lightweight toolset for writing styles in Javascript.", "license": "MIT", "author": "Brian Hough (https://polished.js.org)", diff --git a/src/color/parseToRgb.js b/src/color/parseToRgb.js index 802cac35..a7c91fa4 100644 --- a/src/color/parseToRgb.js +++ b/src/color/parseToRgb.js @@ -10,9 +10,9 @@ const hexRgbaRegex = /^#[a-fA-F0-9]{8}$/ const reducedHexRegex = /^#[a-fA-F0-9]{3}$/ const reducedRgbaHexRegex = /^#[a-fA-F0-9]{4}$/ const rgbRegex = /^rgb\(\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*\)$/i -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 +const rgbaRegex = /^rgb(?:a)?\(\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,|\/)\s*([-+]?\d*[.]?\d+[%]?)\s*\)$/i 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 -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 +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 /** * Returns an RgbColor or RgbaColor object. This utility function is only useful diff --git a/src/color/test/parseToHsl.test.js b/src/color/test/parseToHsl.test.js index 4b51619a..c84c2c4a 100644 --- a/src/color/test/parseToHsl.test.js +++ b/src/color/test/parseToHsl.test.js @@ -56,6 +56,21 @@ describe('parseToHsl', () => { }) }) + it('should parse a rgba color representation with a precise alpha', () => { + expect(parseToHsl('rgba(174,67,255,.12345)')).toEqual({ + alpha: 0.12345, + hue: 274.1489361702128, + lightness: 0.6313725490196078, + saturation: 1, + }) + expect(parseToHsl('rgba(174,67,255,12.345%)')).toEqual({ + alpha: 0.12345, + hue: 274.1489361702128, + lightness: 0.6313725490196078, + saturation: 1, + }) + }) + it('should parse a rgb color representation', () => { expect(parseToHsl('rgb(174,67,255)')).toEqual({ hue: 274.1489361702128, @@ -163,6 +178,21 @@ describe('parseToHsl', () => { }) }) + it('should parse a hsla color representation with a precise alpha', () => { + expect(parseToHsl('hsla(210,10%,40%,.12345)')).toEqual({ + alpha: 0.12345, + hue: 209.99999999999997, + lightness: 0.4, + saturation: 0.09803921568627451, + }) + expect(parseToHsl('hsla(210,10%,40%,12.345%)')).toEqual({ + alpha: 0.12345, + hue: 209.99999999999997, + lightness: 0.4, + saturation: 0.09803921568627451, + }) + }) + it('should parse a hsla 4 space-separated color representation', () => { expect(parseToHsl('hsla(210 10% 40% / 0.75)')).toEqual({ alpha: 0.75, diff --git a/src/color/test/parseToRgb.test.js b/src/color/test/parseToRgb.test.js index 90a3723c..4fc59763 100644 --- a/src/color/test/parseToRgb.test.js +++ b/src/color/test/parseToRgb.test.js @@ -48,7 +48,7 @@ describe('parseToRgb', () => { green: 67, red: 174, }) - expect(parseToRgb('rgb(174 67 255 / 60%)')).toEqual({ + expect(parseToRgb('rgb(174 67 255 / 0.6)')).toEqual({ alpha: 0.6, blue: 255, green: 67, @@ -108,7 +108,7 @@ describe('parseToRgb', () => { green: 102, red: 92, }) - expect(parseToRgb('hsl(210 10% 40% / 75%)')).toEqual({ + expect(parseToRgb('hsl(210 10% 40% / 0.75)')).toEqual({ alpha: 0.75, blue: 112, green: 102, @@ -129,7 +129,7 @@ describe('parseToRgb', () => { green: 0, red: 0, }) - expect(parseToRgb('hsl(210 0.5% 0.5% / 100%)')).toEqual({ + expect(parseToRgb('hsl(210 0.5% 0.5% / 1.0)')).toEqual({ alpha: 1, blue: 0, green: 0,