Skip to content

Instantly share code, notes, and snippets.

@snorpey
Created July 13, 2013 10:24
Show Gist options
  • Save snorpey/5990265 to your computer and use it in GitHub Desktop.
Save snorpey/5990265 to your computer and use it in GitHub Desktop.
convert an imagedata object (from an html5 canvas) to greyscale
/*global define*/
define(
function()
{
function greyscaleImageData( image_data )
{
var data = image_data.data;
var len = image_data.data.length;
var i = 0;
var brightness;
for ( i = 0; i < len; i += 4 )
{
brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
data[i] = brightness;
data[i + 1] = brightness;
data[i + 2] = brightness;
}
image_data.data = data;
return image_data;
}
return greyscaleImageData;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment