我在 Google 上搜索了一下,以澄清 10 位 RGB 的含义。
在维基百科Color Depth – Deep color (30/36/48-bit)我发现:
一些早期的系统将三个 10 位通道放在一个 32 位字中,其中 2 位未使用(或用作 4 级 alpha 通道)。
这似乎是我最合理的。
随之而来的是,红色有 10 位,绿色有 10 位,蓝色有 10 位,+ 2 位未使用(或为 Alpha 保留)。
这留下了两个问题:
它存储的是 RGBa 还是 BGRa 还是 aRGB? (我相信我在过去见过所有这些变化。)
组合值是存储小端还是大端?
当我在实际工作中遇到这种情况时,我基于一个假设进行了实现,渲染了一些测试模式,检查它是否看起来像预期的那样,如果没有交换相应的。实施中的部分。没什么,我很自豪,但是恕我直言,我以最少的努力得到了预期的结果。
因此,假设我将颜色存储为 RGB 三元组,其分量值在 [0, 1] 范围内,以下函数将其转换为 aRGB:
uint32_t makeRGB30(float r, float g, float b)
{
const uint32_t mask = (1u << 10u) - 1u;
/* convert float -> uint */
uint32_t rU = r * mask, gU = g * mask, bU = b * mask;
/* combine and return color components */
return ((rU & mask) << 20) | ((gU & mask) << 10) | (bU & mask);
}
这会产生具有以下位布局的值:
aaRRRRRR.RRRRGGGG.GGGGGGBB.BBBBBBBB
演示的小样本:
#include <stdint.h>
#include <stdio.h>
uint32_t makeRGB30(float r, float g, float b)
{
const uint32_t mask = (1u << 10u) - 1u;
/* convert float -> uint */
uint32_t rU = r * mask, gU = g * mask, bU = b * mask;
/* combine and return color components */
return ((rU & mask) << 20) | ((gU & mask) << 10) | (bU & mask);
}
int main(void)
{
/* samples */
const float colors[][3] = {
{ 0.0f, 0.0f, 0.0f }, /* black */
{ 1.0f, 0.0f, 0.0f }, /* red */
{ 0.0f, 1.0f, 0.0f }, /* green */
{ 0.0f, 0.0f, 1.0f }, /* blue */
{ 1.0f, 1.0f, 0.0f }, /* yellow */
{ 1.0f, 0.0f, 1.0f }, /* magenta */
{ 0.0f, 1.0f, 1.0f }, /* cyan */
{ 1.0f, 1.0f, 1.0f } /* white */
};
const size_t n = sizeof colors / sizeof *colors;
for (size_t i = 0; i < n; ++i) {
float *color = colors[i];
uint32_t rgb = makeRGB30(color[0], color[1], color[2]);
printf("(%f, %f, %f): %08x\n", color[0], color[1], color[2], rgb);
}
/* done */
return 0;
}
输出:
(0.000000, 0.000000, 0.000000): 00000000
(1.000000, 0.000000, 0.000000): 3ff00000
(0.000000, 1.000000, 0.000000): 000ffc00
(0.000000, 0.000000, 1.000000): 000003ff
(1.000000, 1.000000, 0.000000): 3ffffc00
(1.000000, 0.000000, 1.000000): 3ff003ff
(0.000000, 1.000000, 1.000000): 000fffff
(1.000000, 1.000000, 1.000000): 3fffffff
Live Demo on ideone