public static Color MakeColor(string aStr)
{
Color clr = new Color(0, 0, 0);
if (aStr != null && aStr.Length > 0)
{
try
{
int commaCount = 0;
foreach (char c in aStr)
{
if (c == ',') commaCount++;
}
if (aStr.Substring(0, 1) == "#")
{ // #FFFFFF format
string str = aStr.Substring(1, aStr.Length - 1);
clr.r = int.Parse(str.Substring(0, 2),
NumberStyles.AllowHexSpecifier) / 255.0f;
clr.g = int.Parse(str.Substring(2, 2),
NumberStyles.AllowHexSpecifier) / 255.0f;
clr.b = int.Parse(str.Substring(4, 2),
NumberStyles.AllowHexSpecifier) / 255.0f;
if (str.Length == 8)
{
clr.a = int.Parse(str.Substring(6, 2),
NumberStyles.AllowHexSpecifier) / 255.0f;
}
else clr.a = 1.0f;
}
else if (commaCount >= 2 && commaCount <= 3 && aStr.Length >= 2 * commaCount + 1)
{ // 0.3, 1.0, 0.2 format
int p0 = 0;
int p1 = 0;
int c = 0;
p1 = aStr.IndexOf(",", p0);
if (p1 <= 0)
throw new FormatException();
while (p1 > p0 && c < 4)
{
float value = float.Parse(aStr.Substring(p0, p1 - p0));
value = Mathf.Clamp(value, 0, 1);
clr[c++] = value;
p0 = p1 + 1;
if (p0 < aStr.Length) p1 = aStr.IndexOf(",", p0);
if (p1 < 0) p1 = aStr.Length;
}
if (c < 4) clr.a = 1.0f;
}
else
throw new FormatException();
}
catch (Exception e)
{
if (debugLogMessagesOn)
Debug.LogError("MakeColor could not convert " + aStr + " to Color. " + e);
return Color.magenta;
}
}
else
return Color.magenta;
return clr;
}