@ginden/blinkstick-v2
Version:
Improved Blickstick API for Node.js
67 lines • 2.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.interpretParametersInversed = void 0;
exports.interpretParameters = interpretParameters;
const clamp_1 = require("../clamp");
const tsafe_1 = require("tsafe");
const color_keywords_1 = require("../../consts/color-keywords");
const normalize_hex_color_1 = require("./normalize-hex-color");
const color_rgba_1 = __importDefault(require("color-rgba"));
const get_random_color_1 = require("./get-random-color");
function randomColor(options) {
return {
...(0, get_random_color_1.getRandomColor)(),
options: options ?? {},
};
}
function fromRgb(r, green, blue, options) {
return {
r: (0, clamp_1.clampRgb)(r),
g: (0, clamp_1.clampRgb)(green),
b: (0, clamp_1.clampRgb)(blue),
options: options ?? {},
};
}
function interpretParameters(...args) {
if (args[0] === 'random') {
return randomColor(args[1]);
}
if (typeof args[0] === 'number') {
return fromRgb(args[0], args[1], args[2], args[3]);
}
if (typeof args[0] === 'object') {
if (args[0] === null) {
throw new TypeError('Invalid color format. Expected a hex string or a color keyword.');
}
const { r, g, b } = args[0];
return fromRgb(r, g, b, args[1]);
}
if ((0, tsafe_1.typeGuard)(args[0], args[0] in color_keywords_1.COLOR_KEYWORDS)) {
return (0, normalize_hex_color_1.normalizeHexColor)(color_keywords_1.COLOR_KEYWORDS[args[0]], args[1] ?? {});
}
if ((0, tsafe_1.typeGuard)(args[0], args[0].startsWith('#'))) {
return (0, normalize_hex_color_1.normalizeHexColor)(args[0], args[1] ?? {});
}
const parsedColor = (0, color_rgba_1.default)(args[0]);
if (parsedColor.length !== 4) {
throw new TypeError('Invalid color format. Expected a hex string or a color keyword.');
}
if (parsedColor[3] !== 1) {
throw new TypeError('Alpha channel is not supported. Please use a color without alpha channel.');
}
return fromRgb(parsedColor[0], parsedColor[1], parsedColor[2], args[1] ?? {});
}
const interpretParametersInversed = (...args) => {
const ret = interpretParameters(...args);
return {
r: 255 - ret.r,
g: 255 - ret.g,
b: 255 - ret.b,
options: ret.options,
};
};
exports.interpretParametersInversed = interpretParametersInversed;
//# sourceMappingURL=interpret-parameters.js.map