|
| 1 | +# get-image-colors |
| 2 | + |
| 3 | +Extract colors from images. Supports GIF, JPG, PNG, and even SVG! |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```sh |
| 10 | +npm install get-image-colors |
| 11 | +``` |
| 12 | + |
| 13 | +This package is intended for use in node environments. It won't work in a browser because it has node-specific dependencies. |
| 14 | + |
| 15 | +**Note:** when installing with webpack, if you get the error |
| 16 | + |
| 17 | +``` |
| 18 | +Can't resolve 'fs' in '/node_modules/get-svg-colors' |
| 19 | +``` |
| 20 | + |
| 21 | +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`: |
| 22 | + |
| 23 | +```js |
| 24 | +module.exports = { |
| 25 | + ..., |
| 26 | + node: { fs: 'empty' } |
| 27 | +}; |
| 28 | +``` |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +```js |
| 33 | +import { join, dirname } from 'node:path'; |
| 34 | +import { fileURLToPath } from 'node:url'; |
| 35 | +import getColors from 'get-image-colors'; |
| 36 | + |
| 37 | +getColors( |
| 38 | + join(dirname(fileURLToPath(import.meta.url)), 'double-rainbow.png') |
| 39 | +).then((colors) => { |
| 40 | + // `colors` is an array of color objects |
| 41 | +}); |
| 42 | +``` |
| 43 | +
|
| 44 | +You can also use a buffer as an input source. |
| 45 | +
|
| 46 | +```js |
| 47 | +import { join, dirname } from 'node:path'; |
| 48 | +import { fileURLToPath } from 'node:url'; |
| 49 | +import { readFileSync } from 'node:fs'; |
| 50 | +import getColors from 'get-image-colors'; |
| 51 | + |
| 52 | +const buffer = readFileSync( |
| 53 | + join(dirname(fileURLToPath(import.meta.url)), 'double-rainbow.gif') |
| 54 | +); |
| 55 | + |
| 56 | +getColors(buffer, 'image/gif').then((colors) => { |
| 57 | + // `colors` is an array of color objects |
| 58 | +}); |
| 59 | +``` |
| 60 | +
|
| 61 | +`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: |
| 62 | +
|
| 63 | +```js |
| 64 | +colors.map((color) => color.hex()); |
| 65 | +// => ['#FFFFFF', '#123123', '#F0F0F0'] |
| 66 | + |
| 67 | +colors[0].alpha(0.5).css(); |
| 68 | +// => 'rgb(0,128,128)' |
| 69 | +``` |
| 70 | +
|
| 71 | +If you don't like promises, you can use node-style callbacks too: |
| 72 | +
|
| 73 | +```js |
| 74 | +getColors(filename, (err, colors) => { |
| 75 | + if (err != undefined) throw err; |
| 76 | + // ... |
| 77 | +}); |
| 78 | +``` |
| 79 | +
|
| 80 | +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: |
| 81 | +
|
| 82 | +```js |
| 83 | +import { join, dirname } from 'node:path'; |
| 84 | +import { fileURLToPath } from 'node:url'; |
| 85 | +import getColors from 'get-image-colors'; |
| 86 | + |
| 87 | +const options = { |
| 88 | + count: 10, |
| 89 | + type: 'image/png' |
| 90 | +}; |
| 91 | + |
| 92 | +getColors( |
| 93 | + join(dirname(fileURLToPath(import.meta.url)), 'double-rainbow.png'), |
| 94 | + options |
| 95 | +).then((colors) => { |
| 96 | + // `colors` is an array of 10 color objects |
| 97 | +}); |
| 98 | +``` |
| 99 | +
|
| 100 | +## How it Works |
| 101 | +
|
| 102 | +`get-image-colors` uses [get-pixels][] to create a pixel array, then extracts a color palette with [get-rgba-palette][], which uses [quantize](http://npmjs.com/package/quantize) under the hood. |
| 103 | +
|
| 104 | +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][chroma.js]. |
| 105 | +
|
| 106 | +## Tests |
| 107 | +
|
| 108 | +```sh |
| 109 | +npm install |
| 110 | +npm test |
| 111 | +``` |
| 112 | +
|
| 113 | +## Dependencies |
| 114 | +
|
| 115 | +- [chroma-js][chroma.js]: JavaScript library for color conversions |
| 116 | +- [get-pixels][]: Reads the pixels of an image as an ndarray |
| 117 | +- [get-rgba-palette][]: Gets a palette of prominent colors from an array of pixels |
| 118 | +- [get-svg-colors](https://npmjs.com/package/get-svg-colors): Extract stroke and fill colors from SVG files |
| 119 | +
|
| 120 | +## Dev Dependencies |
| 121 | +
|
| 122 | +- [eslint](https://npmjs.com/package/eslint): ECMAScript/JavaScript linter |
| 123 | +- [mocha](https://npmjs.com/package/mocha): Simple, flexible, fun test framework |
| 124 | +- [prettier](https://npmjs.com/package/prettier): Opinionated code formatter |
| 125 | +
|
| 126 | +## License |
| 127 | +
|
| 128 | +MIT |
| 129 | +
|
| 130 | +[chroma.js]: https://npmjs.com/package/chroma-js |
| 131 | +[get-pixels]: https://npmjs.com/package/get-pixels |
| 132 | +[get-rgba-palette]: https://npmjs.com/package/get-rgba-palette |
0 commit comments