Beispiel #1
0
        public object ConvertFrom(XamlNamespaces namespaces, object value)
        {
            if (value is string)
            {
                string stringValue = ((string)value).Trim();

                if (stringValue.StartsWith("#") && stringValue.Length == 9)
                {
                    return(Color.FromUInt32(Granular.Compatibility.Convert.ToUInt32(stringValue.Substring(1), 16)));
                }

                if (stringValue.StartsWith("#") && stringValue.Length == 7)
                {
                    return(Color.FromUInt32(0xff000000 | Granular.Compatibility.Convert.ToUInt32(stringValue.Substring(1), 16)));
                }

                if (stringValue.StartsWith("#") && stringValue.Length == 4)
                {
                    return(Color.FromUInt32(0xff000000 | Granular.Compatibility.Convert.ToUInt32(String.Format("{0}{0}{1}{1}{2}{2}", stringValue[1].ToString(), stringValue[2].ToString(), stringValue[3].ToString()), 16)));
                }

                PropertyInfo propertyInfo = typeof(Colors).GetProperty(stringValue, BindingFlags.Static | BindingFlags.Public);
                if (propertyInfo != null)
                {
                    return(propertyInfo.GetValue(null, new object[0]));
                }
            }

            throw new Granular.Exception("Can't convert \"{0}\" to Color", value);
        }