Reputation: 992
I am working with 24x24 pixel icons. And I would like to be able to change a specific color within this icon to a different color. For example turn the white areas to red.
Upvotes: 2
Views: 3378
Reputation: 192055
I don't know of an API method that does that. And by default, Images
are not writable. However, if you have a BufferedImage
, you could do it like this:
public void changeColor(BufferedImage img, Color old, Color new) {
final int oldRGB = old.getRGB();
final int newRGB = new.getRGB();
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
if (img.getRGB(x, y) == oldRGB)
img.setRGB(x, y, newRGB);
}
}
}
This is not the most efficient way to do it (it's possible to fetch RGB data into an array instead of one pixel at a time), but for 24x24 images it shouldn't be a problem.
Upvotes: 3
Reputation: 108979
You can do this with a BufferedImage. Take a look at the Java Image I/O documentation.
Upvotes: 1