Skip to content

Commit

Permalink
Extend CSS color support
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Jan 28, 2024
1 parent b8ae491 commit 332203e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
********************************************************************/
'use strict';

const { resolve } = require('@asamuzakjp/css-color');
const namedColors = require('./named_colors.json');
const { hslToRgb } = require('./utils/colorSpace');

Expand Down Expand Up @@ -323,7 +324,7 @@ exports.parseColor = function parseColor(val) {
return 'rgb(' + red + ', ' + green + ', ' + blue + ')';
}

res = colorRegEx2.exec(val);
res = val.includes(',') && colorRegEx2.exec(val);
if (res) {
parts = res[1].split(/\s*,\s*/);
if (parts.length !== 3) {
Expand All @@ -346,7 +347,7 @@ exports.parseColor = function parseColor(val) {
return 'rgb(' + red + ', ' + green + ', ' + blue + ')';
}

res = colorRegEx3.exec(val);
res = val.includes(',') && colorRegEx3.exec(val);
if (res) {
parts = res[1].split(/\s*,\s*/);
if (parts.length !== 4) {
Expand Down Expand Up @@ -402,6 +403,11 @@ exports.parseColor = function parseColor(val) {
if (type === exports.TYPES.COLOR) {
return val;
}

res = resolve(val);
if (res) {
return res;
}
return undefined;
};

Expand Down
30 changes: 30 additions & 0 deletions lib/parsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,36 @@ describe('parseColor', () => {
let output = parsers.parseColor(input);
expect(output).toEqual(rgbValue);
});
it('should convert modern rgb to rgb values', () => {
let input = 'rgb(128 0 128 / 1)';
let output = parsers.parseColor(input);

expect(output).toEqual('rgb(128, 0, 128)');
});
it('should convert modern rgba to rgba values', () => {
let input = 'rgba(128 0 128 / 0.5)';
let output = parsers.parseColor(input);

expect(output).toEqual('rgba(128, 0, 128, 0.5)');
});
it('should convert lab to rgb values', () => {
let input = 'lab(46.2775% -47.5621 48.5837)'; // green
let output = parsers.parseColor(input);

expect(output).toEqual('rgb(0, 128, 0)');
});
it('should convert color function to rgb values', () => {
let input = 'color(srgb 0 0.5 0)';
let output = parsers.parseColor(input);

expect(output).toEqual('rgb(0, 128, 0)');
});
it('should convert color-mix to rgb values', () => {
let input = 'color-mix(in srgb, red, blue)';
let output = parsers.parseColor(input);

expect(output).toEqual('rgb(128, 0, 128)');
});
it.todo('Add more tests');
});
describe('parseAngle', () => {
Expand Down

0 comments on commit 332203e

Please sign in to comment.