For CSS hue value must be between 0 and 360 degrees, saturation and lightness must be between 0 and 100 percents, alpha channel must be between 0 and 1.
You may use Regular Expressions to parse string CSS color and get the numeric values:
function parseHsl(color) {
var hslRegexp = /^\s*hsl\s*\(\s*(\d{1,3}\.?\d?)\s*,\s*(\d{1,3}\.?\d*)%\s*,\s*(\d{1,3}\.?\d*)%\s*\)\s*$/
var match = color.match(hslRegexp);
if (!match) {
throw new Error('Not an HSL color.');
}
var h = +match[1];
var s = +match[2];
var l = +match[3];
if (isNaN(h) || isNaN(s) || isNaN(l)) {
throw new Error('Not a number.');
}
h = h / 360;
s = s / 100;
l = l / 100;
if (h < 0 || h > 1 || s < 0 || s > 1 || l < 0 || l > 1) {
throw new Error('Out of range.');
}
return {
h: h,
s: s,
l: l
};
}
function getCssHslColor(hsl) {
return 'hsl(' + hsl.h * 360 + ',' + hsl.s * 100 + '%,' + hsl.l * 100 + '%)';
}