Skip to content

Commit

Permalink
fix(parsetorgb): fixes support for variable length floats
Browse files Browse the repository at this point in the history
Addresses an issue introduced in 4.2.1 that broke colors with values of various float lengths.

fix #610
  • Loading branch information
bhough committed Apr 6, 2022
1 parent 02435da commit 8d5ccfa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 66 deletions.
2 changes: 1 addition & 1 deletion 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 <hello@brianhough.co> (https://polished.js.org)",
Expand Down
68 changes: 3 additions & 65 deletions src/color/test/parseToRgb.test.js
Expand Up @@ -48,29 +48,14 @@ 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,
red: 174,
})
})

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 @@ -123,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,
Expand All @@ -144,29 +129,14 @@ 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,
red: 0,
})
})

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 @@ -183,38 +153,6 @@ 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

0 comments on commit 8d5ccfa

Please sign in to comment.