thank you for your time :). I have already taken a look at Format of TYPE_INT_RGB and TYPE_INT_ARGB and now know how to convert this TYPE_INT_RGB into 4 separate values, but if I were to do a modification to each (say add 20 to each, so Alpha+=20, red+=20, and so on) how would I recombine these values into this TYPE_INT_RGB formatting? Thank you!
Asked
Active
Viewed 6,514 times
3 Answers
5
// to extract the components into individual ints.
int argb = something();
int red = 0xFF & ( argb >> 16);
int alpha = 0xFF & (argb >> 24);
int blue = 0xFF & (argb >> 0 );
int green = 0xFF & (argb >> 8 );
// to recreate the argb
int argb = (alpha << 24) | (red << 16 ) | (green<<8) | blue;

mP.
- 18,002
- 10
- 71
- 105
3
I believe this should work
int RGB = alpha;
RGB = (RGB << 8) + red;
RGB = (RGB << 8) + green;
Rgb = (RGB << 8) + blue;
There's another way without bit shifting, but I'm sure you'll figure it out.
This one is okay, too:
int rgba = new java.awt.Color(r,g,b,a).getRGB();

nullpotent
- 9,162
- 1
- 31
- 42
-
You are absolutely amazing! Thanks! Oh, found one small issue with this, the order was a bit off, here is the syntax that worked for me: int newRGB = alpha; newRGB = (newRGB << 8) + red; newRGB = (newRGB << 8) + green; newRGB = (newRGB << 8) + blue; – Maxwell Sanchez May 12 '12 at 22:30
-
Your bit shifting is wrong and `Color` does not have `toRGB`. I strongly believe that bitwise manipulation should be avoided wherever possible and `Color` allows one to do so. – David Heffernan May 12 '12 at 23:01
-
`Color` has `getRGB()` as of JDK1.0? It returns `int`. See http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#getRGB%28%29. – Chris Dennett May 13 '12 at 05:49
3
I would use java.awt.Color
for this.
int ARGB = new Color(red, green, blue, alpha).getRGB();
I guess you are using bitwise operations to pull out the individual color channels, but again the Color
class can hide those gory details.
Color color = new Color(ARGB);
int red = color.getRed();
int green = color.getGreen();
// etc.

David Heffernan
- 601,492
- 42
- 1,072
- 1,490