竟然这么简单?
int color = 65536 * b + 256 * g + r;
还有人这样写:
using Color = System.Windows.Media.Color;
private static uint ToUint(this Color c)
{
return (uint)(((c.A << 24) | (c.R << 16) | (c.G << 8) | c.B) & 0xffffffffL);
}
private static Color ToColor(this uint value)
{
return Color.FromArgb((byte)((value >> 24) & 0xFF),
(byte)((value >> 16) & 0xFF),
(byte)((value >> 8) & 0xFF),
(byte) (value & 0xFF));
}
还有人这样写:
Color.FromArgb(16777215)//数字形式
Color.FromArgb(0xFFFFFF)//十六进制数字形式
Color.FromArgb(int.Parse("FFFFFF", System.Globalization.NumberStyles.AllowHexSpecifier));//字符串形式
后来,我这样写:
private int Color2Int(Color c)
{
return 65536 * c.B + 256 * c.G + c.R;
}
private Color IntToColor(uint value)
{
int v = (int)value;
return Color.FromArgb(v & 0x0000ff, (v & 0x00ff00) >> 8, (v & 0xff0000) >> 16);
}