I want to change one single color (yellow, to be somewhat specific) to another color in a BufferedImage which was loaded from a .GIF file. I should be able to do it easily enough with getRGB and setRGB, but it would much more efficient if I could just change the color that the IndexColorModel's 'yellow' index is referring to. Failing that, is it possible to create a new IndexColorModel which has the altered map?
Maybe something like this:
BufferedImage bi = javax.imageio.ImageIO.read(new File("pathToGif"));
if(bi.getColorModel() instanceof IndexColorModel) {
IndexColorModel colorModel = (IndexColorModel)bi.getColorModel();
int colorCount = colorModel.getMapSize();
byte[] reds = new byte[colorCount];
byte[] greens = new byte[colorCount];
byte[] blues = new byte[colorCount];
colorModel.getReds(reds);
colorModel.getGreens(greens);
colorModel.getBlues(blues);
Color yellow = Color.YELLOW;
Color blue = Color.BLUE;
for(int i = 0; i < reds.length; i++) {
Color newColor = new Color(reds[i]&0xff, greens[i]&0xff, blues[i]&0xff);
if(newColor.equals(yellow)) {
reds[i] = (byte)blue.getRed();
greens[i] = (byte)blue.getGreen();
blues[i] = (byte)blue.getBlue();
break;
}
}
}
This changed the yellow color to blue color, you can then use the changed color model to create a new BufferedImage and save it.