Reputation:
I'm developing a game client in which texture colors are stored as int values. 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. 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?
Upvotes: 1
Views: 2424
Reputation: 23685
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;
}
Upvotes: 1