代码语言:javascript复制
public class ColorTool
{
/// <summary>
/// 从ARGB转换
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static SolidColorBrush FromArgb(string color)
{
int[] a = new int[4];
int[] b = new int[8];
string rg1 = "#[a-fA-F0-9]{8}";
Regex r1 = new Regex(rg1);
if (r1.IsMatch(color))
{
for (int i = 0; i < 8; i )
{
switch (color.Substring(i 1, 1))
{
case "a":
case "A": b[i] = 10; break;
case "b":
case "B": b[i] = 11; break;
case "c":
case "C": b[i] = 12; break;
case "d":
case "D": b[i] = 13; break;
case "e":
case "E": b[i] = 14; break;
case "f":
case "F": b[i] = 15; break;
default: b[i] = int.Parse(color.Substring(i 1, 1)); break;
}
}
}
else
{
string rg2 = @"^[A-Fa-f0-9]{8}";
Regex r2 = new Regex(rg2);
if (r2.IsMatch(color))
{
for (int i = 0; i < 8; i )
{
switch (color.Substring(i, 1))
{
case "a":
case "A": b[i] = 10; break;
case "b":
case "B": b[i] = 11; break;
case "c":
case "C": b[i] = 12; break;
case "d":
case "D": b[i] = 13; break;
case "e":
case "E": b[i] = 14; break;
case "f":
case "F": b[i] = 15; break;
default: b[i] = int.Parse(color.Substring(i, 1)); break;
}
}
}
}
a[0] = b[0] * 16 b[1];
a[1] = b[2] * 16 b[3];
a[2] = b[4] * 16 b[5];
a[3] = b[6] * 16 b[7];
return (new SolidColorBrush(Color.FromArgb((byte)a[0], (byte)a[1], (byte)a[2], (byte)a[3])));
}
/// <summary>
/// 从Color转换
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static SolidColorBrush FromColor(Color color)
{
return new SolidColorBrush(color);
}
}