I’m writing a game that would (preferably) make use of the fact that you can use an IndexColorModel to color code parts of a sprite.
e.g. If you have a bunch of FFT style sprites they will probably all have things like skin and armor. If you always use indicies 1-3 for skin tones, and indicies 4-7 for armor color, then by switching those colors to something else in the color model you can re-color your sprite without having to do a pixel by pixel color swap.
If you happen to start with all those colors in the right places this works pretty great. Problems occur when you go to use ImageIO to save your image. Let’s say you’ve got indicies 0 (transparent), 1-3 (skin), 4-7 (armor), and 15-17 (pants), but nothing between 7 and 15 in the color model. When you call write from ImageIO:
ImageIO.write(mySparseIndexBufferedImage, "png", myFileOut);
…Then read it back in:
BufferedImage myNewBufferedImage = ImageIO.read(myFileOut);
You’ll get something quite different for the color model. Specifically, the first twenty entries will be blank, then you’ll get however many colors were unique in your color model compressed into the subsequent slots, and the rest (out to 256) as something else (which I really don’t care about). The colors in the image read in are technically correct (i.e. it’s translated to the new color model correctly), it’s just that the indicies in the color model no longer match their original locations.
Has anyone else encountered this? Is there a different way to save these images that will preserve the color model color locations (even when there are gaps in the original model)?