Parse Color with Color.FromArgb
using System;
using System.Collections.Generic;
using System.Windows.Media;
using System.Reflection;
using System.Globalization;
namespace NASA.BeAMartian.Utils
{
public class ColorUtils
{
public static Color ParseColor(string color)
{
string capitalizedColor = color;
MemberInfo[] infos = typeof(Colors).GetMember(capitalizedColor);
if (infos.Length > 0)
{
return (Color)typeof(Colors).InvokeMember(capitalizedColor, BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty, null, null, null);
}
else if (color.IndexOf('#') == 0)
{
return Color.FromArgb(
byte.Parse(color.Substring(1, 2), NumberStyles.HexNumber),
byte.Parse(color.Substring(3, 2), NumberStyles.HexNumber),
byte.Parse(color.Substring(5, 2), NumberStyles.HexNumber),
byte.Parse(color.Substring(7, 2), NumberStyles.HexNumber));
}
return Colors.White;
}
}
}
Related examples in the same category