JSPM

  • Created
  • Published
  • Downloads 30794
  • Score
    100M100P100Q145410F
  • License MIT

Extract color palettes from images

Package Exports

  • extract-colors
  • extract-colors/lib/extract-colors.cjs
  • extract-colors/lib/extract-colors.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (extract-colors) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Extract Colors

package version npm min bundle size npm gzip bundle size zero dependency CI code coverage MIT License Downloaded

Extract color palettes from images.
Simple use, tree shaking, fast process and no dependencies for browser. Need image reader dependence for node.js

Website | Demo | Guide

3 examples of colors extraction

Install

For browser

npm install --save extract-colors

For node.js

Need to install an ImageData extractor like get-pixels

npm install --save extract-colors get-pixels

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, HTMLImageElement or ImageData).

Node.js example

const path = require("path");
const getPixels = require("get-pixels");
const { extractColors } = require("extract-colors");

const src = path.join(__dirname, "./my-image.jpg");

getPixels(src, (err, pixels) => {
  if (!err) {
    const data = [...pixels.data];
    const [width, height] = pixels.shape;

    extractColors({ data, width, height }).then(console.log).catch(console.log);
  }
});

This example use get-pixels but you can change the lib. Just send the ImageData object to extractColors(imageData).

ExtractorOptions

const options = {
  pixels: 64000,
  distance: 0.2,
  colorValidator: (red, green, blue, alpha = 255) => alpha > 250,
  defaultColors: ["dominants", "accents"],
};

extractColors(src, options).then(console.log).catch(console.error);

pixels
Total pixel number of the resized picture for calculation
Type: Integer
Default: 64000

fastDistance
From 0 to 1 is the RGB color distance to not have near colors (1 distance is between white and black). Increase the value accelerate the calculation and reduce the output list of colors.
Type: Number
Default: distance / 2 or 0.2

distance
Minimum distance between two colors otherwise the colors will be merged (from 0 to 1). Calculation with the Lab* CIE deltaE color distance.
Type: Number
Default: fastDistance * 2 or 0.22

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 client side CORS (the server side images need authorizations too)
Type: String
Default: ""

requestMode
Only for Web Workers in browser: it's used to determine if cross-origin requests lead to valid responses, and which properties of the response are readable
Type: String
Default: cors

colorClassifications
List of classified types of colors
Type: String[]
Default: [ "dominants", "accents", "dominantsLight", "dominantsMidtone", "dominantsDark", "accentsLight", "accentsMidtone", "accentsDark", "dullests", "vivids", "dullestsLight", "dullestsMidtone", "dullestsDark", "vividsLight", "vividsMidtone", "vividsDark", "lightests", "midtones", "darkests", "warmest", "coolest", "warmestLight", "warmestMidtone", "warmestDark", "coolestLight", "coolestMidtone", "coolestDark" ]

defaultColors
Default classified colors if no colors founds in the image
Type: Boolean or { [colorClassifications]?: Boolean, Number or () => Number } Default: false

defaultMainColor Default classified color if no colors found in the image and if defaultColors is not false
Type: Number
Default: 0x0077ff


Return of the promise

List colors and colors classified with the followed properties:

{
  // List of cleaned colors
  list: [
    {
      hex: number,​​ // from 0x000000 to 0xFFFFFF
      hexString: "#858409", // from "#000000" to "#FFFFFF"
      area: number, // from 0 to 1
      rgb: [number, number, number], // RGB colors (from 0 to 255 by chanel)
      hsl: [number, number, number], // HSL colors (from 0 to 1 by chanel)
      ecHsl: [number, number, number], // Fake HSL calculated from CIELAB colors (from 0 to 1 by chanel)
      lab: [number, number, number], // CIELAB colors
      count: number // number of pixel near of this color or near colors (determined with distance and fastDistance)
    },
    ...
  ],
  dominants: [...], // List of dominants cleaned colors
  accents: [...], // List of accents cleaned colors
  ...
}

Colors details

Property Example Type Description
hex 0x858409 String color in hexadecimal number
hexString #858409 String color in hexadecimal string with # at start
area 0.0004 Number area of the color and his neighbouring colors from 0 to 1
rgb [133, 132, 9] [uint, uint, uint] red, green and blue canals from 0 to 255
hsl [0.165 0.873 0.278] [Number, Number, Number] hue, saturation and lighness values from 0 to 1
lab [53.503, -12.585, 56.463] [Number, Number, Number] CIELAB color space
ecHsl [0.785, 0.457, 0.535] [Number, Number, Number] custom values HSL like with more human perceptual space than real HSL
count 1337 uint number of pixel near of this color or near colors

License

MIT

Copyright (c) 2025-present, Damien Doussaud