[英]Color conversion from 16 to 32 and vice-versa
I'm developing a game client in which texture colors are stored as int values. 我正在开发一个游戏客户端,其中纹理颜色存储为int值。 Sometimes, however, I have to use 16bit colors to draw some texture... so I would like to know how to convert 32bit color to 16bit color and vice versa... preferably avoiding managed functions like Color.FromArgb() and similar. 但是,有时我必须使用16位颜色来绘制一些纹理...所以我想知道如何将32位颜色转换为16位颜色,反之亦然...最好避免使用诸如Color.FromArgb()之类的托管函数。 I prefer to achieve those operations as fast as possible with byte shifting. 我更喜欢通过字节移位来尽快实现这些操作。 Do you also know how to get the grayscale value of a 32 bit color? 您还知道如何获取32位颜色的灰度值吗?
public static Int32 16To32(UInt16 color)
{
Int32 red = (Int32)(((color >> 0xA) & 0x1F) * 8.225806f);
Int32 green = (Int32)(((color >> 0x5) & 0x1F) * 8.225806f);
Int32 blue = (Int32)((color & 0x1F) * 8.225806f);
if (red < 0)
red = 0;
else if (red > 0xFF)
red = 0xFF;
if (green < 0)
green = 0;
else if (green > 0xFF)
green = 255;
if (blue < 0)
blue = 0;
else if (blue > 0xFF)
blue = 0xFF;
return ((red << 0x10) | (green << 0x8) | blue);
}
public static UInt16 32To16(Int32 color)
{
Int32 red = ((((color >> 0x10) & 0xFF) * 0x1F) + 0x7F) / 0xFF;
Int32 green = ((((color >> 0x8) & 0xFF) * 0x1F) + 0x7F) / 0xFF;
Int32 blue = (((color & 0xFF) * 0x1F) + 0x7F) / 0xFF;
return (UInt16)(0x8000 | (red << 0xA) | (green << 0x5) | blue);
}
For what concerns grayscaling... you could try the following function but I just wrote it without testing so I cannot guarantee it will work perfectly: 对于灰度问题,您可以尝试以下功能,但是我只是在未经测试的情况下编写了它,所以我不能保证它会完美运行:
public static Int32 Grayscale(Int32 color)
{
Single red = ((color >> 0xA) & 0x1F) * 8.225806f;
Single green = ((color >> 0x5) & 0x1F) * 8.225806f;
Single blue = (color & 0x1F) * 8.225806f;
Int32 grayscale = (Int32)(((red * 0.299f) + (green * 0.587f) + (blue * 0.114f)) * 0.1215686f);
if (grayscale < 0)
grayscale = 0;
else if (grayscale > 0x1F)
grayscale = 0x1F;
return grayscale;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.