@ec0lint/plugin-css
Version:
ec0lint plugin that provides rules to verify CSS definition objects
83 lines (82 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseLch = exports.ColorFromLch = void 0;
const color_class_1 = require("./color-class");
const colord_1 = require("./colord");
const parser_1 = require("./parser");
class ColorFromLch extends color_class_1.AbsColor {
constructor(lch) {
super();
this.type = "lch";
this.lch = lch;
}
isComplete() {
var _a;
return (this.lch.complete && ((_a = this.getColord()) === null || _a === void 0 ? void 0 : _a.isValid())) || false;
}
getAlpha() {
var _a, _b;
return (_b = (_a = this.lch.alpha) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null;
}
removeAlpha() {
return new ColorFromLch(Object.assign(Object.assign({}, this.lch), { alpha: null }));
}
toColorString() {
return `${this.lch.rawName}(${this.lch.lch}${this.lch.alpha || ""}${(this.lch.extraArgs || []).join("")})`;
}
newColord() {
return (0, colord_1.parseColord)(this.toColorString());
}
}
exports.ColorFromLch = ColorFromLch;
function parseLch(input) {
const fn = (0, parser_1.parseFunction)(input, "lch");
if (fn == null) {
return null;
}
const values = (0, parser_1.parseArgumentValuesWithSpace)(fn.arguments, {
generate: (tokens) => {
if (tokens.length !== 3) {
return null;
}
const lightness = (0, parser_1.parseNumberUnit)(tokens[0], ["%"]);
if (!(0, parser_1.isPercentRange)(lightness)) {
return null;
}
const chroma = (0, parser_1.parseNumberUnit)(tokens[1], [""]);
const hue = (0, parser_1.parseNumberUnit)(tokens[2], [
"",
"deg",
"rad",
"grad",
"turn",
]);
if (!chroma || !hue) {
return null;
}
return {
lightness,
chroma,
hue,
};
},
});
const lch = values.values;
const alpha = values.alpha;
if (lch.complete && (!alpha || alpha.valid) && fn.arguments.length === 0) {
return {
complete: true,
rawName: fn.rawName,
lch,
alpha,
};
}
return {
complete: false,
rawName: fn.rawName,
lch,
alpha,
extraArgs: fn.arguments,
};
}
exports.parseLch = parseLch;