UNPKG

svg-color-linter

Version:

Linting tool to check if SVG files use only colors of a given color palette

29 lines (28 loc) 1.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSuggestions = void 0; const chroma_js_1 = require("chroma-js"); const utils_1 = require("./utils"); const getSuggestions = (color, palette) => { if (!(0, utils_1.isValidColor)(color)) { throw new Error(`The given color "${color}" is not valid!`); } color = (0, utils_1.toHexColor)(color); const distances = palette .map((paletteColor) => ({ distance: (0, chroma_js_1.deltaE)(paletteColor, color), color: paletteColor, })) .sort((a, b) => a.distance - b.distance); const suggestedColors = []; // pick five colors with the lowest distance to the input color distances.slice(0, 5).forEach((distance) => { const suggestedColor = distance.color; suggestedColors.push({ hex: suggestedColor, distance: distance.distance, }); }); return suggestedColors; }; exports.getSuggestions = getSuggestions;