[Solved] Colors and single RGB integers... getting some strange results.

I’m trying to replace certain colors in an image with different colors inside an int[] array, but something isn’t going right. I have a sprite sheet with letters, numbers, and symbols in shades of gray and I’m trying to change the colors to get different colored fonts. Everything is going well, just something isn’t working when the red, green, and blue values are compressed into one integer value. When I specify a certain pixel to be red for example, I get some strange color with an RGB value of (R: 181, G: 100, B: 58) instead of (R: 255, G: 0, B: 0). The same thing happens for every other color I specify. Here is the code I use to change three separate integers, red, green, and blue, into one integer:

public static int getColor(int r, int g, int b) {
		if(r > 255 || r < 0) r = 255;
		if(g > 255 || g < 0) g = 255;
		if(b > 255 || b < 0) b = 255;
		return ((255 & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
	}

Here is a picture showing the results I got when I tried to change colors into red, green, and blue:
The RGB values are shown next to the color, in the order Red, Green, Blue.

Everything else works fine, the correct shades of gray are being replaced, no errors, etc, I just can’t figure out where I’m going wrong with combining the red, green, and blue values into one int.

I even tried to use this line of code to get a red color, which uses the official Color class in the Java JDK, but I got the same results:

int red = new Color(255, 0, 0).getRGB();

Just in case anyone needs to see, here is a picture with what it looks like:

Does anyone know what the problem is? Any help would be gladly appreciated! :slight_smile:

looks like your alpha transparecy is like at 50%,

switch areound the RGB and Alpha values.

I tried changing

return ((255 & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);

To all of the following:

return ((255 & 0xff) << 24) | (b & 0xff) | ((g & 0xff) << 8) | ((r & 0xff) << 24); // ABGR instead of ARGB
return ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff) | ((255 & 0xff) << 24); // RGBA instead of ARGB
return (b & 0xff) | ((g & 0xff) << 8) | ((r & 0xff) << 16) | ((255 & 0xff) << 24); // BGRA instead of ARGB

but got the same results :-
Is this what you meant by switch around the RGB and Alpha values?

Your getColor method works correctly. Although I would shorten it to

public static int getColor(int r, int g, int b) {
    return (0xff << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
}

The problem must be somewhere else in your code.
Are you extracting the int array from a BufferedImage and write into it?
Is your BufferedImage of type INT_RGB?

[quote]Are you extracting the int array from a BufferedImage and write into it?
Is your BufferedImage of type INT_RGB?
[/quote]
I’m creating a blank array of size 64 (for an 8x8 tile image) and writing into that.

As for the type, that’s a good point. I just checked it, and turns out the sprite sheet is of type BYTE_INDEXED. I couldn’t find a setType() method (only found a getType() method) so I created a new BufferedImage the size of the sprite sheet and set the RGB of the new BufferedImage to that of the old one, and it works!

Is this the only way to convert the type of a BufferedImage or is there a much faster way? It seems like there would be a better method.

Anyways, thanks a lot for the help! :smiley:

The only other way I know of is to use the Graphics.drawImage method.

Graphics g = newImage.getGraphics();
g.drawImage(oldImage, 0, 0, null);
g.dispose();

Hm… doesn’t look like it’ll fit in with the way I have this thing coded. Thanks anyways!