Changing certain colors of an image into other colors

Hello, quite new here, just made an account yesterday. I’ve been learning game development for a while and made a few games, but I’ve come to a problem. For this next game I want to make, it’s going to have a character creation menu in which you can adjust the colors of the character sprite, sort of like how Terraria allows you to change colors of the character. I’m puzzled by the best way to go about doing this. Each sprite is going to have six different shades of gray, and for each shade I want it to be replaced by a certain color chosen by the user in the character selection screen. How would I go about doing this?

I thought of getting the RGB array of the sprite and reading it in a for loop then depending on the shade of gray the color would be changed to what the user picked, but it seems there would be a faster way of doing that, especially since this would need to be done for each iteration of the game loop.

Any help would be great, thanks in advance!

  • Screem

You could do precisely what you suggest as a pre-processing step. Or you could use palette-based images and just change the palette. That involves getting quite deeply into the java.awt.image API, and in particular understanding IndexColorModel.

Hm, alright. Thanks for the advice!

Indexed color images are going to drive you bonkers, and they’re slower on modern hardware. I would just process the template image and create a new one each time.

So once they create the character, store the player sprite with the colors they chose in some player data folder or something and use the custom colored image from then on for that character? Seems like a great idea, thanks! :slight_smile: