diff --git a/lib/parsers.js b/lib/parsers.js index 887bd51..29d6a31 100644 --- a/lib/parsers.js +++ b/lib/parsers.js @@ -4,6 +4,7 @@ ********************************************************************/ 'use strict'; +const { resolve } = require('@asamuzakjp/css-color'); const namedColors = require('./named_colors.json'); const { hslToRgb } = require('./utils/colorSpace'); @@ -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) { @@ -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) { @@ -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; }; diff --git a/lib/parsers.test.js b/lib/parsers.test.js index 0ab78d1..812ccf3 100644 --- a/lib/parsers.test.js +++ b/lib/parsers.test.js @@ -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', () => {