Created
April 27, 2014 12:57
-
-
Save z-------------/11344965 to your computer and use it in GitHub Desktop.
Calculate the average color of a canvas
This file contains hidden or 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
| function avgColor(canvas){ // where canvas is an HTMLCanvasElement | |
| var tr = 0, tg = 0, tb = 0; | |
| var idata = canvas.getContext("2d").getImageData(0,0,canvas.width,canvas.height).data; | |
| for (i=0;i<idata.length;i+=4) { // red | |
| tr += idata[i]; | |
| } | |
| for (i=1;i<idata.length;i+=4) { // green | |
| tg += idata[i]; | |
| } | |
| for (i=2;i<idata.length;i+=4) { // blue | |
| tb += idata[i]; | |
| } | |
| tr = tr/(idata.length/4); | |
| tg = tg/(idata.length/4); | |
| tb = tb/(idata.length/4); | |
| return [tr,tg,tb]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment