从整数值中提取 rgb 颜色分量 [英] Extracting rgb color components from integer value

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

问题描述

如果有一个整数值,例如.86 那么我如何从这个整数值中提取 r、g、b 分量......?我正在开发 Visual C++ 2008 速成版.谢谢..

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

推荐答案

通常(我经常重复,因为您没有在问题中指定太多内容)颜色被打包在一个 4 字节的整数中,并带有 RGBA 组件.

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.

在您的示例中,您谈到了 0-255 值.不清楚组件是否为 2 位大小(每个组件 4 个强度值).

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;

也许你的颜色是用调色板索引的,在这种情况下你应该有一个存储调色板本身的数组,你从文件中读取的字节应该是存储在某处的颜色的索引否则.

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天全站免登陆