A WebAssembly-based color quantization module for JavaScript and TypeScript environments. It uses the NEUQUANT algorithm via Rust's color_quant
library to reduce RGBA pixel data to a limited color palette, returning both indexed pixels and the color map.
- Fast and compact NEUQUANT implementation in WebAssembly
- Works directly with
Uint8Array
buffers - Customizable palette size
- Flat output buffer containing indexed pixels and RGBA palette
npm install wasm-color-quant
Quantizes an RGBA image using NEUQUANT and returns a flat buffer containing the indexed pixel data followed by the RGBA palette.
Name | Type | Description |
---|---|---|
pixels |
Uint8Array |
Flat RGBA pixel buffer (4 bytes per pixel) |
sample_factor |
number |
Quality/sample trade-off (e.g. 10) |
palette_size |
number |
Number of colors to generate (e.g. 256) |
A Uint8Array
where:
- The first
pixels.length / 4
bytes are indices. - The remaining
palette_size * 4
bytes are the color palette (RGBA).
import init, { quantize_with_palette } from "wasm-color-quant";
await init();
const pixels = new Uint8Array([...]); // Flat RGBA input
const sampleFactor = 10;
const paletteSize = 128;
const result = quantize_with_palette(pixels, sampleFactor, paletteSize);
const indexedPixels = result.slice(0, pixels.length / 4);
const colorPalette = result.slice(pixels.length / 4);
console.log("Indexed:", indexedPixels);
console.log("Palette:", colorPalette);
- NEUQUANT performs best on low-frequency, non-photo images like icons and illustrations.
- For photographic images, you may consider alternative quantization algorithms like K-means or Median Cut.
MIT
This module was generated via Rust + wasm-bindgen for modern web-based image processing pipelines.