npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@jimmyclchu/image-palette

v1.0.0

Published

Extract color palettes from images with multiple algorithms and output formats

Readme

Image Palette

Extract color palettes from images using different algorithms. Works in both browser and Node.js.

Installation

npm install @jimmyclchu/image-palette

For Node.js, you'll also need canvas:

npm install canvas

Quick Start

import { extractPalette } from '@jimmyclchu/image-palette';

// Basic usage
const colors = await extractPalette('path/to/image.jpg');
console.log(colors); // ['#ff5733', '#33ff57', '#3357ff', ...]

// With options
const palette = await extractPalette(imageFile, {
  colorCount: 8,
  algorithm: 'k-means',
  format: 'rgb'
});

Algorithms

  • median-cut (default) - Fast and reliable
  • k-means - Higher quality, slower
  • octree - Good color diversity
  • dominant - Simple frequency-based extraction

Options

{
  colorCount: 5,           // Number of colors to extract
  algorithm: 'median-cut', // Algorithm to use
  format: 'hex',           // 'hex', 'rgb', or 'hsl'
  preset: 'custom',        // 'fast', 'quality', or 'custom'
  
  // K-means specific
  seed: 42,                // For reproducible results
  iterations: 50,          // Number of iterations
  
  // Filtering
  filters: {
    minDistance: 0.1,      // Minimum color difference (0-1)
    excludeDark: 0.2,      // Exclude colors darker than threshold
    excludeLight: 0.8,     // Exclude colors lighter than threshold
    excludeGrayscale: true // Remove grayscale colors
  },
  
  // Output
  includeMetadata: false   // Include frequency and pixel count data
}

Input Types

Browser:

  • HTMLImageElement
  • HTMLCanvasElement
  • File / Blob
  • Base64 strings
  • Image URLs

Node.js:

  • File paths
  • Buffer objects
  • Base64 strings

Examples

Different Output Formats

// Hex colors (default)
const hex = await extractPalette(image, { format: 'hex' });
// ['#ff5733', '#33ff57', '#3357ff']

// RGB objects
const rgb = await extractPalette(image, { format: 'rgb' });
// [{ r: 255, g: 87, b: 51 }, { r: 51, g: 255, b: 87 }]

// HSL objects
const hsl = await extractPalette(image, { format: 'hsl' });
// [{ h: 9, s: 100, l: 60 }, { h: 129, s: 100, l: 60 }]

With Metadata

const palette = await extractPalette(image, { 
  includeMetadata: true,
  colorCount: 5
});

console.log(palette);
// [
//   { color: '#ff5733', frequency: 0.34, pixels: 1247 },
//   { color: '#33ff57', frequency: 0.28, pixels: 1031 },
//   ...
// ]

Performance Presets

// Fast extraction
const fastColors = await extractPalette(image, { preset: 'fast' });

// Higher quality
const qualityColors = await extractPalette(image, { preset: 'quality' });

Advanced Filtering

const filtered = await extractPalette(image, {
  colorCount: 10,
  filters: {
    minDistance: 0.15,     // Colors must be 15% different
    excludeDark: 0.1,      // No very dark colors
    excludeLight: 0.9,     // No very light colors
    excludeGrayscale: true // No gray colors
  }
});

Browser File Upload

const input = document.querySelector('input[type="file"]');
input.addEventListener('change', async (e) => {
  const file = e.target.files[0];
  const colors = await extractPalette(file, { colorCount: 6 });
  console.log(colors);
});

Node.js File Processing

import { extractPalette } from '@jimmyclchu/image-palette';
import fs from 'fs';

// From file path
const colors1 = await extractPalette('./photo.jpg');

// From buffer
const buffer = fs.readFileSync('./photo.png');
const colors2 = await extractPalette(buffer);

License

MIT