简体   繁体   English

从 16 位获取颜色

[英]Get a color from 16-bits

I am playing around with colors in C# and I wanted to know how to get a color value from a 16-bit byte array.我正在玩 C# 中的 colors,我想知道如何从 16 位字节数组中获取颜色值。 Here is a code bellow where I use a 32-bit byte array.下面是我使用 32 位字节数组的代码。

                var colorArray = new Color[b.Length/4];                
                for (var i = 0; i < b.Length; i += 4)
                {
                    var color = Color.FromArgb(b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
                    colorArray[i / 4] = color;
                }

You basically shift the most significant bits of each field to the right place in its new format and mask off any missing bits to zero.您基本上将每个字段的最高有效位以其新格式移动到正确的位置,并将任何丢失的位屏蔽为零。 Green is split across two bytes.绿色被分成两个字节。 This would be easier if the array were an array of 16 bit ints, but if it's in bytes, then the bit manipulation for a pair of bytes is roughly like this.如果数组是一个 16 位整数的数组,这会更容易,但如果它是字节,那么一对字节的位操作大致是这样的。

+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
|               byte1                 |             byte0             |
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
| -  |          Red           |       Green       |       Blue        |
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+

B8 = (byte0 << 3) & 0xF8;
G8 = ((byte1 << 6) & 0xC0) || ((byte0 >> 2) & 0x38);
R8 = (byte1 << 1) & 0xF8;

B8G8R8 = B8 | (G8 << 8) || (R8 << 16);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM