Parse Color String - CSharp System.Xml

CSharp examples for System.Xml:XML String

Description

Parse Color String

Demo Code


using System.Text;
using System.Reflection;
using System.Xml;
using System.Collections.Generic;
using System.Collections;
using System.Net;
using System.Drawing;
using System;//from   ww w  . j  ava  2 s  .  co m

public class Main{
        private static Color ParseColor(string value)
      {
         try
         {
            string[] values = value.Split(DELIMITERS);
            if (values.Length == 3)
            {
               byte r = byte.Parse(values[0].Trim());
               byte g = byte.Parse(values[1].Trim());
               byte b = byte.Parse(values[2].Trim());

               return Color.FromArgb(r, g, b);
            }
#if WINDOWS
            else if (values.Length == 4)
            {
               byte r = byte.Parse(values[0].Trim());
               byte g = byte.Parse(values[1].Trim());
               byte b = byte.Parse(values[2].Trim());
               byte a = byte.Parse(values[3].Trim());

               return Color.FromArgb(r, g, b, a);
            }
#endif
            else
            {
               MemberInfo[] colorMemberInfoList = typeof(Color).GetMember(value);
               if (colorMemberInfoList.Length != 1)
               {
                  throw new Exception("Could not find color value: " + value);
               }

               return (Color)typeof(Color).InvokeMember(colorMemberInfoList[0].Name, BindingFlags.GetProperty, null, null, null);
            }
         }
         catch (Exception ex)
         {
            throw new InvalidCastException("Could not parse color", ex);
         }
      }
}

Related Tutorials