Often in games a nice feature to give to users it the possibility to customize their own character, for example changing colors of the different parts (hairs, eyes, shirt, and so on).
To support this features there are 2 possibilities:
The second options will save you the effort to create these multiple images, and will strip down your JAR size. Also, it will allow you to support a lot more colors.
You can see a simple midlet showing the following snipped of code in action on this page.
Here is the code that takes your input image, the color to be replaced and the new color, and returns a new image.
Some notes on this code:
import javax.microedition.lcdui.Image;
public class ColorChanger
{
public static Image changeColor(Image source, int fromColor, int toColor)
{
int imageWidth = source.getWidth();
int imageHeight = source.getHeight();
int[] rgb = new int[imageWidth * imageHeight];
int deviceColor = getDeviceColor(fromColor);
source.getRGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);
for(int i = 0; i < rgb.length; i++)
{
if(rgb[i] == deviceColor)
{
rgb[i] = toColor;
}
}
return Image.createRGBImage(rgb, imageWidth, imageHeight, true);
}
static int getDeviceColor(int color)
{
Image fake = Image.createRGBImage(new int[]{color}, 1, 1, false);
int[] rgb = new int[1];
fake.getRGB(rgb, 0, 1, 0, 0, 1, 1);
return rgb[0];
}
}
No related wiki articles found