Jump to content


Changing The Color Of An Image


  • Please log in to reply
1 reply to this topic

#1 Lednakashim

Lednakashim

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 24 June 2009 - 08:36 PM

Hello,
I have an Image that I wish to dye into different colors, preferably via changing its RGB values. In this way I hope to have one generic .gif file and use it in several Image classes with distinct colors. I am hoping for something that will work like this : theImageIWant.changeRGB(125,0,0);

#2 Captain Pierce

Captain Pierce

    Advanced Member

  • Moderator
  • PipPipPip
  • 877 posts
  • Gender:Male
  • Location:Georgia

Posted 29 June 2009 - 11:37 PM

Start here: RGBImageFilter

The only code I have that might be a useful reference isn't immediately accessible, I may post it later on when I have time to search for it.

Edit:

Okay got the code here, original comments and everything. This is a very basic class that looks for a specific color value and replaces it with another. It is used in a game I wrote for my intro to Java class when I was just starting out ~5 years or so ago.

CODE

import java.awt.image.*;
import java.awt.*;
import javax.swing.JPanel;
/**
*Class file to change the tank image to the custom color.
*
*/

public class RGBImageChanger {

private Color oldColor;

/**
*Default old color is (8, 8, 8), which is almost black.
*
*/

public RGBImageChanger ()
{this(new Color(8, 8, 8));}

public RGBImageChanger (Color color)
{oldColor = color;}

/**
*Changes the pixels that are the old color of the image passed in
*and changes them to the new color.
*/

public Image processImage (Image image, Color newColor) {

//Get image dimensions.
int iw = image.getWidth(null);
int ih = image.getHeight(null);

//Pixel array required for PixelGrabber.
int[] pixels = new int[iw * ih];

//Get those pixels!!!
try {

PixelGrabber pg = new PixelGrabber (image, 0, 0, iw, ih, pixels, 0, iw);
pg.grabPixels();

}catch (Exception e) {}

//Change those pixels!!!
for (int i = 0; i < iw * ih; i++) {

int p = pixels[i];

if (oldColor.getRGB() != p)
continue;

//Split that single int into three.
int r = 0xff & (p >> 16);
int g = 0xff & (p >> 8);
int b = 0xff & (p);

//Try getting the getRGB() int from color and comparing.

r = newColor.getRed();
g = newColor.getGreen();
b = newColor.getBlue();

pixels[i] = (255 << 24) | (r << 16) | (g << 8) | b;
}

//Need something to give a createImage method. So, I chose JPanel.
JPanel createImagePanel = new JPanel();
//Create and return the new image.
return createImagePanel.createImage(new MemoryImageSource(iw, ih, pixels, 0, iw));
}

public Color getOldColor ()
{return oldColor;}

public void setOldColor (Color color)
{oldColor = color;}
}

There are alternatives to using the JPanel at the end, I did that at the time because I had no clue what the alternatives were and didn't want to spend the time to go find them (had other things to do!)

Also that little piece of bit twiddling is dead code (where the ints r, g, band bare declared). I was following an example in The Complete Java Reference when I made that class, and I guess I never got around to commenting that out.

PixelGrabber

Aside from that, Google should be able to provide better, more complete examples that fit your needs. It wouldn't be too much work to modify it to handle a list of colors, which it sounds like from your simple post you might need. Just replace that simple Color reference with a Map and associate color values with the replacements.