从整数值提取rgb颜色组件 [英] Extracting rgb color components from integer value

查看:202
本文介绍了从整数值提取rgb颜色组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有一个整数值,例如。 86那么我如何可以从这个整数值外加r,g,b分量....?
我正在使用Visual C ++ 2008 express版本。
感谢..

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

推荐答案

通常(我经常重复,因为你没有在你的问题中指定太多)

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 it so 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 me modified according to yours.

EDIT:
在您提到的0-255值的示例中。不清楚是否有2位大小(每个组件4个强度值)。

in your example you spoke about a 0-255 value. It is not clear if compnents are 2bit sized (4 intensity values per component).

在这种情况下,方法仍然保持不变,但您只有几种颜色: / p>

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.

这篇关于从整数值提取rgb颜色组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆