UNPKG

@ec0lint/plugin-css

Version:

ec0lint plugin that provides rules to verify CSS definition objects

90 lines (89 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseHwb = exports.ColorFromHwb = void 0; const color_class_1 = require("./color-class"); const colord_1 = require("./colord"); const parser_1 = require("./parser"); class ColorFromHwb extends color_class_1.AbsColor { constructor(hwb) { super(); this.type = "hwb"; this.hwb = hwb; } isComplete() { var _a; return (this.hwb.complete && ((_a = this.getColord()) === null || _a === void 0 ? void 0 : _a.isValid())) || false; } getAlpha() { var _a, _b; return (_b = (_a = this.hwb.alpha) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null; } removeAlpha() { return new ColorFromHwb(Object.assign(Object.assign({}, this.hwb), { alpha: null })); } toColorString() { return `${this.hwb.rawName}(${this.hwb.hwb}${this.hwb.alpha || ""}${(this.hwb.extraArgs || []).join("")})`; } newColord() { const hwb = this.hwb; if (hwb.complete) { return (0, colord_1.parseColord)(`hwb(${numberWithUnitToString(hwb.hwb.value.hue)} ${numberWithUnitToString(hwb.hwb.value.whiteness)} ${numberWithUnitToString(hwb.hwb.value.blackness)}${hwb.alpha ? ` / ${hwb.alpha.toAlphaString()}` : ""})`); } return null; } } exports.ColorFromHwb = ColorFromHwb; function numberWithUnitToString(nu) { return `${nu.number}${nu.unit}`; } function parseHwb(input) { const fn = (0, parser_1.parseFunction)(input, "hwb"); 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 whiteness = (0, parser_1.parseNumberUnit)(tokens[1], ["%"]); const blackness = (0, parser_1.parseNumberUnit)(tokens[2], ["%"]); if (!hue || !(0, parser_1.isPercentRange)(whiteness) || !(0, parser_1.isPercentRange)(blackness)) { return null; } return { hue, whiteness, blackness, }; }, }); const hwb = values.values; const alpha = values.alpha; if (hwb.complete && (!alpha || alpha.valid) && fn.arguments.length === 0) { return { complete: true, rawName: fn.rawName, hwb, alpha, }; } return { complete: false, rawName: fn.rawName, hwb, alpha, extraArgs: fn.arguments, }; } exports.parseHwb = parseHwb;