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 🙏

© 2026 – Pkg Stats / Ryan Hefner

colors-from-image

v1.2.0

Published

A library for extracting color palettes from images using various algorithms

Readme

Color Extractor

Powers the website colorsfromimage.xyz

A powerful library for extracting color palettes from images using multiple algorithms:

  • Vibrant: Extracts vibrant and muted colors (inspired by Material Design)
  • Material Design: Creates a palette following Material Design color principles
  • Median Cut: Uses the median cut algorithm (similar to Photoshop's color reduction)
  • K-Means: Applies k-means clustering in Lab color space for perceptually accurate colors

Installation

bash
npm install colors-from-image

Usage

Basic Usage

const ColorExtractor = require('colors-from-image');

const image = document.getElementById('myImage');

const colors = ColorExtractor.extractColors(image);

colors.forEach(color => {
    const formattedColor = ColorExtractor.formatColor(color.color);
    console.log(Color: ${formattedColor.hex}, Frequency: ${color.frequency});
});

Using Different Extraction Methods

// Extract using Material Design method
const materialColors = ColorExtractor.extractColors(image, 'material');

// Extract using Median Cut method with 10 colors
const medianCutColors = ColorExtractor.extractColors(image, 'mediancut', { colorCount: 10 });

// Extract using K-Means clustering with 8 colors
const kMeansColors = ColorExtractor.extractColors(image, 'kmeans', { colorCount: 8 });

Working with Canvas or ImageData

// With canvas ImageData
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');

// ... draw to canvas ...
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const colors = ColorExtractor.extractColors(imageData);

Node.js Usage with Canvas

const { createCanvas, loadImage } = require('canvas');
const ColorExtractor = require('color-extractor');

async function extractColorsFromFile(filePath) {
    // Load the image
    const image = await loadImage(filePath);
    // Create canvas and draw image
    const canvas = createCanvas(image.width, image.height);
    const ctx = canvas.getContext('2d');
    ctx.drawImage(image, 0, 0);
    // Get image data
    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    // Extract colors
    return ColorExtractor.extractColors(imageData);
}

// Usage
extractColorsFromFile('path/to/image.jpg')
    .then(colors => {
        colors.forEach(color => {
            const formattedColor = ColorExtractor.formatColor(color.color);
            console.log(Color: ${formattedColor.hex}, Frequency: ${color.frequency});
        });
    });

Formatting Colors

const rgb = [255, 100, 50];
const formattedColor = ColorExtractor.formatColor(rgb);
console.log(formattedColor.hex); // "#ff6432"
console.log(formattedColor.rgb); // "rgb(255, 100, 50)"
console.log(formattedColor.rgba); // "rgba(255, 100, 50, 1.0)"
console.log(formattedColor.hsl); // "hsl(14, 100%, 60%)"
console.log(formattedColor.values.rgb); // [255, 100, 50]
console.log(formattedColor.values.hsl); // [14, 100, 60]

API Reference

Main Functions

extractColors(image, method, options)

Extracts a color palette from an image.

  • Parameters:

    • image (HTMLImageElement|ImageData): The image to extract colors from
    • method (string, optional): Extraction method to use: 'vibrant', 'material', 'mediancut', or 'kmeans'. Default: 'vibrant'
    • options (object, optional):
      • colorCount (number): Number of colors to extract (for mediancut and kmeans). Default: 8
      • maxPixels (number): Maximum number of pixels to sample for performance. Default: 10000
  • Returns: Array of color objects with properties:

    • color (Array): RGB color as [r, g, b]
    • frequency (number): Frequency of the color in the image
    • name (string, optional): Name of the color (for vibrant and material methods)

formatColor(rgb)

Formats an RGB color into various color formats.

  • Parameters:

    • rgb (Array): RGB color as [r, g, b]
  • Returns: Object with color in different formats:

    • hex (string): HEX color code
    • rgb (string): RGB color string
    • rgba (string): RGBA color string
    • hsl (string): HSL color string
    • values (object): Raw values
      • rgb (Array): RGB values as [r, g, b]
      • hsl (Array): HSL values as [h, s, l]

Algorithm Details

Vibrant

Extracts a palette of vibrant and muted colors, categorized as:

  • Vibrant
  • Light Vibrant
  • Dark Vibrant
  • Muted
  • Light Muted
  • Dark Muted

Material Design

Creates a palette following Material Design principles with:

  • Primary color
  • Accent colors
  • Light and dark neutral colors

Median Cut

Recursively divides the color space along the axis with the largest range until the desired number of colors is reached.

K-Means

Clusters colors in Lab color space (which is perceptually uniform) using the k-means++ algorithm for better initial centroids.

License

MIT