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

agglomerative-clustering

v4.3.2

Published

Colour palette extraction and image quantization with exact Ward clustering, in WebAssembly. Cluster an image once, then cut it to a palette of any size.

Readme

agglomerative-clustering

Exact Ward colour quantization for JavaScript. Cluster an image once, then cut it to a palette of any size in under a millisecond. Palettes of different sizes nest, so a colour-count slider, a set of 8, 16, 32 and 64 colour versions, or an error curve cost one clustering, not one per size.

Try the interactive demo — quantize your own images in the browser.

npm license types

A photo cut from 2 to 64 colours, over a swatch bar that grows by splitting one colour at a time

Every palette size from one clustering, in 115 ms (a 768×512 photo, 2 to 64 colours, including all 63 cuts).

Why this one

Most colour quantizers answer one question: give me k colours. Ask for a different k and they start over. This one builds the whole tree of an image's colours once, and a palette of any size is a cut of it: the tree merged back down to k colours.

  1. Every palette size from one pass, and the palettes nest. The 16-colour palette is the 32-colour one with pairs merged, so swatches don't jump around as the count changes. A cut takes under a millisecond. Median cut starts over for each k:

    Two rows of swatches for 4 to 12 colours: median cut (MMCQ) reshuffles, this library splits one swatch at a time

    Other quantizers start over for each k (here MMCQ, color-thief's median cut, in the order it returns); these palettes nest.

  2. The lowest squared error on photos, at 64 colours and up. On the 24 Kodak photos at 64, 128 and 256 colours, no other library has lower mean squared error on any photo; at 64 the nearest, image-q Wu, has 1.05× ours. At 16 colours Wu and libimagequant do better on a few photos (4 and 5 of 24), by up to 4.2% (on ΔE2000, libimagequant is level at 128 and slightly ahead at 256). See Benchmarks.

  3. It knows the image. Each colour's share of the pixels (buildSwatches), the error at every palette size, so you can pick a size from an error target (getClusteringInfo), and the best fit of k entries from a fixed palette such as a thread chart (chooseFrom).

  4. Real-world input. Transparency (a logo's background never ends up in its palette), premultiplied pixels (Electron, node-canvas, WebGL), BGRA and other pixel layouts, and a worker entry that keeps the page responsive.

  5. Runs everywhere. Node and browsers, about 42 KB of WebAssembly gzipped, no dependencies, deterministic output.

Install

npm install agglomerative-clustering

It runs on Node 22.12+, and in browsers with WebAssembly SIMD (Chrome and Edge 91, Firefox 89, Safari 16.4). Deno, Bun, Cloudflare Workers, serverless functions, Electron and browser extensions work too: see Environments.

Upgrading? See Migrating.

Examples

In Node, with sharp:

import sharp from 'sharp';
import { buildPalette, quantize } from 'agglomerative-clustering';

const { data, info } = await sharp('photo.jpg').rotate().ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const image = { data, format: 'rgba', width: info.width };   // in TypeScript: format: 'rgba' as const

const palette = await buildPalette(image, 8);                   // flat bytes: [r, g, b, r, g, b, ...]
const reduced = await quantize(image, 16, { dither: true });  // the image, in 16 colours

await sharp(Buffer.from(reduced.data), { raw: { width: info.width, height: info.height, channels: 4 } }).toFile('photo-16.png');

In the browser, with a canvas:

import { quantize } from 'agglomerative-clustering';

const ctx = canvas.getContext('2d');
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);

ctx.putImageData(await quantize(imageData, 16, { dither: true }), 0, 0);   // ImageData in, ImageData out

Swatches, with each colour's share of the image:

import { buildSwatches } from 'agglomerative-clustering';

const { palette, shares } = await buildSwatches(imageData, 6);   // six colours; the shares sum to 1
const hex = (i) => '#' + [...palette.subarray(3 * i, 3 * i + 3)].map((v) => v.toString(16).padStart(2, '0')).join('');
const legend = Array.from(shares, (share, i) => `${hex(i)} ${(share * 100).toFixed(1)}%`);   // ['#1a2b3c 31.4%', …]

Entries keep their place as k grows (split order), so palettes nest. For swatches as objects, the largest share first, or a dominant colour, see Heaviest first.

A colour-count slider: cluster once, then cut and map as often as you like.

import { buildClustering, quantizeWithPalette } from 'agglomerative-clustering';

const clustering = await buildClustering(imageData);                   // the expensive step, done once

slider.oninput = async () => {
  const palette = await clustering.getPalette(Number(slider.value));        // under 1 ms for any k
  ctx.putImageData(await quantizeWithPalette(imageData, palette), 0, 0);   // a few ms at 768×512
};

Input is decoded pixels (a bare Uint8Array of RGBA bytes, an ImageData, or { data, format, width?, height? }): the formats, where pixels come from and how premultiplied alpha works are in the API reference, and decoding image files has a recipe for Node, browsers and edge runtimes. For a page that stays responsive while it clusters, use agglomerative-clustering/worker (see Workers). The clustering.… methods run on the calling thread; off the thread, createWorker().cluster(image) gives a handle with the same methods.

Gallery

What people build with it. Each card has the code that makes it, and a guide.

Pixel art and posterising

One photo at 4, 8, 16 and 32 colours with ordered dithering

One photo at 4, 8, 16 and 32 colours, ordered dithering. The clustering is built once; each size is a cut and a map.

import { buildClustering } from 'agglomerative-clustering';

const clustering = await buildClustering(image);
const sizes = [4, 8, 16, 32];
const frames = await Promise.all(sizes.map((k) => clustering.quantizeToIndices(image, k, { dither: 'ordered' })));

Guides: GIFs and palette PNGs; choosing a dither method for the ordered method.

E-ink and limited displays

A photo beside its rendering in the seven inks of an e-ink panel

The photo beside its rendering in seven inks, Floyd–Steinberg. The picture uses ditherInLight, a short recipe in the API reference (linked below), not an export, which mixes in light, as the panel does; the code below is the plain call, and ditherInLight is a drop-in replacement for it.

import { quantizeToIndicesWithPalette } from 'agglomerative-clustering';

const inks = new Uint8Array([0,0,0, 255,255,255, 0,255,0, 0,0,255, 255,0,0, 255,255,0, 255,128,0]);   // black white green blue red yellow orange
const { indices } = await quantizeToIndicesWithPalette(image, inks, { dither: 'floyd-steinberg' });   // one entry of `inks` per pixel

Guides: Fixed palettes; the recipe: Dithering to a few inks: mix in light.

Threads, bricks, paint-by-number

A photo beside the 12 colours chosen from a fixed set of 24, and its rendering in them

chooseFrom picks the 12 entries of a fixed set (here 24 illustrative colours, not a real chart) that best fit the photo.

import { buildClustering, quantizeToIndicesWithPalette } from 'agglomerative-clustering';

const clustering = await buildClustering(image);
const choice = await clustering.chooseFrom(chart, 12);   // chart: a flat RGB Uint8Array of the colours you can buy
const { indices } = await quantizeToIndicesWithPalette(image, choice.palette);   // choice.indices: positions in `chart`

Guide: Fixed palettes, with a stitch count for the shopping list.

Transparency

An app icon with a soft shadow, and its 24-colour version over light and dark backgrounds

An app icon with a soft shadow, reduced to 24 colours. The transparent background takes no palette entries, the soft shadow counts in proportion to its opacity, and each pixel keeps its own alpha, so the result sits on any background. The gallery's GIFs are the library's own quantizeToIndices output, written with gifenc: see the GIF and palette PNG recipe.

import { quantize } from 'agglomerative-clustering';

const reduced = await quantize(image, 24, { dither: 0.5 });   // same type back, each pixel's alpha kept

Docs: Transparency.

Swatches for design tools

A photo beside eight swatches with a bar for each colour's share

buildSwatches gives the palette and each colour's share of the image, ready for bars or a legend.

import { buildSwatches } from 'agglomerative-clustering';

const { palette, shares } = await buildSwatches(image, 8);   // the shares sum to 1

Guides: Swatches and choosing a palette size; hex strings, heaviest first.

Animation: ordered dithering keeps still areas still

A ball moving over a still photo, in 12 colours

One palette for the clip, then ordered dithering: a pixel's colour depends only on its colour and position, so still areas stay still. With Floyd–Steinberg, 4.1% of the pixels away from the ball changed in each frame; with ordered, 0%.

import { buildClustering, quantizeToIndicesWithPalette } from 'agglomerative-clustering';

const palette = await (await buildClustering(image)).getPalette(12);   // one palette for every frame
const frame = await quantizeToIndicesWithPalette(image, palette, { dither: 'ordered' });   // repeat for each frame

Guides: Animated GIFs and sets of images, video frames.

Benchmarks

The 24 Kodak photos (768×512) at 64 colours. "Error vs ours" is mean squared error relative to this library, so 1.08× means 8% more error. Every palette is mapped to pixels the same way, so palettes are compared like for like. Times are medians per image, in WASM on Node 24 on a laptop (Intel Core i7-1185G7), for version 4.3.0.

| Library | PSNR | Error vs ours | One palette | All four palettes (16, 64, 128, 256) | |---|---|---|---|---| | agglomerative-clustering | 35.72 dB | 1.00× | 53 ms | 53 ms | | agglomerative-clustering, precision: 6 | 35.70 dB | 1.00× | 18 ms | 18 ms | | libimagequant (pngquant's engine) | 35.39 dB | 1.08× | 64 ms | 262 ms | | image-q Wu | 35.51 dB | 1.05× | 433 ms | 1989 ms | | image-q NeuQuant | 34.41 dB | 1.35× | 361 ms | 2147 ms | | MMCQ (color-thief's algorithm) | 32.53 dB | 2.08× | 20 ms | 82 ms | | image-q RgbQuant | 31.95 dB | 2.38× | 194 ms | 791 ms |

This library clusters once and cuts four times, so its two time columns match; the others start over for each size. All rows are from one run. Full results at every palette size, on synthetic images too, with ΔE2000 and the method, are in bench/COMPARISON.md; reproduce them with node bench/run.js. These numbers are a snapshot, so expect them to move with the hardware and the versions.

Large photos. Clustering a photo with about 150,000 to 350,000 distinct colours takes 0.5 to 1.2 s at full precision and 0.14 to 0.29 s with precision: 6; 4.3 is 1.5% to 20.7% faster than 4.2 at full precision in RGB on these, with identical output.

How it works

 image ──▶ buildClustering ──▶ a clustering ──▶ cut to k colours ──▶ a palette ──▶ map the pixels ──▶ reduced image
           once, the slow step bytes you can     under 1 ms,          flat RGB       a few ms,
                               store             any k                bytes          any palette

The one-shot functions (buildPalette, quantize, …) do every step for you. To try many palette sizes, call buildClustering once and cut it as often as you like.

| You want | from the image | from a clustering you hold¹ | with a palette you supply | |---|---|---|---| | a palette (flat RGB) | buildPalette | clustering.getPalette(k) | | | a palette with each colour's share | buildSwatches | clustering.getSwatches(k) | | | the image reduced to those colours | quantize | clustering.quantize(image, k) | quantizeWithPalette | | palette indices (GIF, PNG-8) | quantizeToIndices | clustering.quantizeToIndices(image, k) | quantizeToIndicesWithPalette | | its error curve, for choosing a palette size | | clustering.getClusteringInfo() | | | the k entries of a fixed palette (threads, bricks) that best fit | | clustering.chooseFrom(palette, k) | |

A clustering you hold has methods that take the colours from it, and …WithPalette takes colours you supply; with neither, the image is clustered for you. buildClustering builds a clustering, and init loads the WebAssembly module (optional). Most code only needs the first column.

The names say how much work a call does (build… clusters, get… cuts what you hold, quantize… maps pixels): see Naming.

¹ A clustering from storage is plain bytes: wrap it with new Clustering(bytes) to get the methods back, or off the thread load it with worker.load(bytes). See Clustering methods.

Palettes come in split order: the palette for k + 1 is the one for k with one colour split in two, so entries keep their place as k grows (they are not sorted by share). The options are precision (6 is about 3× faster at practically the same quality), colorSpace ('rgb' or 'oklab'), alpha, transparent, dither (Floyd–Steinberg, Atkinson, or ordered for animation, which keeps still areas still), width, signal and onProgress: see the API reference.

When something else fits better

The gallery is where this library does well: tools where people choose the number of colours, several sizes of one image, and the most accurate palette for a given size. It is the wrong tool for these:

  • Shrinking PNG files in a build pipeline. Use sharp or pngquant, which run libimagequant natively and are tuned for compression.
  • A handful of vivid "dominant" swatches for a UI theme. color-thief or node-vibrant are lighter and pick vivid or salient colours rather than the lowest-error ones.
  • Palettes that need their own alpha values, as palette PNGs with translucent entries do. Pixels are weighted by opacity, but palette colours are opaque RGB, and each pixel keeps its own alpha.

Documentation

  • Guides: swatches and choosing a palette size, GIFs and palette PNGs, animations, live video, workers, fixed palettes (e-ink, thread, bricks), batches of images, converting the output, and moving from color-thief, node-vibrant or image-q.
  • API reference: input formats, options, choosing a colour space, transparency, Palette and Clustering, cancelling and progress, errors, the worker entry, TypeScript, and every function.
  • Environments: CDNs, Deno, Bun, Cloudflare Workers, serverless functions, Next.js, Angular and other bundlers, Electron, browser extensions, plugin hosts, pnpm and Yarn, and testing.
  • Performance and limits: servers and worker pools, memory, and large images.
  • Troubleshooting: what an error or a surprising result means, and what to do.
  • Migrating from 3.x, 2.x and 1.x, and the CHANGELOG.
  • Stability and versioning: what won't change within a major version.
  • How the algorithm works, with architecture notes for contributors in CLAUDE.md.
  • Media credits: the gallery images are made by docs/media/make.mjs from the library itself.
  • Roadmap: what is planned, and what was tried and dropped.

Demo

git clone https://github.com/RohanFredriksson/agglomerative-clustering && cd agglomerative-clustering
scripts/setup-emsdk.sh && source ~/.local/share/emsdk/emsdk_env.sh   # Emscripten, the version CI uses
npm install && npm run build
npm run demo                   # then open http://localhost:8080/demo/

The demo clusters an image once, then re-cuts and re-maps it live as you drag the palette-size and dithering sliders.

Questions

Have a question about using it? Ask in Discussions.

License

MIT © Rohan Fredriksson