UNPKG

color-fns

Version:

Modern JavaScript color utility library.

31 lines 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var defaultOpts = { allowDecimal: true }; function parseHsl(value, options) { if (options === void 0) { options = defaultOpts; } if (typeof value !== 'string') { return null; } var regex = /^hsla?\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)%\s*,\s*(\d+(?:\.\d+)?)%\s*,*\s*(\d*(?:\.\d+)*)*\)/i; if (!options.allowDecimal) { regex = /^hsla?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*,*\s*(\d*(?:\.\d+)*)*\)/i; } // will consider hsl/hsla color prefix as a valid input color // while the output will be a valid web colors // valid input colors examples 'hsl(255, 100%, 50%, 0.5)', 'hsla(100, 100%, 50%)' // the output for the inputted examples 'hsla(255, 100%, 50%, 0.5)', 'hsl(100, 100%, 50%)' var match = value.match(regex); if (!match || match.length < 4) { return null; } return { alpha: typeof match[4] !== 'undefined' ? Number(match[4]) : undefined, hue: Number(match[1]), lum: Number(match[3]), sat: Number(match[2]), }; } exports.parseHsl = parseHsl; //# sourceMappingURL=parseHsl.js.map