https://github.com/josip/node-colour-extractor
Extract colour palettes from images đ¨
https://github.com/josip/node-colour-extractor
color-palette-generator colors image-processing nodejs photos
Last synced: 4 months ago
JSON representation
Extract colour palettes from images đ¨
- Host: GitHub
- URL: https://github.com/josip/node-colour-extractor
- Owner: josip
- License: mit
- Created: 2011-03-21T00:16:52.000Z (about 15 years ago)
- Default Branch: master
- Last Pushed: 2023-05-16T13:15:33.000Z (almost 3 years ago)
- Last Synced: 2025-04-09T02:46:16.992Z (about 1 year ago)
- Topics: color-palette-generator, colors, image-processing, nodejs, photos
- Language: Rust
- Homepage:
- Size: 807 KB
- Stars: 126
- Watchers: 6
- Forks: 20
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-projects - node-colour-extractor - Extract colour palettes from images đ¨ â126 `Rust` (đĻ Legacy & Inactive Projects)
README
> âšī¸ For GraphicsMagick version please see tag [v0.2.1](https://github.com/josip/node-colour-extractor/tree/v0.2.1).
---
[](https://github.com/josip/node-colour-extractor/actions/workflows/CI.yml)
# colour-extractor
Extracts colour palettes from photos using k-means clustering in LAB colour space.



## Installation
Is as simple as with any other Node.js module:
$ npm install @colour-extractor/colour-extractor
Note: This module contains native Rust libraries. Please open an issue if your platform isn't supported.
## Usage
`colour-extractor` exports two functions:
```js
const { topColours, topColoursHex } = require('@colour-extractor/colour-extractor');
const colours = await topColours('./photos/cats/tigre.jpg');
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:
```json
[
[46, 70, 118],
[0, 0, 2],
[12, 44, 11]
]
```
`topColoursHex` on the other hand returns hex codes (with `#` included):
```js
const hexColours = await topColoursHex('./photos/cats/tigre.jpg');
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](https://github.com/image-rs/image/blob/master/README.md#supported-image-formats) 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](https://en.wikipedia.org/wiki/K-means_clustering).
3. Identified clusters are further refined using [CIEDE2000 colour distance](https://en.wikipedia.org/wiki/Color_difference#CIEDE2000), removing any duplicates.
## Licence
colour-extractor is published under MIT license.
Photos used the examples above can be found on Unsplash:
* https://unsplash.com/photos/7QaYj09Wbhs
* https://unsplash.com/photos/pPRT4CLykp8
* https://unsplash.com/photos/ttF84ygvliI