Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

visual c++ - Extracting rgb color components from integer value

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...