UNPKG

replace-color-ts

Version:

Replace color with another one pixel by pixel.

72 lines (71 loc) 2.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const hex_color_regex_1 = __importDefault(require("hex-color-regex")); const validateColors = (colors) => { if (!colors) { return { code: "PARAMETER_REQUIRED", field: "options.colors", }; } if (!["hex", "rgb"].includes(colors.type)) { return { code: "PARAMETER_INVALID", field: "options.colors.type", }; } let isTargetColorValid; let isReplaceColorValid; if (colors.type === "hex") { isTargetColorValid = typeof colors.targetColor === "string" && colors.targetColor.length === 7 && (0, hex_color_regex_1.default)({ strict: true }).test(colors.targetColor); isReplaceColorValid = typeof colors.replaceColor === "string" && (colors.targetColor.length === 7 || colors.targetColor.length === 9) && (0, hex_color_regex_1.default)({ strict: true }).test(colors.replaceColor); } if (colors.type === "rgb") { isTargetColorValid = Array.isArray(colors.targetColor) && colors.targetColor.length === 3 && colors.targetColor.every((color) => color >= 0 && color <= 255); if (Array.isArray(colors.replaceColor)) { if (colors.replaceColor.length === 3) { isReplaceColorValid = colors.replaceColor.every((color) => color >= 0 && color <= 255); } else if (colors.replaceColor.length === 4) { isReplaceColorValid = colors.replaceColor .slice(0, 3) .every((color) => color >= 0 && color <= 255) && colors.replaceColor[3] >= 0 && colors.replaceColor[3] <= 1; } else { isReplaceColorValid = false; } } else { isReplaceColorValid = false; } } if (!isTargetColorValid) { return { code: "PARAMETER_INVALID", field: "options.colors.targetColor", }; } if (!isReplaceColorValid) { return { code: "PARAMETER_INVALID", field: "options.colors.replaceColor", }; } return null; }; exports.default = validateColors;