# Changing the color of a sprite at runtime

Is there a form to change the color of a sprite in runtime?}

I have a black point loaded with an image, but i dont want to load various images with different colors, is there a mode to change the color in runtime and not loading lot of images?

And i want to tell it what part to color of the sprite, is it possible?

I had to do something like this in XNA with pixel-art sprites. The best way i've found was using pixel shader.

The sprites are done in gray-scale (8 bits colors without alpha), then you'll have to map each gray-color to the correspondent 32 bit color you want, using a 256 lenght array of Colors.

In the shader you can use the gray colors of the sprite as a look-up table to the array of 32 bit colors.

• Yes, With the gray color i can tint my images :) and with the set color of the sprite i change the color. Thanks for that – Rudy_TM Mar 8 '12 at 17:46

If you refer to libgdx then the class com.badlogic.gdx.graphics.g2d.Sprite provides two different methods called setColor: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setColor(com.badlogic.gdx.graphics.Color)

• Ok I missed the point. You just need to parse all the pixels of your sprite and simply change by another color when a pixel matches the color you want to replace. Be careful it can turns out to be very time-consuming in real-time that's why usually sprites can be pre-rendered in several variations of colors in the resources. – user14170 Mar 7 '12 at 13:37
Color c = new Color(batch.getColor());
batch.setColor(a,b,c,d);
batch.draw(yourSprite, x, y);
batch.setColor(c);

• setColor(a,b,c,d) ?? Are you sure? – RichieHH Aug 25 '15 at 23:37