public Color HexColor(String hex)//C# 色碼 轉color
{//https://dotblogs.com.tw/junegoat/2012/06/22/color-convert-form-hex-string
//將井字號移除
hex = hex.Replace(“#”, “”);
byte a = 255;
byte r = 255;
byte g = 255;
byte b = 255;
int start = 0;
//處理ARGB字串
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
start = 2;
}
// 將RGB文字轉成byte
r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);
return Color.FromArgb(a, r, g, b);
}
|