JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 35351
  • Score
    100M100P100Q149871F
  • License MIT

Extract colors from images. Supports GIF, JPG, PNG, and even SVG!

Package Exports

  • get-image-colors

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (get-image-colors) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

get-image-colors

Extract colors from images. Supports GIF, JPG, PNG, and even SVG!

example

Installation

npm install get-image-colors --save

This package is intended for use in node environments. It won't work in a browser because it has node-specific dependencies.

Usage

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

getColors(__dirname + 'double-rainbow.png', function(err, colors){
  // colors is an array of colors
})

colors is an array of chroma.js color objects. chroma.js objects have methods that lets you pick the color format you want (RGB hex, HSL, etc), and give you access to powerful color manipulation features:

colors.map(color => color.hex())
// => ['#FFFFFF', '#123123', '#F0F0F0']

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

How it Works

get-image-colors uses get-pixels to create a pixel array, then extracts a color palette with get-rgba-palette, which uses quantize under the hood.

Colors are converted from get-rgba-palette's flat array format into chroma.js color instances.

To extract palettes from SVG files, a PNG copy is created on the fly using svg2png, which depends on PhantomJS. PhantomJS can be installed as a local node module, unlike canvas which has external dependencies.

Tests

npm install
npm test

Dependencies

  • chroma-js: JavaScript library for color conversions
  • get-pixels: Reads the pixels of an image as an ndarray
  • get-rgba-palette: gets a palette of prominent colors from an array of pixels
  • svg2png: A SVG to PNG converter, using PhantomJS

Dev Dependencies

  • mocha: simple, flexible, fun test framework

License

MIT