Quanti the [color quantization](https://en.wikipedia.org/wiki/Color_quantization) library
npm install quantits
const Jimp = require("jimp");
const quanti = require("quanti");
(async () => {
const image = await Jimp.read("./image/sample.png");
const data = image.bitmap.data;
const palette = quanti(data, 8, 4);
palette.ditherProcess(data, image.getWidth());
await image.writeAsync("./image/sample_quantized.png");
})();
`
$3
`ts
// ArrayLike = Array, Uint8Array, any other number indexed object with length
function quanti(
pixels: ArrayLike, // total pixels that are considered
colorCount: number, // output palette size
channelCount: number // 3 if RGB, 4 if RGBA, >=5 unsupported
): quanti.Palette;
namespace quanti {
class Palette {
palette: ArrayLike[];
constructor(palette: ArrayLike[]);
mapIndex(color: ArrayLike, offset: number = 0): number; // mapping one color to the palette index
map(color: ArrayLike, offset: number = 0): ArrayLike; // mapping one color
process(data: WritableArrayLike): void; // mapping pixels data
ditherProcess(data: WritableArrayLike, width: number): void; // mapping pixels data with dithering
}
function srgb_to_linear(n: number): number;
function linear_to_srgb(n: number): number;
}
``