auto-palette
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

Auto Palette

Auto Palette is a library that automatically extracts a prominent color palette from your image.

NPM Version License GitHub Actions Codacy Codecov FOSSA

Features

Color palette extracted from an image using Auto Palette

[!NOTE] Photo by Pixabay from Pexels: https://www.pexels.com/photo/yellow-pink-and-violet-tulips-52508/

❯ Automatically extracts color palette from image
❯ Provides detailed color information color, name, position and population
❯ Supports multiple color extraction algorithms (dbscan, dbscan++, kmeans )
❯ Supports multiple image sources (HTMLImageElement, HTMLCanvasElement, ImageData)
❯ Supports both Browser and Node.js
❯ Zero dependencies

Installation

Using npm:

$ npm install auto-palette

Using yarn:

$ yarn add auto-palette

Using pnpm:

$ pnpm add auto-palette

Usage

// ESM
import { Palette } from 'auto-palette';

// CJS
const { Palette } = require('auto-palette');

const palette = Palette.extract(image);
const swatches = palette.findSwatches(8, 'vivid');
for (const swatch of swatches) {
  console.log({
    name: swatch.name,              // The similar color name of the swatch
    color: swatch.color.toString(), // The color of the swatch
    position: swatch.position,      // The position of the swatch in the image
    population: swatch.population,  // The pixel count of the swatch
  });
}

Examples

Source Image Default Vivid Muted
Yellow Pink and Violet Tulips Default color palette Vivid color palette Muted color palette
Closeup photo of doughnuts Default color palette Vivid color palette Muted color palette
Close-up Photo of Assorted Colored Chalks Default color palette Vivid color palette Muted color palette

API

For more detailed information, please refer to the documentations in the docs directory.

Palette

extract(image: ImageSource, options?: Options): Palette

Extracts a color palette from the given image source(HTMLImageElement, HTMLCanvasElement or ImageData).
It takes an image source and an optional Options object as parameters.

const options: Options = {
  algorithm: 'dbscan',
  samplingRate: 0.5,
  maxSwatches: 16,
  filters: [luminanceFilter(0.2, 0.8)],
};
const palette = Palette.extract(image, options);

The Options can include properties such as algorithm, samplingRate, maxSwatches, and filters.

interface Options {
  // The color extraction algorithm to use. Default is 'dbscan'.
  algorithm?: 'dbscan' | 'dbscan++' | 'kmeans';
  // The sampling rate of the image. Default is 1.0.
  samplingRate?: number;
  // The maximum number of swatches to extract. Default is 256.
  maxSwatches?: number;
  // The color filters to apply. Default is [opacityFilter()].
  filters?: ColorFilter[];
}

findSwatches(n: number, theme?: Theme): Swatch[]

Finds the best n swatches in the palette.
The “best” swatches are determined based on their population and optionally a theme. The theme can be basic, vivid, muted, light or dark. Default is basic.

const swatches = palette.findSwatches(5, 'light');

Contributing

Contributions are welcome! For detailed information on how to contribute, please refer to the CONTRIBUTING guidelines.

License

This library is distributed under the MIT License.See the LICENSE.

FOSSA Status

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.4.018latest

Version History

VersionDownloads (Last 7 Days)Published
1.4.018
1.3.42
1.3.32
1.3.21
1.3.10
1.3.00
1.2.00
1.1.00
1.0.10
1.0.00
0.1.10
0.1.00
0.0.20
0.0.10

Package Sidebar

Install

npm i auto-palette

Weekly Downloads

23

Version

1.4.0

License

MIT

Unpacked Size

492 kB

Total Files

9

Last publish

Collaborators

  • t28