# get-image-colors

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

Latest version **4.0.1** (published 2022-02-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install get-image-colors
pnpm add get-image-colors
yarn add get-image-colors
bun add get-image-colors
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2022-02-04 |
| First published | 2015-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/get-image-colors) |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 290.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 350 |
| Author | zeke |
| Maintainers | zeke |
| Keywords | color, palette, svg, gif, png, jpg, canvas, pixels, rgb, rgba |

## Links

- npm: https://www.npmjs.com/package/get-image-colors
- Repository: https://github.com/colorjs/get-image-colors
- Homepage: https://github.com/colorjs/get-image-colors#readme
- Issues: https://github.com/colorjs/get-image-colors/issues
- npm.io page: https://npm.io/package/get-image-colors

## Dependencies (5)

- [pify](https://npm.io/package/pify.md) ^5.0.0
- [chroma-js](https://npm.io/package/chroma-js.md) ^2.1.0
- [get-pixels](https://npm.io/package/get-pixels.md) ^3.3.2
- [get-svg-colors](https://npm.io/package/get-svg-colors.md) ^2.0.0
- [get-rgba-palette](https://npm.io/package/get-rgba-palette.md) ^2.0.1

## Alternatives

- [postcss-color-hex-alpha](https://npm.io/package/postcss-color-hex-alpha.md) — 6.4M weekly downloads
- [randomcolor](https://npm.io/package/randomcolor.md) — 348.0K weekly downloads
- [bows](https://npm.io/package/bows.md) — 1.3K weekly downloads
- [ep_prefer_color_scheme](https://npm.io/package/ep_prefer_color_scheme.md) — 260 weekly downloads
- [coc-yank](https://npm.io/package/coc-yank.md) — 61 weekly downloads

## Recent versions

- 4.0.1 (latest) — 2022-02-04
- 4.0.0 — 2020-10-06
- 2.0.1 — 2020-04-23
- 2.0.0 — 2019-01-02
- 1.8.1 — 2017-02-12
- 1.8.0 — 2016-12-20
- 1.7.1 — 2016-12-20
- 1.7.0 — 2016-12-20
- 1.6.0 — 2016-12-20
- 1.5.1 — 2016-10-06
- 1.5.0 — 2016-02-27
- 1.4.0 — 2016-02-22
- 1.3.0 — 2015-12-06
- 1.2.0 — 2015-11-18
- 1.1.1 — 2015-11-17
- … 4 more at https://npm.io/package/get-image-colors/versions

## README

# get-image-colors

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

![example color palette](https://cldup.com/-uw9Ub6L6s.png)

## Installation

```sh
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](https://github.com/webpack-contrib/css-loader/issues/447), you will need to add `node: { fs: 'empty' }` to your `webpack.base.config`:
```
module.exports = {
    ... ,
    node: { fs: 'empty' }
}
```

## Usage

```js
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.
```js
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](http://gka.github.io/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:

```js
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:

```js
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:

```js
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](http://npm.im/get-pixels) to create a pixel array, then extracts a color palette with [get-rgba-palette](http://npm.im/get-rgba-palette), which uses [quantize](http://npm.im/quantize) under the hood.

Colors are converted from [get-rgba-palette's flat array format](https://github.com/mattdesl/get-rgba-palette#palettepixels-count-quality-filter) into [chroma.js color instances](http://gka.github.io/chroma.js/).

## Tests

```sh
npm install
npm test
```

## Dependencies

- [chroma-js](https://github.com/gka/chroma.js): JavaScript library for color conversions
- [get-pixels](https://github.com/scijs/get-pixels): Reads the pixels of an image as an ndarray
- [get-rgba-palette](https://github.com/mattdesl/get-rgba-palette): gets a palette of prominent colors from an array of pixels
- [get-svg-colors](https://github.com/colorjs/get-svg-colors): Extract stroke and fill colors from SVG files

## Dev Dependencies

- [mocha](https://github.com/mochajs/mocha): simple, flexible, fun test framework

## License

MIT

---
_Source: https://npm.io/package/get-image-colors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
