我有一个将 RGB555 值转换为 System.Drawing.Color 对象的算法;
public static Color ToColor(ushort color)
{
int a = color & 0x8000;
int r = color & 0x7C00;
int g = color & 0x03E0;
int b = color & 0x1F;
int rgb = (r << 9) | (g << 6) | (b << 3);
return Color.FromArgb((a * 0x1FE00) | rgb | ((rgb >> 5) & 0x070707));
}
一位朋友为我写了这个方法(按位移位有点过头了),反转这段代码的最有效方法是什么?
感谢您的任何建议,几天来我一直在努力寻找答案,因此任何见解都将是一股清新的空气!
编辑
这个问题已经解决了一段时间,但我想我会回来用最终结果更新我的帖子 - 感谢所有回答的人!
public struct Color555 : IEquatable<Color555>, IComparable<Color555>, IEquatable<ushort>, IComparable<ushort>
{
public static readonly Color555 MinValue = ushort.MinValue;
public static readonly Color555 MaxValue = ushort.MaxValue;
private readonly ushort _Value;
public Color555(Color value)
{
uint c = (uint)value.ToArgb();
_Value = (ushort)(((c >> 16) & 0x8000 | (c >> 9) & 0x7C00 | (c >> 6) & 0x03E0 | (c >> 3) & 0x1F));
}
public Color555(ushort value)
{
_Value = value;
}
public override int GetHashCode()
{
return _Value.GetHashCode();
}
public override bool Equals(object obj)
{
return (obj is ushort && Equals((ushort)obj)) || (obj is Color555 && Equals((Color555)obj));
}
public bool Equals(ushort other)
{
return _Value == other;
}
public bool Equals(Color555 other)
{
return _Value == other._Value;
}
public int CompareTo(Color555 other)
{
return _Value.CompareTo(other._Value);
}
public int CompareTo(ushort other)
{
return _Value.CompareTo(other);
}
public override string ToString()
{
return String.Format("{0}", _Value);
}
public string ToString(string format)
{
return String.Format(format, _Value);
}
public string ToString(IFormatProvider provider)
{
return String.Format(provider, "{0}", _Value);
}
public string ToString(string format, IFormatProvider provider)
{
return String.Format(provider, format, _Value);
}
public int ToArgb()
{
return ToColor().ToArgb();
}
public Color ToColor()
{
int a = _Value & 0x8000;
int r = _Value & 0x7C00;
int g = _Value & 0x03E0;
int b = _Value & 0x1F;
int rgb = (r << 9) | (g << 6) | (b << 3);
return Color.FromArgb((a * 0x1FE00) | rgb | ((rgb >> 5) & 0x070707));
}
public static bool operator ==(Color555 l, Color555 r)
{
return l.Equals(r);
}
public static bool operator !=(Color555 l, Color555 r)
{
return !l.Equals(r);
}
public static implicit operator Color555(Color value)
{
return new Color555(value);
}
public static implicit operator Color555(ushort value)
{
return new Color555(value);
}
public static implicit operator ushort(Color555 value)
{
return value._Value;
}
}