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

extract-colors

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extract-colors

Extract color palettes from images

  • 1.1.10
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10K
increased by20.4%
Maintainers
1
Weekly downloads
 
Created
Source

Extract Colors

package version npm bundle size Statements Dependencies GNU GPL software License

Extract color palettes from images.
Simple use, < 5ko minified, fast process and no dependencies for browser.
Dependency to canvas for node.js

3 examples of colors extraction

Requirements

Browsers

  • Firefox: 29+
  • Chrome: 33+
  • Edge: 12+
  • Opera: 19+
  • Safari: 8+
  • Webview Android: 4.4.3+
  • Samsung Internet: 2.0+
  • Internet Explorer

Node

  • Node.js: 0.12+

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
  },
  ...
]
FieldExampleTypeDescription
hex#62342bStringcolor in hexadecimal string
red98Integerred canal from 0 to 255
green52Integergreen canal from 0 to 255
blue43Integerblue canal from 0 to 255
area0.5915Numberarea of the color and his neighbouring colors from 0 to 1
saturation0.2156862Numbercolor 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.

ParamTypeDescription
redNumberRed channel integer from 0 to 255
greenNumberGreen channel integer from 0 to 255
blueNumberBlue channel integer from 0 to 255
[hex]NumberOptional 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

ParamTypeDescription
colorColorColor 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

ParamTypeDescription
saturationImportanceNumberDetermine the weight of the saturation for the calcul (from 0 to 1)
maxCountNumberNumber of pixels in the image.

module.exports.getSaturation() ⇒ Number

Saturation of the color from 0 to 1.

Kind: instance method of module.exports

ColorsExtractor

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

ParamTypeDescription
imageImageSource image
pixelsNumberMaximum number of pixels for process

Browser~extractColorsFromImageData(imageData, [options]) ⇒ Array.<Object>

Extract colors from an ImageData object.

Kind: inner method of Browser

ParamTypeDescription
imageDataImageData
[options]ObjectOptional data
[options.pixels]StringTotal pixel number of the resized picture for calculation
[options.distance]StringFrom 0 to 1 is the color distance to not have near colors (1 distance is between white and black)
[options.saturationImportance]StringPower of the saturation weight during the process (0 is not used, 1 is only saturation and not area size)
[options.splitPower]StringApproximation power in the first color splitting during process (from 2 to 16)
[options.colorValidator]StringCallback with test to enable only some colors

Browser~extractColorsFromImage(image, [options]) ⇒ Array.<Object>

Extract colors from an Image object.

Kind: inner method of Browser

ParamTypeDescription
imageImage
[options]ObjectOptional data
[options.pixels]StringTotal pixel number of the resized picture for calculation
[options.distance]StringFrom 0 to 1 is the color distance to not have near colors (1 distance is between white and black)
[options.saturationImportance]StringPower of the saturation weight during the process (0 is not used, 1 is only saturation and not area size)
[options.splitPower]StringApproximation power in the first color splitting during process (from 2 to 16)
[options.colorValidator]StringCallback with test to enable only some colors

Browser~extractColorsFromSrc(src, [options]) ⇒ Array.<Object>

Extract colors from a path. The image will be downloaded.

Kind: inner method of Browser

ParamTypeDescription
srcString
[options]ObjectOptional data
[options.pixels]StringTotal pixel number of the resized picture for calculation
[options.distance]StringFrom 0 to 1 is the color distance to not have near colors (1 distance is between white and black)
[options.saturationImportance]StringPower of the saturation weight during the process (0 is not used, 1 is only saturation and not area size)
[options.splitPower]StringApproximation power in the first color splitting during process (from 2 to 16)
[options.colorValidator]StringCallback with test to enable only some colors

Browser~extractColors(picture, [options]) ⇒ Array.<Object>

Extract colors from a picture.

Kind: inner method of Browser

ParamTypeDescription
pictureString | Image | ImageDataSrc, Image or ImageData
[options]ObjectOptional data
[options.pixels]StringTotal pixel number of the resized picture for calculation
[options.distance]StringFrom 0 to 1 is the color distance to not have near colors (1 distance is between white and black)
[options.saturationImportance]StringPower of the saturation weight during the process (0 is not used, 1 is only saturation and not area size)
[options.splitPower]StringApproximation power in the first color splitting during process (from 2 to 16)
[options.colorValidator]StringCallback 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

ParamTypeDescription
imageImageSource image
pixelsNumberMaximum number of pixels for process

Node~extractColorsFromImageData(imageData, [options]) ⇒ Array.<Object>

Extract colors from an ImageData object.

Kind: inner method of Node

ParamTypeDescription
imageDataImageData
[options]ObjectOptional data
[options.pixels]StringTotal pixel number of the resized picture for calculation
[options.distance]StringFrom 0 to 1 is the color distance to not have near colors (1 distance is between white and black)
[options.saturationImportance]StringPower of the saturation weight during the process (0 is not used, 1 is only saturation and not area size)
[options.splitPower]StringApproximation power in the first color splitting during process (from 2 to 16)
[options.colorValidator]StringCallback with test to enable only some colors

Node~extractColorsFromSrc(src, [options]) ⇒ Array.<Object>

Extract colors from a path. The image will be downloaded.

Kind: inner method of Node

ParamTypeDescription
srcString
[options]ObjectOptional data
[options.pixels]StringTotal pixel number of the resized picture for calculation
[options.distance]StringFrom 0 to 1 is the color distance to not have near colors (1 distance is between white and black)
[options.saturationImportance]StringPower of the saturation weight during the process (0 is not used, 1 is only saturation and not area size)
[options.splitPower]StringApproximation power in the first color splitting during process (from 2 to 16)
[options.colorValidator]StringCallback with test to enable only some colors

Node~extractColors(picture, [options]) ⇒ Array.<Object>

Extract colors from a picture.

Kind: inner method of Node

ParamTypeDescription
pictureString | Image | ImageDataSrc, Image or ImageData
[options]ObjectOptional data
[options.pixels]StringTotal pixel number of the resized picture for calculation
[options.distance]StringFrom 0 to 1 is the color distance to not have near colors (1 distance is between white and black)
[options.saturationImportance]StringPower of the saturation weight during the process (0 is not used, 1 is only saturation and not area size)
[options.splitPower]StringApproximation power in the first color splitting during process (from 2 to 16)
[options.colorValidator]StringCallback with test to enable only some colors

Keywords

FAQs

Package last updated on 31 Oct 2020

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