2012-05-18, 07:35 PM
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:
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:
gives the following output:
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!
Wow, just managed to get it myself, which I wasn't expecting (not so soon, at least). Thanks anyway
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!
Wow, just managed to get it myself, which I wasn't expecting (not so soon, at least). Thanks anyway