コード例 #1
0
            public int Compare(object left, object right)
            {
                ExtendedColors cLeft  = (ExtendedColors)left;
                ExtendedColors cRight = (ExtendedColors)right;

                return(string.Compare(cLeft.Name, cRight.Name, false, CultureInfo.InvariantCulture));
            }
コード例 #2
0
 /// <summary>
 /// Creates a ExtendedColors structure from the
 /// </summary>
 /// <param name="color">Enumeration of type KnownExtendedColors</param>
 /// <returns>The ExtendedColors structure that this method creates based on given enumeration otherwise an empty ExtendedColors.</returns>
 public static ExtendedColors FromKnownColor(KnownExtendedColors color)
 {
     if (IsEnumValid(unchecked ((int)color), (int)KnownExtendedColors.AbaloneShell, (int)KnownExtendedColors.ZurichWhite)) // end side
     {
         return(ExtendedColors.FromName(color.ToString()));
     }
     return(new ExtendedColors(color));
 }
コード例 #3
0
        /// <summary>
        /// Tests whether the specified object is a ExtendedColors structure and is equivalent to this ExtendedColors structure.
        /// </summary>
        /// <param name="obj">The object to test. </param>
        /// <returns>true if obj is a Color structure equivalent to this ExtendedColors structure; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            if (obj is ExtendedColors)
            {
                ExtendedColors right = (ExtendedColors)obj;
                if (m_Value == right.m_Value &&
                    m_State == right.m_State &&
                    m_KnownColor == right.m_KnownColor)
                {
                    if (m_Name == right.m_Name)
                    {
                        return(true);
                    }

                    if (m_Name == (object)null || right.m_Name == (object)null)
                    {
                        return(false);
                    }

                    return(m_Name.Equals(m_Name));
                }
            }
            return(false);
        }
コード例 #4
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string strValue = value as string;

            if (strValue != null)
            {
                object obj  = null;
                string text = strValue.Trim();

                if (text.Length == 0)
                {
                    obj = Color.Empty;
                }
                else
                {
                    // First, check to see if this is a standard name.
                    //
                    obj = GetNamedColor(text);

                    if (obj == null)
                    {
                        if (culture == null)
                        {
                            culture = CultureInfo.CurrentCulture;
                        }

                        char sep = culture.TextInfo.ListSeparator[0];
                        bool tryMappingToKnownColor = true;

                        TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));

                        // If the value is a 6 digit hex number only, then
                        // we want to treat the Alpha as 255, not 0
                        //
                        if (text.IndexOf(sep) == -1)
                        {
                            // text can be '' (empty quoted string)
                            if (text.Length >= 2 && (text[0] == '\'' || text[0] == '"') && text[0] == text[text.Length - 1])
                            {
                                // In quotes means a named value
                                string colorName = text.Substring(1, text.Length - 2);
                                obj = ExtendedColors.FromName(colorName);
                                tryMappingToKnownColor = false;
                            }
                            else if ((text.Length == 7 && text[0] == '#') ||
                                     (text.Length == 8 && (text.StartsWith("0x") || text.StartsWith("0X"))) ||
                                     (text.Length == 8 && (text.StartsWith("&h") || text.StartsWith("&H"))))
                            {
                                // Note: ConvertFromString will raise exception if value cannot be converted.
                                obj = ExtendedColors.FromArgb(unchecked ((int)(0xFF000000 | (uint)(int)intConverter.ConvertFromString(context, culture, text))));
                            }
                        }

                        // Nope.  Parse the RGBA from the text.
                        //
                        if (obj == null)
                        {
                            string[] tokens = text.Split(new char[] { sep });
                            int[]    values = new int[tokens.Length];
                            for (int i = 0; i < values.Length; i++)
                            {
                                values[i] = unchecked ((int)intConverter.ConvertFromString(context, culture, tokens[i]));
                            }

                            // We should now have a number of parsed integer values.
                            // We support 1, 3, or 4 arguments:
                            //
                            // 1 -- full ARGB encoded
                            // 3 -- RGB
                            // 4 -- ARGB
                            //
                            switch (values.Length)
                            {
                            case 1:
                                obj = ExtendedColors.FromArgb(values[0]);
                                break;

                            case 3:
                                obj = ExtendedColors.FromArgb(values[0], values[1], values[2]);
                                break;

                            case 4:
                                obj = ExtendedColors.FromArgb(values[0], values[1], values[2], values[3]);
                                break;
                            }
                            tryMappingToKnownColor = true;
                        }

                        if ((obj != null) && tryMappingToKnownColor)
                        {
                            // Now check to see if this color matches one of our known colors.
                            // If it does, then substitute it.  We can only do this for "Colors"
                            // because system colors morph with user settings.
                            //
                            int targetARGB = ((ExtendedColors)obj).ToArgb();

                            foreach (ExtendedColors c in Colors.Values)
                            {
                                if (c.ToArgb() == targetARGB)
                                {
                                    obj = c;
                                    break;
                                }
                            }
                        }
                    }

                    if (obj == null)
                    {
                        throw new ArgumentException("Invalid ExtendedColors text");
                    }
                }
                return(obj);
            }
            return(base.ConvertFrom(context, culture, value));
        }
コード例 #5
0
        /// <summary>
        /// Converts the specified object to another type.
        /// </summary>
        /// <param name="context">A formatter context. Use this object to extract additional information about the environment from which this converter is being invoked. Always check whether this value is null. Also, properties on the context object may return null. </param>
        /// <param name="culture">A CultureInfo that specifies the culture to represent the color. </param>
        /// <param name="value">The object to convert. </param>
        /// <param name="destinationType">The type to convert the object to. </param>
        /// <returns>An Object representing the converted value.</returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }

            if (value is ExtendedColors)
            {
                if (destinationType == typeof(string))
                {
                    ExtendedColors c = (ExtendedColors)value;

                    if (c == ExtendedColors.Empty)
                    {
                        return(string.Empty);
                    }
                    else
                    {
                        // If this is a known color, then ExtendedColors can provide its own
                        // name.  Otherwise, we fabricate an ARGB value for it.
                        //
                        if (c.IsKnownColor)
                        {
                            return(c.Name);
                        }
                        else if (c.IsNamedColor)
                        {
                            return("'" + c.Name + "'");
                        }
                        else
                        {
                            if (culture == null)
                            {
                                culture = CultureInfo.CurrentCulture;
                            }
                            string        sep          = culture.TextInfo.ListSeparator + " ";
                            TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
                            string[]      args;
                            int           nArg = 0;

                            if (c.A < 255)
                            {
                                args         = new string[4];
                                args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.A);
                            }
                            else
                            {
                                args = new string[3];
                            }

                            // Note: ConvertToString will raise exception if value cannot be converted.
                            args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.R);
                            args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.G);
                            args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.B);

                            // Now slam all of these together with the fantastic Join
                            // method.
                            //
                            return(string.Join(sep, args));
                        }
                    }
                }
                if (destinationType == typeof(InstanceDescriptor))
                {
                    MemberInfo member = null;
                    object[]   args   = null;

                    ExtendedColors c = (ExtendedColors)value;

                    if (c.IsEmpty)
                    {
                        member = typeof(ExtendedColors).GetField("Empty");
                    }
                    else if (c.IsKnownColor)
                    {
                        member = typeof(ExtendedColors).GetProperty(c.Name);
                    }
                    else if (c.A != 255)
                    {
                        member = typeof(ExtendedColors).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) });
                        args   = new object[] { c.A, c.R, c.G, c.B };
                    }
                    else if (c.IsNamedColor)
                    {
                        member = typeof(ExtendedColors).GetMethod("FromName", new Type[] { typeof(string) });
                        args   = new object[] { c.Name };
                    }
                    else
                    {
                        member = typeof(ExtendedColors).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int) });
                        args   = new object[] { c.R, c.G, c.B };
                    }

                    Debug.Assert(member != null, "Could not convert color to member.  Did someone change method name / signature and not update Colorconverter?");
                    if (member != null)
                    {
                        return(new InstanceDescriptor(member, args));
                    }
                    else
                    {
                        return(null);
                    }
                }
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
コード例 #6
0
 /// <summary>
 /// Creates a ExtendedColors structure from the specified 8-bit color values (red, green, and blue). The alpha value is implicitly 255 (fully opaque). Although this method allows a 32-bit value to be passed for each color component, the value of each component is limited to 8 bits.
 /// </summary>
 /// <param name="red">The red component value for the new ExtendedColors. Valid values are 0 through 255. </param>
 /// <param name="green">The green component value for the new ExtendedColors. Valid values are 0 through 255. </param>
 /// <param name="blue">The blue component value for the new ExtendedColors. Valid values are 0 through 255. </param>
 /// <returns>The Color that this method creates.</returns>
 public static ExtendedColors FromArgb(int red, int green, int blue)
 {
     return(ExtendedColors.FromArgb(255, red, green, blue));
 }
コード例 #7
0
 /// <summary>
 /// Creates a ExtendedColors structure from the specified Color structure, but with the new specified alpha value. Although this method allows a 32-bit value to be passed for the alpha value, the value is limited to 8 bits.
 /// </summary>
 /// <param name="alpha">The alpha value for the new Color. Valid values are 0 through 255.</param>
 /// <param name="baseColor">The ExtendedColors from which to create the new ExtendedColors. </param>
 /// <returns>The Color that this method creates.</returns>
 public static ExtendedColors FromArgb(int alpha, ExtendedColors baseColor)
 {
     CheckByte(alpha, "alpha");
     // unchecked - because we already checked that alpha is a byte in CheckByte above
     return(new ExtendedColors(MakeArgb(unchecked ((byte)alpha), baseColor.R, baseColor.G, baseColor.B), StateARGBValueValid, null, (KnownExtendedColors)0));
 }