UNPKG

svg-color-linter

Version:

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

21 lines (20 loc) 802 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getColorsOfFile = void 0; const getColorsOfFile = (svgFileContents) => { const colorPattern = new RegExp(/(fill|stop-color|stroke)="([^"]+)"/, 'gi'); const colorPatternCss = new RegExp(/(stroke|fill):\s?([^;"]+)/, 'gi'); const urlReference = new RegExp(/url\(#\w+\)/); const colors = [ ...svgFileContents.matchAll(colorPattern), ...svgFileContents.matchAll(colorPatternCss), ]; const result = [...colors] .map((c) => c[2]) .filter((c) => c !== undefined) .filter((c) => !c.match(urlReference)) .filter((c) => c !== 'none'); // remove duplicates before returning return [...new Set(result)]; }; exports.getColorsOfFile = getColorsOfFile;