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

swatchme

v0.1.0

Published

Extract dominant colors from images using k-means clustering - browser-only, no dependencies

Readme

swatchme

Extract dominant colors from images using k-means clustering — browser-only, zero dependencies.

Install

npm install swatchme

Usage

import { extractColors, generatePaletteCanvas } from 'swatchme';

// From file input
const file = document.querySelector('input[type="file"]').files[0];
const colors = await extractColors(file);

console.log(colors);
// [
//   { hex: '#4A90D9', percentage: 32.5, rgb: { r: 74, g: 144, b: 217 } },
//   { hex: '#2C3E50', percentage: 24.1, rgb: { r: 44, g: 62, b: 80 } },
//   ...
// ]

// Generate a palette canvas
const canvas = generatePaletteCanvas(colors, 500, 100);
document.body.appendChild(canvas);

API

extractColors(input, options?)

Extract dominant colors from an image.

Input types:

  • HTMLImageElement
  • HTMLCanvasElement
  • ImageData
  • File or Blob
  • URL string (including data URLs)

Options:

| Option | Type | Default | Description | |--------|------|---------|-------------| | numColors | number | 5 | Number of colors to extract | | quality | number | 50 | Quality 0-100 (lower = faster) | | excludeWhite | boolean | false | Filter out near-white colors | | excludeBlack | boolean | false | Filter out near-black colors | | minContrast | number | 0 | Minimum distance between colors | | colorThreshold | number | 30 | Threshold for white/black detection |

Returns: Promise<ExtractedColor[]>

generatePaletteCanvas(colors, width?, height?)

Create a canvas element showing the color palette.

const canvas = generatePaletteCanvas(colors, 600, 120);

generatePaletteDataURL(colors, width?, height?)

Get the palette as a PNG data URL.

const dataUrl = generatePaletteDataURL(colors);
img.src = dataUrl;

getContrastColor(rgb)

Get black or white text color for optimal contrast on a background.

import { getContrastColor } from 'swatchme';

const textColor = getContrastColor({ r: 74, g: 144, b: 217 });
// '#FFFFFF'

Types

interface ExtractedColor {
  hex: string;        // e.g., "#4A90D9"
  percentage: number; // e.g., 32.5
  rgb: { r: number; g: number; b: number };
}

interface ExtractOptions {
  numColors?: number;
  quality?: number;
  excludeWhite?: boolean;
  excludeBlack?: boolean;
  minContrast?: number;
  colorThreshold?: number;
}

Development

cd js

# Install dependencies
npm install

# Run example dev server
npm run dev

# Build library
npm run build

How it works

  1. Image is loaded and optionally downscaled based on quality setting
  2. Pixels are extracted via Canvas API
  3. K-means++ clustering finds dominant color clusters
  4. Colors are filtered (white/black exclusion, min contrast)
  5. Results sorted by dominance percentage

License

MIT