1 decade ago by Cavalier
I'd like to know if it's possible to change an animationSheet's color ingame, through a method call similar to the one below.
Basically, I'm running through all pixels, looking for a specific color (240 white) and changing it according to the entity's 'owner' variable. However, context.getImageData() requires 4 parameters, which if I'm not mistaken, are the coordinates of the image rectangle.
Ideas?
//Adapted from http://www.html5canvastutorials.com/advanced/html5-canvas-invert-image-colors-tutorial/ colorChange: function(){ var imageData = ig.system.context.getImageData(this.animSheet); var data = imageData.data; var red = 240; var green = 240; var blue = 240; var baseColor = 240; for(var i = 0; i < data.length; i += 4) { // i = red; i+1 = green; i+2 = blue if (data[i] == baseColor && data[i+1] == baseColor && data[i+2] == baseColor) { switch (this.owner) { case 1: //blue red = 0; green = 0; blue = 240; break; case 2: //red red = 240; green = 0; blue = 0; break; case 3: //green red = 0; green = 240; blue = 0; break; case 4: //yellow red = 250; green = 240; blue = 40; break; } data[i] = red; data[i + 1] = green; data[i + 2] = blue; } } // alpha channel //data[i + 3] = 255 - data[i + 2]; // overwrite original image this.animSheet = imageData; },
Basically, I'm running through all pixels, looking for a specific color (240 white) and changing it according to the entity's 'owner' variable. However, context.getImageData() requires 4 parameters, which if I'm not mistaken, are the coordinates of the image rectangle.
Ideas?