Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

get-image-colors

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-image-colors

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

  • 4.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
increased by10.92%
Maintainers
1
Weekly downloads
 
Created
Source

get-image-colors

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

example color palette

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.

Note: when installing with webpack, if you get the error

Can't resolve 'fs' in '/node_modules/get-svg-colors' 

as per an open issue in webpack-contrib, you will need to add node: { fs: 'empty' } to your webpack.base.config:

module.exports = {
    ... ,
    node: { fs: 'empty' }
}

Usage

const path = require('path')
const getColors = require('get-image-colors')

getColors(path.join(__dirname, 'double-rainbow.png')).then(colors => {
  // `colors` is an array of color objects
})

You can also use a buffer as an input source.

const fs = require('fs')
const buffer = fs.readFileSync(path.join(__dirname, 'double-rainbow.gif'))
const getColors = require('get-image-colors')

getColors(buffer, 'image/gif').then(colors => {
  // `colors` is an array of color objects
})

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)''

If you don't like promises, you can use node-style callbacks too:

getColors(filename, function (err, colors) {
  if (err) throw err
  // ...
})

The default number of colors returned is 5. You can specify a different number of colors by passing an options object into the call to getColors:

const path = require('path')
const getColors = require('get-image-colors')

const options = {
  count: 10,
  type: 'image/png'
}
getColors(path.join(__dirname, 'double-rainbow.png'), options).then(colors => {
  // `colors` is an array of 10 color objects
})

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.

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
  • get-svg-colors: Extract stroke and fill colors from SVG files

Dev Dependencies

  • mocha: simple, flexible, fun test framework

License

MIT

Keywords

FAQs

Package last updated on 04 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc