Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/__tests__/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ deepStrictEqual(fromString('rgba(255 255 255 / .5)').toHslaArray(), [0, 0, 100,
deepStrictEqual(fromString('rgba(0, 0, 0, 0)').toRgbaArray(), [0, 0, 0, 0]);
deepStrictEqual(fromString('rgba(3, 2, 1, 0)').toRgbaArray(), [3, 2, 1, 0]);
deepStrictEqual(fromString('hsl(0, 0%, 100%)').toHslaArray(), [0, 0, 100, 1]);
deepStrictEqual(fromString('hsl(0 0% 100%)').toHslaArray(), [0, 0, 100, 1]);
deepStrictEqual(fromString('hsl(0, 0%, 100%)').toRgbaArray(), [255, 255, 255, 1]);
deepStrictEqual(fromString('hsl(0 0% 100%)').toRgbaArray(), [255, 255, 255, 1]);
deepStrictEqual(fromString('hsla(0, 0%, 100%, .5)').toHslaArray(), [0, 0, 100, 0.5]);
deepStrictEqual(fromString('hsla(0 0% 100% / .5)').toHslaArray(), [0, 0, 100, 0.5]);
deepStrictEqual(fromString('hsla(0, 0%, 100%, .5)').toRgbaArray(), [255, 255, 255, 0.5]);
deepStrictEqual(fromString('hsla(0 0% 100% / .5)').toRgbaArray(), [255, 255, 255, 0.5]);
deepStrictEqual(fromString('#ffffff').toRgbaArray(), [255, 255, 255, 1]);
deepStrictEqual(fromString('#ffffff').toHslaArray(), [0, 0, 100, 1]);
deepStrictEqual(fromString('#ffffffff').toRgbaArray(), [255, 255, 255, 1]);
Expand Down
13 changes: 9 additions & 4 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const rgb = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(?:\s*,\s*(0|1|0?\.\d+|\d+%
const rgbfn = /^rgba?\(\s*(\d+)\s+(\d+)\s+(\d+)(?:\s*\/\s*(0|1|0?\.\d+|\d+%))?\s*\)$/;
const rgbperc = /^rgba?\(\s*(\d+%)\s*,\s*(\d+%)\s*,\s*(\d+%)(?:\s*,\s*(0|1|0?\.\d+|\d+%))?\s*\)$/;
const rgbpercfn = /^rgba?\(\s*(\d+%)\s+(\d+%)\s+(\d+%)(?:\s*\/\s*(0|1|0?\.\d+|\d+%))?\s*\)$/;
const hsl = /^hsla?\(\s*(\d+)(deg|rad|grad|turn)?\s*,\s*(\d+)%\s*,\s*(\d+)%(?:\s*,\s*(0|1|0?\.\d+|\d+%))?\s*\)$/;
const hsl = /^hsla?\(\s*(0?\.\d+|\d+)(deg|rad|grad|turn)?\s*,\s*(\d+)%\s*,\s*(\d+)%(?:\s*,\s*(0?\.\d+|\d+%))?\s*\)$/;
const hslws = /^hsla?\(\s*(0?\.\d+|\d+)(deg|rad|grad|turn)?\s+(\d+)%\s+(\d+)%(?:\s*\/\s*(0?\.\d+|\d+%))?\s*\)$/;

function contains(haystack, needle) {
return haystack.indexOf(needle) > -1;
Expand Down Expand Up @@ -151,8 +152,8 @@ function fromRgbString(str) {
return fromRgba([r, g, b, a]);
}

function fromHslString(str) {
let [, h, unit, s, l, a] = hsl.exec(str);
function fromHslString(reg, str) {
let [, h, unit, s, l, a] = reg.exec(str);
unit = unit || 'deg';
h = unitConverter(parseFloat(h), unit, 'deg');
s = parseFloat(s);
Expand All @@ -175,7 +176,11 @@ export function fromString(str) {
}

if (hsl.test(str)) {
return fromHslString(str);
return fromHslString(hsl, str);
}

if (hslws.test(str)) {
return fromHslString(hslws, str);
}

return null;
Expand Down