MyBB Community Forums

Full Version: Java Converting values into ARGB integer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, I'm writing an application in Java, and to save myself time when working out the values for colours, I thought I'd write myself a class with a few static methods so I can run the operations easily.

Now, I'm using a buffered image and the colour type is TYPE_INT_ARGB. I'm fairly new to Java though (only been using it for a month or so), and this is the first time I'm having to use bit shift operators, so I'm a little confused. What I have so far is the following class:

public class Color {

	public static int getA(int argb) {
		return (argb >> 24) & 0xFF;
	}

	public static int getR(int argb) {
		return (argb >> 16) & 0xFF;
	}

	public static int getG(int argb) {
		return (argb >> 8) & 0xFF;
	}

	public static int getB(int argb) {
		return argb & 0xFF;
	}

	public static int getARBGInt(int a, int r, int g, int b) {
		return ((a << 24) | 0xFF) + ((r << 16) | 0xFF) + ((g << 8) | 0xFF) + (b | 0xFF);
	}

}

From what I've been reading, and from various questions other people on the internet, I'm fairly sure that the code for getting the single values is correct. However, I was unable to find anyone asking about putting the single values back into the one, so I tried to take a guess at what the code should be.

To anybody who knows what should be done, my guess may look stupid, or it may be not far off. Either way, I don't really know what I'm meant to be doing, and I don't want to read up about bit shifts properly just yet as I haven't needed to use them before, and there's other things I want to learn first. So yeah, if anyone could give me the correct code, I would greatly appreciate it.

My solution (if I dare call it that) doesn't seem too far off to me, since my test:

System.out.println("Using ARGB int: " + 2147124220);

int a = Color.getA(2147124220);
int r = Color.getR(2147124220);
int g = Color.getG(2147124220);
int b = Color.getB(2147124220);

int product = Color.getARBGInt(a, r, g, b);

System.out.println("Compiles to: " + product);

gives the following output:

Using ARGB int: 2147124220
Compiles to: 2147124988

I'm going to carry on playing around with it to try and get it working, but if anybody knows the answer and wouldn't mind sharing, I'd be very thankful. I don't need any explanations of why, I just need the code for now, since I will get about to learning it properly over the next few weeks.

Thanks! Smile

Wow, just managed to get it myself, which I wasn't expecting (not so soon, at least). Thanks anyway Toungue
For posterity reasons, I'll go ahead and post this.

The way to put a single value into a ARGB color is to shift it up and OR it in:
// Some examples:
color |= a << 24;

color |= r << 16;

color |= g << 8;

color |= b;

Of course, this assumes that the colors are in the 0-255 range, and that the place you're putting them in is empty. If the first is not the case, you'll need to convert it into that range. If the second is not the case, you'll need to mask out the old color before ORing in the new one.

// Examples:
//       Mask out old color      OR in new one
color = (color & ~(0xFF << 24)) | (a << 24);

color = (color & ~(0xFF << 16)) | (r << 16);

color = (color & ~(0xFF << 8)) | (g << 8);

color = (color & ~0xFF) | b;

The mask works like this:
The color you have: 01010101
The new channel you want: 00100000
The bits you want to clear: 00000011
Shift up the bits you want to clear: 00110000
Invert it to the bits you want to keep: 11001111
AND the color with the bits you want to keep: 01000101
OR in the new channel: 01100101

Note that you invert after shifting, not before, because the shift operator puts in 0s at the bottom of the number.

The best part: This works in a great deal of languages, i.e. all of them that use << for bit shifting, ~ for inversion, and & and | for bitwise 'and' and 'or', respectively. This includes C/C++ and family.