public int Compare(object left, object right) { XmlColor color = (XmlColor)left; XmlColor color2 = (XmlColor)right; return(string.Compare(color.Name, color2.Name, false, CultureInfo.InvariantCulture)); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (value is XmlColor) { if (destinationType == typeof(string)) { string[] strArray; XmlColor color = (XmlColor)value; if (color == XmlColor.Empty) { return(string.Empty); } if (color.IsKnownColor) { return(color.Name); } if (color.IsNamedColor) { return("'" + color.Name + "'"); } if (culture == null) { culture = CultureInfo.CurrentCulture; } string separator = culture.TextInfo.ListSeparator + " "; TypeConverter converter = TypeDescriptor.GetConverter(typeof(int)); int num = 0; if (color.A < 0xff) { strArray = new string[4]; strArray[num++] = converter.ConvertToString(context, culture, color.A); } else { strArray = new string[3]; } strArray[num++] = converter.ConvertToString(context, culture, color.R); strArray[num++] = converter.ConvertToString(context, culture, color.G); strArray[num++] = converter.ConvertToString(context, culture, color.B); return(string.Join(separator, strArray)); } if (destinationType == typeof(InstanceDescriptor)) { MemberInfo member = null; object[] arguments = null; XmlColor color2 = (XmlColor)value; if (color2.IsEmpty) { member = typeof(XmlColor).GetField("Empty"); } else if (color2.IsSystemColor) { member = typeof(SystemColors).GetProperty(color2.Name); } else if (color2.IsKnownColor) { member = typeof(XmlColor).GetProperty(color2.Name); } else if (color2.A != 0xff) { member = typeof(XmlColor).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }); arguments = new object[] { color2.A, color2.R, color2.G, color2.B }; } else if (color2.IsNamedColor) { member = typeof(XmlColor).GetMethod("FromName", new Type[] { typeof(string) }); arguments = new object[] { color2.Name }; } else { member = typeof(XmlColor).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int) }); arguments = new object[] { color2.R, color2.G, color2.B }; } if (member != null) { return(new InstanceDescriptor(member, arguments)); } return(null); } } return(base.ConvertTo(context, culture, value, destinationType)); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string str = value as string; if (str == null) { return(base.ConvertFrom(context, culture, value)); } object namedColor = null; string name = str.Trim(); if (name.Length == 0) { return(XmlColor.Empty); } namedColor = GetNamedColor(name); if (namedColor == null) { if (culture == null) { culture = CultureInfo.CurrentCulture; } char ch = culture.TextInfo.ListSeparator[0]; bool flag = true; TypeConverter converter = TypeDescriptor.GetConverter(typeof(int)); if (name.IndexOf(ch) == -1) { if (((name.Length >= 2) && ((name[0] == '\'') || (name[0] == '"'))) && (name[0] == name[name.Length - 1])) { namedColor = XmlColor.FromName(name.Substring(1, name.Length - 2)); flag = false; } else if ((((name.Length == 7) && (name[0] == '#')) || ((name.Length == 8) && (name.StartsWith("0x") || name.StartsWith("0X")))) || ((name.Length == 8) && (name.StartsWith("&h") || name.StartsWith("&H")))) { namedColor = XmlColor.FromArgb(-16777216 | ((int)converter.ConvertFromString(context, culture, name))); } } if (namedColor == null) { string[] strArray = name.Split(new char[] { ch }); int[] numArray = new int[strArray.Length]; for (int i = 0; i < numArray.Length; i++) { numArray[i] = (int)converter.ConvertFromString(context, culture, strArray[i]); } switch (numArray.Length) { case 1: namedColor = XmlColor.FromArgb(numArray[0]); break; case 3: namedColor = XmlColor.FromArgb(numArray[0], numArray[1], numArray[2]); break; case 4: namedColor = XmlColor.FromArgb(numArray[0], numArray[1], numArray[2], numArray[3]); break; } flag = true; } if ((namedColor != null) && flag) { int num2 = ((XmlColor)namedColor).ToArgb(); foreach (XmlColor color in Colors.Values) { if (color.ToArgb() == num2) { namedColor = color; break; } } } } if (namedColor == null) { //throw new ArgumentException(SR.GetString("InvalidColor", new object[] { name })); } return(namedColor); }