Extract color palettes from images.
Simple use, < 5ko minified, fast process and no dependencies for browser.
Dependency to canvas for node.js
Requirements
Browsers
- Firefox: 29+
- Chrome: 33+
- Edge: 12+
- Opera: 19+
- Safari: 8+
- Webview Android: 4.4.3+
- Samsung Internet: 2.0+
Internet Explorer
Node
Install
For browser
npm install --save extract-colors
For node.js
Need to install dependency canvas
npm install --save extract-colors canvas
Usage
Browser example
import extractColors from 'extract-colors'
const src = 'my-image.jpg'
extractColors(src)
.then(console.log)
.catch(console.error)
You can use different types for src
param (String
for a path of image, Image
or ImageData
).
If you use ImageData
type, be carrefull because the extractor will not optimize the process (it will not reduce the count of pixels).
Node.js example
const path = require('path')
const { extractColors } = require('extract-colors')
const src = path.join(__dirname, './my-image.jpg')
extractColors(src)
.then(console.log)
.catch(console.log)
You can use different types for src
param (String
for a path of image or ImageData
).
If you use ImageData
type, be carrefull because the extractor will not optimize the process (it will not reduce the count of pixels).
Options
const src = 'my-image.jpg'
const options = {
pixels: 10000,
distance: 0.2,
saturationImportance: 0.2,
splitPower: 10,
colorValidator: (red, green, blue, alpha = 255) => alpha > 250
}
extractColors(src, options)
.then(console.log)
.catch(console.error)
pixels
Total pixel number of the resized picture for calculation
Type: Integer
Default: 10000
distance
From 0 to 1 is the color distance to not have near colors (1 distance is between white and black)
Type: Number
Default: 0.2
saturationImportance
Power of the saturation weight during the process (0 is not used, 1 is only saturation and not area size)
Type: Number
Default: 0.2
splitPower
Approximation power in the first color splitting during process (from 2 to 16)
Type: Integer
Default: 10
colorValidator
Test function to enable only some colors
Type: Function
Default: (red, green, blue, alpha = 255) => alpha > 250
crossOrigin
Only for browser, can be 'Anonymous' to avoid CORS
Type: String
Default: null
Return of the promise
Array of colors with the followed properties:
[
{
hex: '#62342b',
red: 98,
green: 52,
blue: 43,
area: 0.5915,
saturation: 0.2156862
},
...
]
Field | Example | Type | Description |
---|
hex | #62342b | String | color in hexadecimal string |
red | 98 | Integer | red canal from 0 to 255 |
green | 52 | Integer | green canal from 0 to 255 |
blue | 43 | Integer | blue canal from 0 to 255 |
area | 0.5915 | Number | area of the color and his neighbouring colors from 0 to 1 |
saturation | 0.2156862 | Number | color saturation from 0 to 1 |
API doc
Modules
- Color
Informations like saturation or count of pixels in image.
- ColorsExtractor
Process to extract main colors from list of colors.
- ColorGroup
Group colors with algorithms to optimize and merge neighbors colors.
- Browser
Browser exported functions.
- Node
Node exported functions.
Color
Informations like saturation or count of pixels in image.
.module.exports ⏏
Calculate some informations and store data about color.
Kind: static class of Color
new module.exports(red, green, blue, [hex])
Set red, green and blue colors to create the Color object.
Param | Type | Description |
---|
red | Number | Red channel integer from 0 to 255 |
green | Number | Green channel integer from 0 to 255 |
blue | Number | Blue channel integer from 0 to 255 |
[hex] | Number | Optional hexadecimal color from 0x000000 to 0xFFFFFF |
module.exports.distance(color) ⇒ Number
Distance between two colors.
- Minimum is 0 (between two same colors)
- Maximum is 1 (for example between black and white)
Kind: instance method of module.exports
Param | Type | Description |
---|
color | Color | Color to compare |
module.exports.getWeight(saturationImportance, maxCount) ⇒ Number
Weight of the color depends of his saturation and his count.
Kind: instance method of module.exports
Param | Type | Description |
---|
saturationImportance | Number | Determine the weight of the saturation for the calcul (from 0 to 1) |
maxCount | Number | Number of pixels in the image. |
module.exports.getSaturation() ⇒ Number
Saturation of the color from 0 to 1.
Kind: instance method of module.exports
Process to extract main colors from list of colors.
.module.exports ⏏
Process to extract neighboring colors.
Kind: static class of ColorsExtractor
ColorGroup
Group colors with algorithms to optimize and merge neighbors colors.
.module.exports ⏏
Manage list of colors or groups.
Kind: static class of ColorGroup
Browser
Browser exported functions.
Example
import extractColors from 'extract-colors'
const src = 'my-image.jpg'
extractColors(src)
.then(console.log)
.catch(console.error)
Browser~getImageData(image, pixels) ⇒ ImageData
Extract ImageData from image.
Reduce image to a pixel count.
Kind: inner method of Browser
Param | Type | Description |
---|
image | Image | Source image |
pixels | Number | Maximum number of pixels for process |
Extract colors from an ImageData object.
Kind: inner method of Browser
Param | Type | Description |
---|
imageData | ImageData | |
[options] | Object | Optional data |
[options.pixels] | String | Total pixel number of the resized picture for calculation |
[options.distance] | String | From 0 to 1 is the color distance to not have near colors (1 distance is between white and black) |
[options.saturationImportance] | String | Power of the saturation weight during the process (0 is not used, 1 is only saturation and not area size) |
[options.splitPower] | String | Approximation power in the first color splitting during process (from 2 to 16) |
[options.colorValidator] | String | Callback with test to enable only some colors |
Extract colors from an Image object.
Kind: inner method of Browser
Param | Type | Description |
---|
image | Image | |
[options] | Object | Optional data |
[options.pixels] | String | Total pixel number of the resized picture for calculation |
[options.distance] | String | From 0 to 1 is the color distance to not have near colors (1 distance is between white and black) |
[options.saturationImportance] | String | Power of the saturation weight during the process (0 is not used, 1 is only saturation and not area size) |
[options.splitPower] | String | Approximation power in the first color splitting during process (from 2 to 16) |
[options.colorValidator] | String | Callback with test to enable only some colors |
Extract colors from a path.
The image will be downloaded.
Kind: inner method of Browser
Param | Type | Description |
---|
src | String | |
[options] | Object | Optional data |
[options.pixels] | String | Total pixel number of the resized picture for calculation |
[options.distance] | String | From 0 to 1 is the color distance to not have near colors (1 distance is between white and black) |
[options.saturationImportance] | String | Power of the saturation weight during the process (0 is not used, 1 is only saturation and not area size) |
[options.splitPower] | String | Approximation power in the first color splitting during process (from 2 to 16) |
[options.colorValidator] | String | Callback with test to enable only some colors |
Extract colors from a picture.
Kind: inner method of Browser
Param | Type | Description |
---|
picture | String | Image | ImageData | Src, Image or ImageData |
[options] | Object | Optional data |
[options.pixels] | String | Total pixel number of the resized picture for calculation |
[options.distance] | String | From 0 to 1 is the color distance to not have near colors (1 distance is between white and black) |
[options.saturationImportance] | String | Power of the saturation weight during the process (0 is not used, 1 is only saturation and not area size) |
[options.splitPower] | String | Approximation power in the first color splitting during process (from 2 to 16) |
[options.colorValidator] | String | Callback with test to enable only some colors |
Node
Node exported functions.
Example
const path = require('path')
const { extractColors } = require('extract-colors')
const src = path.join(__dirname, './my-image.jpg')
extractColors(src)
.then(console.log)
.catch(console.log)
Example
import { extractColorsFromImageData } from 'extract-colors'
const imageData = { data: [0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF] }
extractColorsFromImageData(imageData)
.then(console.log)
.catch(console.error)
Node~getImageData(image, pixels) ⇒ ImageData
Extract ImageData from image.
Reduce image to a pixel count.
Kind: inner method of Node
Param | Type | Description |
---|
image | Image | Source image |
pixels | Number | Maximum number of pixels for process |
Extract colors from an ImageData object.
Kind: inner method of Node
Param | Type | Description |
---|
imageData | ImageData | |
[options] | Object | Optional data |
[options.pixels] | String | Total pixel number of the resized picture for calculation |
[options.distance] | String | From 0 to 1 is the color distance to not have near colors (1 distance is between white and black) |
[options.saturationImportance] | String | Power of the saturation weight during the process (0 is not used, 1 is only saturation and not area size) |
[options.splitPower] | String | Approximation power in the first color splitting during process (from 2 to 16) |
[options.colorValidator] | String | Callback with test to enable only some colors |
Extract colors from a path.
The image will be downloaded.
Kind: inner method of Node
Param | Type | Description |
---|
src | String | |
[options] | Object | Optional data |
[options.pixels] | String | Total pixel number of the resized picture for calculation |
[options.distance] | String | From 0 to 1 is the color distance to not have near colors (1 distance is between white and black) |
[options.saturationImportance] | String | Power of the saturation weight during the process (0 is not used, 1 is only saturation and not area size) |
[options.splitPower] | String | Approximation power in the first color splitting during process (from 2 to 16) |
[options.colorValidator] | String | Callback with test to enable only some colors |
Extract colors from a picture.
Kind: inner method of Node
Param | Type | Description |
---|
picture | String | Image | ImageData | Src, Image or ImageData |
[options] | Object | Optional data |
[options.pixels] | String | Total pixel number of the resized picture for calculation |
[options.distance] | String | From 0 to 1 is the color distance to not have near colors (1 distance is between white and black) |
[options.saturationImportance] | String | Power of the saturation weight during the process (0 is not used, 1 is only saturation and not area size) |
[options.splitPower] | String | Approximation power in the first color splitting during process (from 2 to 16) |
[options.colorValidator] | String | Callback with test to enable only some colors |