Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vipertechofficial/933dbecd24ebfdf3971a2ccbfd98a3ca to your computer and use it in GitHub Desktop.
Save vipertechofficial/933dbecd24ebfdf3971a2ccbfd98a3ca to your computer and use it in GitHub Desktop.
Get a map of pixel and their corresponding colors in Javascript so in other words a palette and a list of the color index (to be used with my other gist and something like RGBQuant.js)
_get_pixels_palette_and_list_from_image_data = (image_data) => {
const image_data_data = image_data.data;
let new_pxl_colors = [];
let new_pxl_colors_set = new Set();
let new_pxls = new Array(image_data.width * image_data.height);
for (let i = 0; i < image_data_data.length; i += 4) {
const color_hex = this._get_hex_color_from_rgba_values(image_data_data[index], image_data_data[index+1], image_data_data[index+2], image_data_data[index+3]);
const deja_vu_color_hex = new_pxl_colors_set.has(color_hex);
let color_hex_index = deja_vu_color_hex ? new_pxl_colors.indexOf(color_hex): -1;
if (color_hex_index === -1) {
color_hex_index = new_pxl_colors.push(color_hex)-1;
new_pxl_colors_set.add(color_hex);
}
new_pxls[i / 4] = color_hex_index;
}
return [
new_pxl_colors,
new_pxls,
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment