get-svg-colors

2.0.1 • Public • Published

get-svg-colors

Extract stroke and fill colors from SVG files. Designed for use in node, not the browser.

Installation

npm install get-svg-colors --save

Usage

const getColors = require("get-svg-colors")

// Give it an SVG filename
const colors = getColors(__dirname + '/australia.svg')

// Or an SVG string
const colors = getColors('<svg...>')

// You'll get back an object with two keys: `fills` and `strokes`

// `fills` is an array of chroma-js objects
colors.fills.map(color => color.hex())
// => ['#FFFFFF', '#123123', '#F0F0F0']

// `strokes` is also an array of chroma-js objects
colors.strokes.map(color => color.hex())
// => ['#FFFFFF', '#123123', '#F0F0F0']

// Crazy stuff...
colors.fills[0].alpha(0.5).css();
// => 'rgb(0,128,128)'

// Pass the `flat` option to get back a single array including
// de-duped fills and strokes together
const colors = getColors('<svg...>', {flat: true})
// => [...]

Tests

npm install
npm test

Dependencies

  • cheerio: Tiny, fast, and elegant implementation of core jQuery designed specifically for the server
  • chroma-js: JavaScript library for color conversions
  • is-svg: Check if a String/Buffer is SVG
  • lodash.compact: The modern build of lodash’s _.compact as a module.
  • lodash.uniq: The modern build of lodash’s _.uniq as a module.

Dev Dependencies

  • mocha: simple, flexible, fun test framework

License

ISC

Generated by package-json-to-readme

Readme

Keywords

none

Package Sidebar

Install

npm i get-svg-colors

Weekly Downloads

14,361

Version

2.0.1

License

ISC

Unpacked Size

253 kB

Total Files

13

Last publish

Collaborators

  • zeke