6
\$\begingroup\$

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?

\$\endgroup\$
8
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Rudy_TM Mar 8 '12 at 17:46
3
\$\begingroup\$

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)

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – user14170 Mar 7 '12 at 13:37
3
\$\begingroup\$
Color c = new Color(batch.getColor());
batch.setColor(a,b,c,d);
batch.draw(yourSprite, x, y);
batch.setColor(c);
\$\endgroup\$
  • 2
    \$\begingroup\$ setColor(a,b,c,d) ?? Are you sure? \$\endgroup\$ – HörmannHH Aug 25 '15 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.