if there is an integer value, eg. 86 then how can i extraact the r,g,b components from this integer value....? I am working on Visual C++ 2008 express edition. Thanks..
-
How are the RGB components encoded in this integer in the first place? – Timo Geusch Feb 26 '10 at 14:11
-
let me explain what i have done uptil now... i have read the file content into a byte array, so now the byte array has values from 0 - 255, now i have to display these array values in a picturebox control, so i used, Bitmap^ bmp; bmp->SetPixel(i,j,Color::FromArgb( buf[count] ); //buf is the byte array but this code does not display anything, so i found out that maybe if i extract the rgb components and give them as argument in Color::FromArbg(int red,int green,int blue), then it might work.. eg. if i have value 86 then how can i get rgb components from it..? – JAYMIN Feb 26 '10 at 14:28
2 Answers
Usually (and I repeat usually, since you don't specify much in your question) a color is packed in a 4-bytes integer with RGBA components.
What you need to do is mask and shift, for example:
int color = 0xRRGGBBAA;
u8 red = (color & 0xFF000000) >> 24;
u8 green = (color & 0x00FF0000) >> 16;
u8 blue = (color & 0x0000FF00) >> 8;
This assumes the kind of encoding I specified, but can be modified according to yours.
EDIT: In your example you spoke about a 0-255 value. It is not clear if components are 2bit sized (4 intensity values per component).
In that case the approach still remains the same but you will have just few colors:
u8 color = 86;
// so you take 2 bits and multiply by 64 to possibly have intensities: 0, 64, 128, 192
u8 red = ((color & 0xC0) >> 6) * 64;
u8 green = ((color & 0x30) >> 4) * 64;
u8 blue = ((color & 0x0C) >> 2) * 64;
EDIT2: Maybe your colors are indexed with palette, in that case you should have an array that stores the palette itself and the byte you read from the file should be the index of a color stored somewhere else.

- 5
- 1
- 4

- 131,802
- 30
- 241
- 343
-
Thanks for the great answer. And opacity would be: u8 opacity = color & 0x000000FF – wcandillon Jan 01 '20 at 15:51
Normally you want to create a color from three components using the RGB
macro. Assuming the value you have is in the same format, you can pull it back apart into the individual pieces with GetRValue
, GetGValue
and GetBValue
.

- 476,176
- 80
- 629
- 1,111