Exemplo n.º 1
0
 private static void InitializeHtmlSysColorTable()
 {
     htmlSysColorTable = new Hashtable(26);
     htmlSysColorTable["activeborder"]        = ColorExt.FromKnownColor(KnownColor.ActiveBorder);
     htmlSysColorTable["activecaption"]       = ColorExt.FromKnownColor(KnownColor.ActiveCaption);
     htmlSysColorTable["appworkspace"]        = ColorExt.FromKnownColor(KnownColor.AppWorkspace);
     htmlSysColorTable["background"]          = ColorExt.FromKnownColor(KnownColor.Desktop);
     htmlSysColorTable["buttonface"]          = ColorExt.FromKnownColor(KnownColor.Control);
     htmlSysColorTable["buttonhighlight"]     = ColorExt.FromKnownColor(KnownColor.ControlLightLight);
     htmlSysColorTable["buttonshadow"]        = ColorExt.FromKnownColor(KnownColor.ControlDark);
     htmlSysColorTable["buttontext"]          = ColorExt.FromKnownColor(KnownColor.ControlText);
     htmlSysColorTable["captiontext"]         = ColorExt.FromKnownColor(KnownColor.ActiveCaptionText);
     htmlSysColorTable["graytext"]            = ColorExt.FromKnownColor(KnownColor.GrayText);
     htmlSysColorTable["highlight"]           = ColorExt.FromKnownColor(KnownColor.Highlight);
     htmlSysColorTable["highlighttext"]       = ColorExt.FromKnownColor(KnownColor.HighlightText);
     htmlSysColorTable["inactiveborder"]      = ColorExt.FromKnownColor(KnownColor.InactiveBorder);
     htmlSysColorTable["inactivecaption"]     = ColorExt.FromKnownColor(KnownColor.InactiveCaption);
     htmlSysColorTable["inactivecaptiontext"] = ColorExt.FromKnownColor(KnownColor.InactiveCaptionText);
     htmlSysColorTable["infobackground"]      = ColorExt.FromKnownColor(KnownColor.Info);
     htmlSysColorTable["infotext"]            = ColorExt.FromKnownColor(KnownColor.InfoText);
     htmlSysColorTable["menu"]              = ColorExt.FromKnownColor(KnownColor.Menu);
     htmlSysColorTable["menutext"]          = ColorExt.FromKnownColor(KnownColor.MenuText);
     htmlSysColorTable["scrollbar"]         = ColorExt.FromKnownColor(KnownColor.ScrollBar);
     htmlSysColorTable["threeddarkshadow"]  = ColorExt.FromKnownColor(KnownColor.ControlDarkDark);
     htmlSysColorTable["threedface"]        = ColorExt.FromKnownColor(KnownColor.Control);
     htmlSysColorTable["threedhighlight"]   = ColorExt.FromKnownColor(KnownColor.ControlLight);
     htmlSysColorTable["threedlightshadow"] = ColorExt.FromKnownColor(KnownColor.ControlLightLight);
     htmlSysColorTable["window"]            = ColorExt.FromKnownColor(KnownColor.Window);
     htmlSysColorTable["windowframe"]       = ColorExt.FromKnownColor(KnownColor.WindowFrame);
     htmlSysColorTable["windowtext"]        = ColorExt.FromKnownColor(KnownColor.WindowText);
 }
Exemplo n.º 2
0
        /// <include file='doc\ColorConverter.uex' path='docs/doc[@for="ColorConverter.ConvertTo"]/*' />
        /// <devdoc>
        ///      Converts the given object to another type.  The most common types to convert
        ///      are to and from a string object.  The default implementation will make a call
        ///      to ToString on the object if the object is valid and if the destination
        ///      type is string.  If this cannot convert to the desitnation type, this will
        ///      throw a NotSupportedException.
        /// </devdoc>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }

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

                    if (c == Color.Empty)
                    {
                        return(string.Empty);
                    }
                    else
                    {
                        // If this is a known color, then Color can provide its own
                        // name.  Otherwise, we fabricate an ARGB value for it.
                        //
                        if (ColorExt.IsKnownColor(c))
                        {
                            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;

                    Color c = (Color)value;

                    if (c.IsEmpty)
                    {
                        member = typeof(Color).GetField("Empty");
                    }
                    else if (ColorExt.IsSystemColor(c))
                    {
                        member = typeof(SystemColors).GetProperty(c.Name);
                    }
                    else if (ColorExt.IsKnownColor(c))
                    {
                        member = typeof(Color).GetProperty(c.Name);
                    }
                    else if (c.A != 255)
                    {
                        member = typeof(Color).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(Color).GetMethod("FromName", new Type[] { typeof(string) });
                        args   = new object[] { c.Name };
                    }
                    else
                    {
                        member = typeof(Color).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));
        }
Exemplo n.º 3
0
        public static string ToHtml(Color c)
        {
            string colorString = String.Empty;

            if (c.IsEmpty)
            {
                return(colorString);
            }
            bool flag = true;

            if (ColorExt.IsKnownColor(c))
            {
                flag = false;
                switch (ColorExt.ToKnownColor(c))
                {
                case KnownColor.ActiveBorder: colorString = "activeborder"; break;

                case KnownColor.GradientActiveCaption:
                case KnownColor.ActiveCaption: colorString = "activecaption"; break;

                case KnownColor.AppWorkspace: colorString = "appworkspace"; break;

                case KnownColor.Desktop: colorString = "background"; break;

                case KnownColor.Control: colorString = "buttonface"; break;

                case KnownColor.ControlLight: colorString = "buttonface"; break;

                case KnownColor.ControlDark: colorString = "buttonshadow"; break;

                case KnownColor.ControlText: colorString = "buttontext"; break;

                case KnownColor.ActiveCaptionText: colorString = "captiontext"; break;

                case KnownColor.GrayText: colorString = "graytext"; break;

                case KnownColor.HotTrack:
                case KnownColor.Highlight: colorString = "highlight"; break;

                case KnownColor.MenuHighlight:
                case KnownColor.HighlightText: colorString = "highlighttext"; break;

                case KnownColor.InactiveBorder: colorString = "inactiveborder"; break;

                case KnownColor.GradientInactiveCaption:
                case KnownColor.InactiveCaption: colorString = "inactivecaption"; break;

                case KnownColor.InactiveCaptionText: colorString = "inactivecaptiontext"; break;

                case KnownColor.Info: colorString = "infobackground"; break;

                case KnownColor.InfoText: colorString = "infotext"; break;

                case KnownColor.MenuBar:
                case KnownColor.Menu: colorString = "menu"; break;

                case KnownColor.MenuText: colorString = "menutext"; break;

                case KnownColor.ScrollBar: colorString = "scrollbar"; break;

                case KnownColor.ControlDarkDark: colorString = "threeddarkshadow"; break;

                case KnownColor.ControlLightLight: colorString = "buttonhighlight"; break;

                case KnownColor.Window: colorString = "window"; break;

                case KnownColor.WindowFrame: colorString = "windowframe"; break;

                case KnownColor.WindowText: colorString = "windowtext"; break;

                default: flag = true; break;
                }
            }
            if (flag)
            {
                if (c.IsNamedColor)
                {
                    if (c == Color.LightGray)
                    {
                        // special case due to mismatch between Html and enum spelling
                        colorString = "LightGrey";
                    }
                    else
                    {
                        colorString = c.Name;
                    }
                }
                else
                {
                    colorString = "#" + c.R.ToString("X2", null) +
                                  c.G.ToString("X2", null) +
                                  c.B.ToString("X2", null);
                }
            }
            return(colorString);
        }