As an optimization i want to combine some text elements that are on top one another, I want to say something like this as an arbitrary example:<color=green> Hello </color> <b> - </b> <color=red> World </color>
Hello - World
my inital instinct was to do something like this:
Color color; // = some color
string.Format(<color={0}> Hello </color> <b> - </b> <color=red> World </color>, color.ToString());
obvious problem, ToString spits out RGBA, I need either the actual name of the color or better yet, the hex value for it.
a quick search revealed this: Unity - Scripting API: ColorUtility.TryParseHtmlString
but looking through it I started thinking this isn’t gonna get me anywhere good with how you end up using it.
I need the function to return the value and not the bool if it did it or not, I’m good with throwing an exception if something went wrong (that is something you want after all)
found this on stackoverflow
public static string ConvertRgbaToHex(string rgba)
{
if (!Regex.IsMatch(rgba, @"rgba\((\d{1,3},\s*){3}(0(\.\d+)?|1)\)"))
throw new FormatException("rgba string was in a wrong format");
var matches = Regex.Matches(rgba, @"\d+");
StringBuilder hexaString = new StringBuilder("#");
for(int i = 0; i < matches.Count - 1; i++)
{
int value = Int32.Parse(matches[i].Value);
hexaString.Append(value.ToString("X"));
}
return hexaString.ToString();
}
Seems to be what I need and fairly optimized, any reason not to use it?( i don’t fully understand what’s going on in it)
any other stuff you have to share about rich text and unity?