Created
July 13, 2013 10:24
-
-
Save snorpey/5990265 to your computer and use it in GitHub Desktop.
convert an imagedata object (from an html5 canvas) to greyscale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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