Install quantize via npm
masterInstall the quantize module using npm to perform color quantization in your Node.js projects.
npm install quantizerepository·master·Indexed 41 days ago
https://github.com/olivierlesnicki/quantizeA Node.js module for color quantization based on Leptonica. It implements the Modified Median Cut Quantization (MMCQ) algorithm to reduce a large set of RGB pixels into a smaller, optimized color palette of 2 to 256 colors.
Install the quantize module using npm to perform color quantization in your Node.js projects.
npm install quantizeTo quantize a set of colors, require the module and call the quantize function with two arguments:
arrayOfPixels: An array of pixels, where each pixel is represented as an [R, G, B] array.maximumColorCount: The maximum number of colors allowed in the resulting reduced palette.The function returns a colorMap object used to access the palette and map individual pixels.
var quantize = require('quantize');
var arrayOfPixels = [[190,197,190], [202,204,200], [207,214,210], [211,214,211], [205,207,207]];
var maximumColorCount = 4;
var colorMap = quantize(arrayOfPixels, maximumColorCount);After creating a colorMap via quantize(), call the .palette() method to get the array of reduced colors.
// Returns the reduced palette as an array of [R, G, B] arrays
colorMap.palette();
// Example output: [[204, 204, 204], [208,212,212], [188,196,188], [212,204,196]]To find the closest color in the reduced palette for a specific pixel, use the .map(pixel) method on the colorMap object. Pass an [R, G, B] array as the argument.
// Returns the reduced pixel color
colorMap.map(arrayOfPixels[0]);
// Example output: [188,196,188]The quantize function implements the Modified Median Cut Quantization (MMCQ) algorithm. It takes an array of pixels and reduces them to a specified number of colors, returning a color map object used to retrieve the new palette and map original pixels to the reduced set.
Parameters:
pixels: An array of pixels, where each pixel is represented as an [R, G, B] array (e.g., [[190, 197, 190], [202, 204, 200]]).maxcolors: The target number of colors for the palette. Must be between 2 and 256 inclusive.Returns:
CMap object (color map) if successful.false if the input is invalid (e.g., pixels is empty, or maxcolors is out of range).Usage Example:
var myPixels = [[190,197,190], [202,204,200], [207,214,210], [211,214,211], [205,207,207]];
var maxColors = 4;
var cmap = MMCQ.quantize(myPixels, maxColors);
if (cmap) {
var newPalette = cmap.palette(); // Returns the reduced palette
var newPixels = myPixels.map(function(p) {
return cmap.map(p); // Maps original pixel to the nearest palette color
});
}var myPixels = [[190,197,190], [202,204,200], [207,214,210], [211,214,211], [205,207,207]];
var maxColors = 4;
var cmap = MMCQ.quantize(myPixels, maxColors);
var newPalette = cmap.palette();
var newPixels = myPixels.map(function(p) {
return cmap.map(p);
});The CMap object returned by quantize() provides methods to interact with the generated color palette and perform color mapping.
Methods:
palette(): Returns an array of the colors in the reduced palette. Each color is an [R, G, B] array.map(color): Takes an [R, G, B] color and returns the closest color from the generated palette. It first attempts to find a match within the color space boxes; if no exact box contains the color, it falls back to the nearest() color.nearest(color): Calculates the Euclidean distance between the provided [R, G, B] color and all colors in the palette, returning the one with the smallest distance.size(): Returns the number of colors in the palette.