Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parsetorgb): fix rgba/hsla alpha precision #611

Merged
merged 2 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/assets/polished.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
};
}

Expand Down Expand Up @@ -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])
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/color/parseToRgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/color/test/parseToHsl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
62 changes: 62 additions & 0 deletions src/color/test/parseToRgb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ describe('parseToRgb', () => {
})
})

it('should parse a rgba color representation with a precise alpha', () => {
expect(parseToRgb('rgba(174,67,255,.12345)')).toEqual({
alpha: 0.12345,
blue: 255,
green: 67,
red: 174,
})
expect(parseToRgb('rgba(174,67,255,12.345%)')).toEqual({
alpha: 0.12345,
blue: 255,
green: 67,
red: 174,
})
})

it('should parse a rgb color representation', () => {
expect(parseToRgb('rgb(174,67,255)')).toEqual({
blue: 255,
Expand Down Expand Up @@ -137,6 +152,21 @@ describe('parseToRgb', () => {
})
})

it('should parse a hsla color representation with a precise alpha', () => {
expect(parseToRgb('hsla(210,10%,40%,.12345)')).toEqual({
alpha: 0.12345,
blue: 112,
green: 102,
red: 92,
})
expect(parseToRgb('hsla(210,10%,40%,12.345%)')).toEqual({
alpha: 0.12345,
blue: 112,
green: 102,
red: 92,
})
})

it('should throw an error if an invalid color string is provided', () => {
expect(() => {
parseToRgb('(174,67,255)')
Expand All @@ -153,6 +183,38 @@ describe('parseToRgb', () => {
)
})

it('should throw an error if an invalid rgba string is provided', () => {
const colors = [
'rgba(174,67,255,)',
'rgba(174,67,255,%)',
'rgba(174,67,255,.)',
'rgba(174,67,255,1.)',
]
colors.forEach(color => {
expect(() => {
parseToRgb(color)
}).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 hsla string is provided', () => {
const colors = [
'hsla(210,10%,40%,)',
'hsla(210,10%,40%,%)',
'hsla(210,10%,40%,.)',
'hsla(210,10%,40%,1.)',
]
colors.forEach(color => {
expect(() => {
parseToRgb(color)
}).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 hsl string is provided', () => {
expect(() => {
parseToRgb('hsl(210,120%,4%)')
Expand Down