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!