Skip to content

roteKlaue/node-colour-extractor

 
 

Repository files navigation

ℹ️ For GraphicsMagick version please see tag v0.2.1.


CI

colour-extractor-buffered

Extracts colour palettes from photos using k-means clustering in LAB colour space.

sample 1 sample 2 sample 3

Installation

Is as simple as with any other Node.js module:

$ npm install colour-extractor-buffered

Note: This module contains native Rust libraries. Please open an issue if your platform isn't supported.

Usage

colour-extractor-buffered exports four functions:

const { topColours, topColoursHex, topColoursBuffer, topColoursBuffer } = require('colour-extractor-buffered');
const colours = await topColours('./photos/cats/tigre.jpg');
// const colours = await topColoursBuffer(/* an image buffer here */);
console.log(colours);
// => [ [158, 64, 75], ... ]

topColours function needs a path to your image (see below for supported formats) and it resolves to an Array containing RGB triplet for each prominent colour:

[
  [46, 70, 118],
  [0, 0, 2],
  [12, 44, 11]
]

topColoursHex on the other hand returns hex codes (with # included):

const hexColours = await topColoursHex('./photos/cats/tigre.jpg');
// const hexColours = await topColoursHexBuffer(/* an image buffer here */);
console.log(hexColours);
// => ["#2e4676", "#000002", "#0c2c0b"]

Supported image formats

All major image formats are supported, including PNG, JPG and WebP. Please see image-rs's documentation for a full list. Note that file extension is used for determining the image type.

How does it work?

Here's the simplified algorithm:

  1. Image is scaled down to 48x48px with a fast nearest-neighbour algorithm.
  2. Colours are grouped into up to 16 clusters using k-means clustering.
  3. Identified clusters are further refined using CIEDE2000 colour distance, removing any duplicates.

Licence

colour-extractor-buffered is published under MIT license.

Photos used the examples above can be found on Unsplash:

About

Extract colour palettes from images 🎨

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 52.1%
  • Rust 47.9%