Skip to content

Instantly share code, notes, and snippets.

@abods
Created May 20, 2014 05:32
Show Gist options
  • Save abods/eff35a20d85815434ad7 to your computer and use it in GitHub Desktop.
Save abods/eff35a20d85815434ad7 to your computer and use it in GitHub Desktop.
get canvas pixels rgb
ctx.drawImage(image, 0, 0);
var imageData = ctx.getImageData(0, 0, 1199, 677);
// moust used colors
var r=0,g=0,b=0,a=0;
for (var i = 0; i < imageData.data.length; i+=4) {
r+=imageData.data[i];
g+=imageData.data[i+1];
b+=imageData.data[i+2];
a+=imageData.data[i+3];
// R red
imageData.data[i] = Math.floor((Math.random() * 200) + 1)-imageData.data[i];
// G Green
imageData.data[i+1] = Math.floor((Math.random() * 100) + 1)-imageData.data[i];
// B Blue
imageData.data[i+2] = Math.floor((Math.random() * 0) + 1)-imageData.data[i];
// Alfa
imageData.data[i+3] = Math.floor((Math.random() * 255) + 1)-imageData.data[i];
};
ctx.putImageData(imageData,0,0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment