开发者

c# read rgb from A1R5G5B5 image type

I开发者_如何学JAVA need in c# convert 2 bytes (16 bits) that are a pixel of an image of type A1R5G5B5 (so 1 bit alpha, 5 bits red, 5 bits green, 5 bits blue) from a standard 0-255 value thanks in advance


This is a quick-and-dirty solution, but it should work for you.

using System.Drawing;

class ShortColor
{
    public bool Alpha { get; set; }

    public byte Red   { get; set; }
    public byte Green { get; set; }
    public byte Blue  { get; set; }

    public ShortColor(short value)
    {
         this.Alpha = (value & 0x8000) > 0;

         this.Red = (byte)((value & 0x7C64) >> 10);
         this.Green = (byte)((value & 0x3E0) >> 5);
         this.Blue = (byte)((value & 0x001F));
    }

    public ShortColor(Color color)
    {
         this.Alpha = color.A != 0;

         this.Red = (byte)(color.R / 8);
         this.Green = (byte)(color.G / 8);
         this.Blue = (byte)(color.B / 8);
    }

    public static explicit operator Color(ShortColor shortColor)
    {
         return Color.FromArgb(
             shortColor.Alpha ? 255 : 0,
             shortColor.Red * 8,
             shortColor.Green * 8,
             shortColor.Blue * 8
         );
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜