# How do I undo changing the color of an image in Slick2d?

This code colors the enemy red:

enemies.get(i).image.setColor(0,1,0,0,1);
enemies.get(i).image.setColor(1,1,0,0,1);
enemies.get(i).image.setColor(2,1,0,0,1);
enemies.get(i).image.setColor(3,1,0,0,1);


if I set it to:
enemies.get(i).image.setColor(0,0,0,0,1);
it turns it pure black, and if I set it to
enemies.get(i).image.setColor(0,0,0,0,0);
it turns the enemy invisiable

How do I un-color enemy red? as in return to state as it was before it was colored.

Don't fiddle with the alpha at all if you're not wanting to change it. Use the non-alpha version.

enemies.get(i).setCurrentColor(1,0,0);


Then create this method:

setCurrentColor(int r, int g, int b) {
image.setColor(0,r,g,b);
image.setColor(1,r,g,b);
image.setColor(2,r,g,b);
image.setColor(3,r,g,b);
}


Then when you want to return to no color, just texture, set the enemy to white:

enemies.get(i).setCurrentColor(1,1,1);


Turns out there's a setImageColor function that Eric mentioned in his answer. I checked the source:

public void setImageColor(float r, float g, float b) {
setColor(TOP_LEFT, r, g, b);
setColor(TOP_RIGHT, r, g, b);
setColor(BOTTOM_LEFT, r, g, b);
setColor(BOTTOM_RIGHT, r, g, b);
}


So, setImageColor is not any different than calling setColor on all four corners. Just in case you were wondering.

You need to track the last color set before changing it red, and use that when you want to restore the color.

• but there is no other colors, its just enemy.png image , when he gets hit, it changes red. – Matthew Apr 3 '13 at 16:27

An Image's default color is 1,1,1,1 (rgba) a.k.a. opaque (or non-transparent) white. Use Image.setImageColor to reset it:

enemies.get(i).image.setImageColor(1,1,1,1);


Be sure to use this method instead of applying Image.setColor to each of the image's four corners.

• it says setImageColor doesnt exist – Matthew Apr 5 '13 at 17:57
• @Matthew Right, I corrected the code for your copy-pasting convenience. – Eric Apr 5 '13 at 18:02