Skip to content

Commit

Permalink
feat: parse 'transparent' as css color rgb(0 0 0 / 0)
Browse files Browse the repository at this point in the history
resolves #280
  • Loading branch information
gka committed Aug 18, 2024
1 parent 34a5d9d commit f22aa3f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/io/css/css2rgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ const noneToValue = (v, noneValue) => {

const css2rgb = (css) => {
css = css.toLowerCase().trim();

if (css === 'transparent') {
return [0, 0, 0, 0];
}

let m;

if (input.format.named) {
Expand Down Expand Up @@ -225,7 +230,8 @@ css2rgb.test = (s) => {
RE_RGB_LEGACY.test(s) ||
RE_RGBA_LEGACY.test(s) ||
RE_HSL_LEGACY.test(s) ||
RE_HSLA_LEGACY.test(s)
RE_HSLA_LEGACY.test(s) ||
s === 'transparent'
);
};

Expand Down
3 changes: 2 additions & 1 deletion test/io/css2rgb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ describe('Testing CSS2RGB color conversions', () => {
'lch(93.12% 39.2% 115.62deg)': [212, 248, 128, 1], // #d4f880
'lch(93.12% none none)': [235, 235, 235, 1],
'oklch(92.83% 0.15 123.12deg)': [212, 248, 130, 1],
'oklch(92.83% none none)': [231, 231, 231, 1]
'oklch(92.83% none none)': [231, 231, 231, 1],
transparent: [0, 0, 0, 0]
};

Object.keys(testCases).forEach((name) => {
Expand Down
3 changes: 3 additions & 0 deletions test/valid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ const valid = chroma.valid;
describe('Some tests for chroma.valid', () => {
it('valid color', () => {
expect(valid('red')).toBe(true);
expect(valid('transparent')).toBe(true);
});

it('invalid color', () => {
expect(valid('bread')).toBe(false);
expect(valid('unset')).toBe(false);
expect(valid('inherit')).toBe(false);
});
});

0 comments on commit f22aa3f

Please sign in to comment.