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

@ltcode/color-extractor

v1.0.0

Published

Extract dominant colors from images easily with JavaScript/TypeScript.

Downloads

10

Readme

@ltcode/color-extractor

Extract dominant colors from images easily using JavaScript/TypeScript. Compatible with Node.js and Browsers!

Installation

npm install @ltcode/color-extractor

Usage

📦 Node.js

import { extractColors } from '@ltcode/color-extractor';

// Extract 5 dominant colors in hexadecimal format
const colors = await extractColors('path/to/image.jpg');
console.log(colors); // ['#ff8040', '#2a5d84', '#f4e8d0', ...]

🌐 Browser

import { extractColors } from '@ltcode/color-extractor';

// File upload
const fileInput = document.querySelector('input[type="file"]');
fileInput.addEventListener('change', async (e) => {
  const file = e.target.files[0];
  const colors = await extractColors(file);
  console.log(colors);
});

// HTML image element
const img = document.querySelector('img');
const colors = await extractColors(img);

// Canvas
const canvas = document.querySelector('canvas');
const colors = await extractColors(canvas);

// Image URL (with CORS)
const colors = await extractColors('https://example.com/image.jpg');

Advanced Options

import { extractColors, extractDominantColor } from '@ltcode/color-extractor';

// Extract only the dominant color
const dominantColor = await extractDominantColor('image.jpg');

// Custom options
const colors = await extractColors('image.jpg', {
  maxColors: 8,        // Maximum number of colors (default: 5)
  quality: 5,          // Analysis quality (default: 10)
  format: 'rgb',       // Format: 'hex', 'rgb', 'hsl'
  ignoreLightColors: true,  // Ignore light colors
  ignoreDarkColors: false   // Ignore dark colors
});

Using with Buffer (Node.js)

import fs from 'fs/promises';
import { extractColors } from '@ltcode/color-extractor';

const imageBuffer = await fs.readFile('image.jpg');
const colors = await extractColors(imageBuffer);

Drag & Drop in Browser

const dropZone = document.getElementById('dropZone');

dropZone.addEventListener('drop', async (e) => {
  e.preventDefault();
  const files = e.dataTransfer.files;
  
  if (files.length > 0) {
    const colors = await extractColors(files[0]);
    displayColors(colors);
  }
});

API

extractColors(input, options?)

  • input:
    • Node.js: string (file path) | Buffer
    • Browser: string (URL) | File | HTMLImageElement | HTMLCanvasElement | ImageData
  • options: ColorExtractionOptions - Configuration options
  • Returns: Promise<ColorResult[]> - Array of colors

extractDominantColor(input, options?)

  • input: Same as extractColors
  • options: ColorExtractionOptions - Configuration options
  • Returns: Promise<ColorResult | null> - Dominant color

extractPalette(input, options?)

Alias for extractColors - extracts complete color palette.

Options (ColorExtractionOptions)

{
  maxColors?: number;        // Maximum colors (default: 5)
  quality?: number;          // Quality 1-50 (default: 10)
  format?: 'hex'|'rgb'|'hsl'; // Output format (default: 'hex')
  ignoreLightColors?: boolean; // Ignore light colors
  ignoreDarkColors?: boolean;  // Ignore dark colors
}

Supported Environments

  • Node.js 18+ (ES Modules)
  • Modern browsers (Chrome, Firefox, Safari, Edge)
  • Bundlers (Webpack, Vite, Rollup, Parcel)

Image Formats

  • Node.js: PNG, JPEG (basic implementation)
  • Browser: All formats supported by the browser

Browser Demo

Check out the interactive demo to test in your browser!

Examples

See more examples in the GitHub repository

License

MIT