48 lines
1.4 KiB
Groff
48 lines
1.4 KiB
Groff
|
/**
|
||
|
* Part of Picozu Image Editing application.
|
||
|
* Visit https://www.picozu.com for more info.
|
||
|
* The code is copyrighted by Picozu <www.picozu.com>.
|
||
|
*
|
||
|
* @version 1.1.121413
|
||
|
* @package Workers
|
||
|
* @application Picozu <www.picozu.com>
|
||
|
* @license www.picozu.com/license
|
||
|
*/
|
||
|
|
||
|
importScripts('quantize.js');
|
||
|
|
||
|
self.addEventListener('message', function(e) {
|
||
|
function process(data) {
|
||
|
data.params.numcolors = (typeof data.params.numcolors != 'undefined') ? data.params.numcolors : 29;
|
||
|
return {
|
||
|
colors: loadSwatches(data.imageData, data.params.numcolors)
|
||
|
};
|
||
|
}
|
||
|
self.postMessage(process(e.data));
|
||
|
}, false);
|
||
|
|
||
|
function loadSwatches(image, colorCount) {
|
||
|
var pixels = image.data;
|
||
|
var pixelCount = image.width * image.height;
|
||
|
var pixelArray = [];
|
||
|
for (var i = 0, offset, r, g, b, a; i < pixelCount; i++) {
|
||
|
offset = i * 4;
|
||
|
r = pixels[offset + 0];
|
||
|
g = pixels[offset + 1];
|
||
|
b = pixels[offset + 2];
|
||
|
a = pixels[offset + 3];
|
||
|
if (a >= 125) {
|
||
|
if (!(r > 250 && g > 250 && b > 250)) {
|
||
|
pixelArray.push([r, g, b]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
var cmap = MMCQ.quantize(pixelArray, colorCount);
|
||
|
if (cmap) {
|
||
|
var palette = cmap.palette();
|
||
|
return palette;
|
||
|
}
|
||
|
else {
|
||
|
return null;
|
||
|
}
|
||
|
}
|