Given a color string in one of the formats listed below, return an array of the RGB elements that make it up.
For Example:
"RGB(255,255,255)"
"#FFFFFF"
"#FFF"
All of these examples return [255, 255, 255]
- Case does not matter
- #abc translates to #aabbcc
- There will be no spaces in the string
One solution (73 IL):
s = s.Replace("RGB(", "").Replace(")", "").Replace("#", "");
if (s.Contains(",")) { return s.Split(','); }
int[] a = new int[3];
int i = 0;
do { a[i] = (s.Length == 3 ? 17 : 1) * Convert.ToInt32(s.Substring(i * s.Length / 3, s.Length / 3), 16); } while (++i < 3);
return a;
Another (68 IL, I think):
object[] colorMyWorld(string htmlColor)
{
if (htmlColor.StartsWith("#"))
{
Color myColor = ColorTranslator.FromHtml(htmlColor);
return new object[] { myColor.R, myColor.G, myColor.B };
}
else
{
string[] components = htmlColor.Split("(),".ToCharArray());
return new object[] { components[1], components[2], components[3] };
}
}