unity Color和Hex转化
程序员文章站
2024-03-18 18:11:34
...
color to hex
public static string ColorToHex(Color32 color)
{
return color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2") + color.a.ToString("X2");
}
hex to color
public static Color HexToColor(string hex)
{
hex = hex.Replace("0x", string.Empty);
hex = hex.Replace("#", string.Empty);
byte a = byte.MaxValue;
byte r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(6, 2), NumberStyles.HexNumber);
}
return new Color32(r, g, b, a);
}
歪比歪比 歪比巴卜