C# 如何将 colorDialog 值发送到 5bitRGB 中的文本框?

C# How to send colorDialog value to a textBox in 5bitRGB?

所以我正在创建 PS2 游戏的编辑器。 而且这个游戏有两种"Systems"的颜色。

"normal" RGB R: 0 到 255 G: 0 到 255 B: 0 到 255.

而or我认为是5bitRGB R: 0 to 31 G: 0 to 31 B: 0 to 31.

为了让游戏中的颜色发生变化, 我必须转换中选择的值 十六进制的 colorDialog 例如:R:255 G:176 B:15 十六进制表示FFB00F。

然后通过十六进制在 3 个字节的 "slots" 中更改这些值。

美到现在这么好,但是5bitRGB只有"slots" 2个字节。

示例:5bitRGB R:31 G:0 B:0 十六进制 1F80。

这就是我不知道该怎么做的地方,因为普通 RGB 的颜色 我可以将十六进制值发送到文本框。

然后我通过十六进制将这些值保存在 "slots" 的 3 个字节中。

同时 5bitRGB 颜色的插槽通过 Hex 改变 它们只有 "slots" 个 2 个字节。

所以我必须将转换后的 colorDialog 值发送到 5bitRGB 对于 2 个字节的 textBox,这真的可能吗?

So I would have to send the converted colorDialog value to 5bitRGB for textBox in 2 bytes, is this really possible?

好的! 5 位 x 3 个字段 = 15 位,2 个字节 = 16 位。你甚至还有剩余空间,但不多。

听起来这只是处理每个字段分辨率降低的问题。这可以通过位移运算将每个 8 位字段减少为 5 位字段来完成。

您需要将存储的值右移为 5 位字段。您还可以将值左移以用作 8 位(字节)字段来设置颜色 属性 - 但请注意,这只是 5 位值的 8 位表示和分辨率损失从转移到 5 位将持续存在。您将不得不决定处理分辨率丢失的约定 - 什么都不做是一种选择。

例如:

// 8-bit color values
byte rValue = 255;
byte gValue = 127;
byte bValue = 63;

// set color
pictureBox1.BackColor = Color.FromArgb(rValue, gValue, bValue);

// 5-bit color values
var rBits = new BitArray(5, false);
var gBits = new BitArray(5, false);
var bBits = new BitArray(5, false);

// bit position comparison operator
byte op = 0x80;

// convert to 5-bit arrays
for (int i = 0; i < 5; i++)
{
    if (rValue > op) { rBits[i] = true; rValue -= op; }
    if (gValue > op) { gBits[i] = true; gValue -= op; }
    if (bValue > op) { bBits[i] = true; bValue -= op; }
    op >>= 1;
}

byte rRetrieved = 0;
byte gRetrieved = 0;
byte bRetrieved = 0;

// bit position comparison operator
op = 0x80;

// convert back to 8-bit bytes
for (int i = 0; i < 5; i++)
{
    if (rBits[i]) { rRetrieved += op; }
    if (gBits[i]) { gRetrieved += op; }
    if (bBits[i]) { bRetrieved += op; }
    op >>= 1;
}

// set color - note loss of resolution due to shifting
pictureBox1.BackColor = Color.FromArgb(rRetrieved, gRetrieved, bRetrieved);