/// <summary>
/// 把十六进制的字符串转化为Color
/// </summary>
/// <param name="strHex">没有#号键,因为后台不加</param>
/// <returns></returns>
public static Color TransformHexToRGB(string strHex)
{
if (strHex.Length < 6 || strHex.Length > 8 || strHex.Length == 7)
return new Color(0, 0, 0, 0);
byte br = byte.Parse(strHex.Substring(0, 2), NumberStyles.HexNumber);
byte bg = byte.Parse(strHex.Substring(2, 2), NumberStyles.HexNumber);
byte bb = byte.Parse(strHex.Substring(3, 2), NumberStyles.HexNumber);
byte ba = 255;
if (strHex.Length == 8)
ba = byte.Parse(strHex.Substring(6, 2), NumberStyles.HexNumber);
float r = br / 255f;
float g = bg / 255f;
float b = bb / 255f;
float a = ba / 255f;
return new Color(r, g, b, a); //RGBA(0.063, 0.231, 0.953, 1.000)
}
C# 把十六进制的字符串转化为Color
最新推荐文章于 2024-05-14 16:27:49 发布