UNPKG

@ec0lint/plugin-css

Version:

ec0lint plugin that provides rules to verify CSS definition objects

83 lines (82 loc) 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseHsl = exports.ColorFromHsl = void 0; const color_class_1 = require("./color-class"); const colord_1 = require("./colord"); const parser_1 = require("./parser"); class ColorFromHsl extends color_class_1.AbsColor { constructor(hsl) { super(); this.type = "hsl"; this.hsl = hsl; } isComplete() { var _a; return (this.hsl.complete && ((_a = this.getColord()) === null || _a === void 0 ? void 0 : _a.isValid())) || false; } getAlpha() { var _a, _b; return (_b = (_a = this.hsl.alpha) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null; } removeAlpha() { return new ColorFromHsl(Object.assign(Object.assign({}, this.hsl), { rawName: this.hsl.rawName.replace(/a$/iu, ""), alpha: null })); } toColorString() { return `${this.hsl.rawName}(${this.hsl.hsl}${this.hsl.alpha || ""}${(this.hsl.extraArgs || []).join("")})`; } newColord() { return (0, colord_1.parseColord)(this.toColorString()); } } exports.ColorFromHsl = ColorFromHsl; function parseHsl(input) { const fn = (0, parser_1.parseFunction)(input, ["hsl", "hsla"]); if (fn == null) { return null; } const values = (0, parser_1.parseArgumentValues)(fn.arguments, { argCount: 3, generate: (tokens) => { if (tokens.length !== 3) { return null; } const hue = (0, parser_1.parseNumberUnit)(tokens[0], [ "", "deg", "rad", "grad", "turn", ]); const saturation = (0, parser_1.parseNumberUnit)(tokens[1], ["%"]); const lightness = (0, parser_1.parseNumberUnit)(tokens[2], ["%"]); if (!hue || !(0, parser_1.isPercentRange)(saturation) || !(0, parser_1.isPercentRange)(lightness)) { return null; } return { hue, saturation, lightness, }; }, }); const hsl = values.values; const alpha = values.alpha; if (hsl.complete && (!alpha || alpha.valid) && fn.arguments.length === 0) { return { complete: true, rawName: fn.rawName, hsl, alpha, }; } return { complete: false, rawName: fn.rawName, hsl, alpha, extraArgs: fn.arguments, }; } exports.parseHsl = parseHsl;