/// <summary> /// 解析颜色值。 /// </summary> /// <returns>解析后的字符串。</returns> public string ParseColor(string text) { Color def = GetComponent <Text>().color; int index = 0; m_ColorInfos.Clear(); CacheSB.Length = 0; CacheSB.Append(UnderLineChar); //首个解析函数要加下划线占位符 foreach (Match match in ColorRegex.Matches(text)) { string colorstr = match.Groups[1].Value.Trim(); string innertext = match.Groups[2].Value; ColorInfo info = new ColorInfo(); CacheSB.Append(text.Substring(index, match.Index - index)); //匹配目标前的那一部分 info.StartIndex = CacheSB.Length; info.EndIndex = info.StartIndex + innertext.Length; info.TextColor = UiUtil.ToColor(colorstr, def); m_ColorInfos.Add(info); CacheSB.Append(innertext); index = match.Index + match.Length; } CacheSB.Append(text.Substring(index, text.Length - index)); return(CacheSB.ToString()); }
public static Color GetColor(string str) //根据 "#FFFFFFFF" 或者 "#FFFF"格式来获取Color { if (ColorRegex.IsMatch(str)) { if (str.Length == 5) //#RGBA { string r = TwoChar2OneString(str[1]); string g = TwoChar2OneString(str[2]); string b = TwoChar2OneString(str[3]); string a = TwoChar2OneString(str[4]); int aa = GetNum(a); int rr = GetNum(r); int gg = GetNum(g); int bb = GetNum(b); return(GetColor(rr, gg, bb, aa)); } else if (str.Length == 9) //#RRGGBBAA { int rr = GetNum(str.Substring(1, 2)); int gg = GetNum(str.Substring(3, 2)); int bb = GetNum(str.Substring(5, 2)); int aa = GetNum(str.Substring(7, 2)); return(GetColor(rr, gg, bb, aa)); } else { MyLog.Red("不可能吧 返回白色"); return(Color.white); } } else { MyLog.Red("颜色代码不符合 #FFFFFFFF 或者 #0000 —— " + str); return(Color.white); } }