16进制转换为Color
public static Color TryParseColor(string rgba)
{
// 如果字符串包含 #,先移除它
if (rgba.StartsWith("#"))
{
rgba = rgba.Substring(1); // 移除字符串前面的 #
}
return rgba.HexToColor();
}
Color转换为16进制
/// 颜色结构转进制字符
/// </summary>
/// <param name="color">颜色结构</param>
/// <returns></returns>
public static string TryParseString(Color color)
{
string colorHex = color.ColorToHex();
return colorHex;
}
/// <summary>
/// 将颜色值转化成索引
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static string ColorToHex(this Color color)
{
int r = Mathf.RoundToInt(color.r * 255.0f);
int g = Mathf.RoundToInt(color.g * 255.0f);
int b = Mathf.RoundToInt(color.b * 255.0f);
int a = Mathf.RoundToInt(color.a * 255.0f);
string hex = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}", r, g, b, a);
return hex;
}