tags:

views:

103

answers:

3

How can I convert an ARGB integer to the corresponding ARGB tuple (A,R,G,B)?

I receive some XML where a color tag is given with some integer value (e.g -16777216). I need to draw a rectangle filled with that color. But I am unable to retrieve values of the A,R,G,B components from the integer value.

+1  A: 

You can try use unions. Something like this

struct color
{
  unsigned char alpha:8;
  unsigned char r:8;
  unsigned char g:8;
  unsigned char b:8;
};

union
{
  struct color selector;
  unsigned int base:32;
};
+1 - This could be used to provide more information about questions on unions at SO.
Praveen S
-1, since this introduces endian-problems.
unwind
+2  A: 

Use bitwise AND and shift right to select individual bytes from the 32-bit integer.

uint32_t color = -16777216;

uint8_t b = (color & 0x000000ff);
uint8_t g = (color & 0x0000ff00) >> 8;
uint8_t r = (color & 0x00ff0000) >> 16;
uint8_t a = (color & 0xff000000) >> 24;
Todd Yandell
BTW, IMO it'd be slightly less error-prone if you shifted before masking, and then all the masks can be `0xFF`.
jamesdlin
You may be right — I’ve just always done it this way. Would you elaborate on why you would consider this more error-prone? I hope there’s not a subtle bug I’m missing.
Todd Yandell
@Todd: Probably because it's easier to mistype e.g. 0x00ff0000 than to type 0xff. Also, the larger masks are very likely to be more expensive code-wise.
unwind
it works , but color is different..e.g. -65536 is value for red that I receive from window system. when I used the above steps to convert ARGB into A,R,G,B. it gives some other color not the red one
Greshi Gupta
even in case of red it givesa=0r=0g=0b=0instead of r=255
Greshi Gupta
@unwind: That’s what I assumed. It might be easy to type one too many 0’s or f’s. Also, you were right: it does produce a few more instructions. Interesting.
Todd Yandell
no Todd,my data is in ARGB format. because I am sending in that format only.
Greshi Gupta
+1  A: 

If the integer is ARGB I think it should be:

unsigned char b = color & 0x000000FF;
unsigned char g = (color>> 8) & 0x000000FF;
unsigned char r = (color>>16) & 0x000000FF;
unsigned char a = (color>>24) & 0x000000FF;
DrDipshit
it works but in place of flora green it gives me yellow..is there is problem with some alpha value??
Greshi Gupta
Oops, you’re right. Mine was backwards. Fixed it.
Todd Yandell
@Greshi GuptaCan you provide the colour value you're trying to convert? This should work for 32 bit ARGB colour. Yellow is a mix of red and green, so perhaps your color values have a lower bit depth or something. AFAIK alpha values should not affect the colour hue.
DrDipshit
-8323328 is value of light green. but when i convert this i get light yellow.
Greshi Gupta
I'm guessing you're dealing with 16 or 24 bit colour. You should probably check the bitdepth.
DrDipshit
thanks Drdipshit..i got the answer..actually i needed to divide it by 255,because I am using core graphic classes where value of RGB lies between 0-1. thanks alot for your help.
Greshi Gupta