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 parseHsv(value, options) { if (options === void 0) { options = defaultOpts; } if (typeof value !== 'string') { return null; } var regex = /^hsva?\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)%\s*,\s*(\d+(?:\.\d+)?)%\s*,*\s*(\d*(?:\.\d+)*)*\)/i; if (!options.allowDecimal) { regex = /^hsva?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*,*\s*(\d*(?:\.\d+)*)*\)/i; } // will consider hsv/hsva color prefix as a valid input color // while the output will be a valid web colors // valid input colors examples 'hsv(255, 100%, 50%, 0.5)', 'hsva(100, 100%, 50%)' // the output for the inputted examples 'hsva(255, 100%, 50%, 0.5)', 'hsv(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]), sat: Number(match[2]), val: Number(match[3]), }; } exports.parseHsv = parseHsv; //# sourceMappingURL=parseHsv.js.map