@ec0lint/plugin-css
Version:
ec0lint plugin that provides rules to verify CSS definition objects
81 lines (80 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseGray = exports.ColorFromGray = void 0;
const color_class_1 = require("./color-class");
const colord_1 = require("./colord");
const parser_1 = require("./parser");
class ColorFromGray extends color_class_1.AbsColor {
constructor(gray) {
super();
this.type = "gray";
this.gray = gray;
}
isComplete() {
var _a;
return (this.gray.complete && ((_a = this.getColord()) === null || _a === void 0 ? void 0 : _a.isValid())) || false;
}
getAlpha() {
var _a, _b;
return (_b = (_a = this.gray.alpha) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null;
}
removeAlpha() {
return new ColorFromGray(Object.assign(Object.assign({}, this.gray), { alpha: null }));
}
toColorString() {
return `${this.gray.rawName}(${this.gray.lightness}${this.gray.alpha || ""}${(this.gray.extraArgs || []).join("")})`;
}
newColord() {
var _a, _b;
const gray = this.gray;
if (gray.complete) {
return (0, colord_1.parseColord)({
l: gray.lightness.value.number,
a: 0,
b: 0,
alpha: (_b = (_a = gray.alpha) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined,
});
}
return null;
}
}
exports.ColorFromGray = ColorFromGray;
function parseGray(input) {
const fn = (0, parser_1.parseFunction)(input, "gray");
if (fn == null) {
return null;
}
const values = (0, parser_1.parseArgumentValues)(fn.arguments, {
argCount: 1,
generate: (tokens) => {
if (tokens.length !== 1) {
return null;
}
const lightness = (0, parser_1.parseNumberUnit)(tokens[0], ["", "%"]);
if (!(0, parser_1.isPercentRange)(lightness)) {
return null;
}
return lightness;
},
});
const lightness = values.values;
const alpha = values.alpha;
if (lightness.complete &&
(!alpha || alpha.valid) &&
fn.arguments.length === 0) {
return {
complete: true,
rawName: fn.rawName,
lightness,
alpha,
};
}
return {
complete: false,
rawName: fn.rawName,
lightness,
alpha,
extraArgs: fn.arguments,
};
}
exports.parseGray = parseGray;