UNPKG

tailwindcss-palette-generator

Version:
69 lines 2.23 kB
import chroma from "chroma-js"; import { initialOptions } from "./consts.js"; import PaletteError from "./palette-error.js"; const isValidColor = (color) => { return chroma.valid(color); }; const getHexColor = (color) => { return chroma(color).hex().toLowerCase(); }; const checkParam = (palette) => { const { color, name, shade, shades } = palette; if (!color || typeof color !== "string" || !isValidColor(color)) { throw new PaletteError(`Invalid color: '${color}'. Please provide a valid color.`); } if (!name || typeof name !== "string") { throw new PaletteError(`Invalid name: '${name}'. Please provide a valid name.`); } if (shade && typeof shade !== "number") { throw new PaletteError(`Invalid shade value: '${shade}'. Shade must be a number.`); } if (shades) { if (!Array.isArray(shades)) { throw new PaletteError(`Invalid shades: '${shades}'. Shades must be an array.`); } if (shades.length < 3) { throw new PaletteError("Shades array must contain at least 3 elements."); } if (shade && !shades.includes(shade)) { throw new PaletteError(`Main shade '${shade}' is not included in the shades array.`); } } else { if (shade && !initialOptions.shades.includes(shade)) { throw new PaletteError(`Main shade '${shade}' must be one of: 50, 100, 200, 300, 400, 500, 600, 700, 800, 900.`); } } return true; }; const getPalettesFromOptions = (options) => { const palettes = []; for (const [key, value] of Object.entries(options)) { const palette = { name: key, color: value, shade: initialOptions.mainShade, shades: initialOptions.shades }; palettes.push(palette); } return palettes; }; const convertResultToCSS = (result, prefix) => { const colors = {}; for (const [key, color] of Object.entries(result)) { colors[`--${prefix}${key}`] = color.DEFAULT; for (const [shade, colorValue] of Object.entries(color)) { if (shade === "DEFAULT") continue; colors[`--${prefix}${key}-${shade}`] = colorValue; } } return colors; }; export { checkParam, convertResultToCSS, getHexColor, getPalettesFromOptions, isValidColor }; //# sourceMappingURL=utils.js.map