Skip to content

Commit

Permalink
Add css-color
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Nov 17, 2024
1 parent 9711014 commit 20eb33a
Show file tree
Hide file tree
Showing 4 changed files with 65 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
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
],
"main": "./lib/CSSStyleDeclaration.js",
"dependencies": {
"@asamuzakjp/css-color": "^1.1.1",
"rrweb-cssom": "^0.7.1"
},
"devDependencies": {
Expand Down
37 changes: 37 additions & 0 deletions test/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,52 @@ describe('parseColor', () => {
it('should convert not zero hsl with non zero hue 120 to rgb(0, 255, 0)', () => {
let input = 'hsl(120, 100%, 50%)';
let output = parsers.parseColor(input);

assert.strictEqual(output, 'rgb(0, 255, 0)');
});

it('should convert not zero hsl with non zero hue 240 to rgb(0, 0, 255)', () => {
let input = 'hsl(240, 100%, 50%)';
let output = parsers.parseColor(input);

assert.strictEqual(output, 'rgb(0, 0, 255)');
});

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

assert.strictEqual(output, '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);

assert.strictEqual(output, '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);

assert.strictEqual(output, '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);

assert.strictEqual(output, '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);

assert.strictEqual(output, 'rgb(128, 0, 128)');
});

it.todo('Add more tests');
});
describe('parseAngle', () => {
Expand Down

0 comments on commit 20eb33a

Please sign in to comment.