将8位颜色转换为RGB值 [英] Converting 8 bit color into RGB value

查看:2820
本文介绍了将8位颜色转换为RGB值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用反射阴影贴图在我的游戏引擎中实现全局照明。 RSM有i.a.颜色纹理。保存内存。我把24位值打包成8位值。好。我知道如何包装。但是如何解压呢?我想创建一个1D纹理与8位调色板,有255种不同的颜色。我的8位颜色将是该纹理中像素的索引。
我不知道如何生成这种纹理。
是否有任何数学方法将8位值转换为rgb?



//对不起我的英语。提前感谢。



@edit
颜色为以下格式:

RRR GGG BB



@ edit2:
我正在打包我的颜色:

 code> int packed =(red / 32< 5)+(green / 32< 2)+ 
// int实际上是一个字节,如果是字节,c#编译器就是bitching。

@ edit3:

好​​吧,我找到了一种方法。



@ edit4错了...

  int r =(packed>> 5)* 32; 
int g =((packed> 2)<< 3)* 32;
int b =(packed<< 6)* 64;


解决方案

在javascript



编码

  encodedData =(Math.floor((red / 32))< +(Math.Floor((green / 32))<<2)+ Math.floor((blue / 64)); 

解码

  red =(encodedData>> 5)* 32; 
green =((encodedData& 28)>> 2)* 32;
blue =(encodedData& 3)* 64;

解码时,我们使用AND门/运算符提取所需的位并丢弃前导位。



当编码时Math.floor用于截断小数部分,如果四舍五入,它将创建总数

UPDATE 1

如果我们提供的值大于255,将颜色除以32或64.



RRRGGGBB



R / G = 3位,二进制的最大值为111这是十进制的7。
B = 2bit,最大值为二进制11,十进制为3.



我们应该将R / G除以等于或大于255/7和B的值等于或大于255/3。
我们还应该注意,代替Math.floor,我们应该使用Math.round,因为舍入会得到更准确的结果。


I'm implementing global illumination in my game engine with "reflective shadow maps". RSM has i.a. color texture. To save memory. I'm packing 24 bit value into 8 bit value. Ok. I know how to pack it. But how do I unpack it? I had idea to create a 1D texture with 8 bit palette, with 255 different colors. My 8 bit color would be index of pixel in that texture. I'm not sure how to generate this kind of texture. Are there any mathematical ways to convert 8 bit value into rgb?

//Sorry for my bad english. Thanks in advance.

@edit The color is in this format:
RRR GGG BB

@edit2: And I'm packing my colour like this:

int packed = (red / 32 << 5) + (green / 32 << 2) + (blue / 64);
//the int is actually a byte, c# compiler is bitching if it's byte.

@edit3:
Alright, I found a way to do this I think. Tell me if it's wrong.

@edit4 It's wrong...

int r = (packed >> 5) * 32;    
int g = ((packed >> 2) << 3) * 32;    
int b = (packed << 6) * 64;

解决方案

In javascript

Encode

encodedData = (Math.floor((red / 32)) << 5) + (Math.floor((green / 32)) << 2) + Math.floor((blue / 64));

Decode

red = (encodedData >> 5) * 32;
green = ((encodedData & 28) >> 2) * 32;
blue = (encodedData & 3) * 64;

While decoding we are using AND Gate/Operator to extract desired bits and discard leading bits. With green, we would then have to shift right to discard bits at right.

While encoding Math.floor is used to truncate decimal part, if rounded off it would create total value greater than 255 making it a 9 bit number.

UPDATE 1
It does not provide accurate results if we divide color by 32 or 64.

RRRGGGBB

R/G = 3bit, max value is 111 in binary which is 7 in decimal. B = 2bit, max value is 11 in binary which is 3 in decimal.

We should divide R/G by value equal or greater than 255/7 and B by value equal or greater than 255/3. We should also note that in place of Math.floor we should use Math.round because rounding off gives more accurate results.

这篇关于将8位颜色转换为RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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