Exemplo n.º 1
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string strValue)
            {
                return(ColorConverterCommon.ConvertFromString(strValue, culture ?? CultureInfo.CurrentCulture));
            }

            return(base.ConvertFrom(context, culture, value));
        }
Exemplo n.º 2
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string strValue = value as string;

            if (strValue != null)
            {
                return(ColorConverterCommon.ConvertFromString(strValue, culture));
            }
            return(base.ConvertFrom(context, culture, value));
        }
Exemplo n.º 3
0
        public static Color FromHtml(string htmlColor)
        {
            if (string.IsNullOrEmpty(htmlColor))
            {
                return(Color.Empty);
            }

            switch (htmlColor.ToLowerInvariant())
            {
            case "buttonface":
            case "threedface":
                return(SystemColors.Control);

            case "buttonhighlight":
            case "threedlightshadow":
                return(SystemColors.ControlLightLight);

            case "buttonshadow":
                return(SystemColors.ControlDark);

            case "captiontext":
                return(SystemColors.ActiveCaptionText);

            case "threeddarkshadow":
                return(SystemColors.ControlDarkDark);

            case "threedhighlight":
                return(SystemColors.ControlLight);

            case "background":
                return(SystemColors.Desktop);

            case "buttontext":
                return(SystemColors.ControlText);

            case "infobackground":
                return(SystemColors.Info);

            // special case for Color.LightGray versus html's LightGrey (#340917)
            case "lightgrey":
                return(Color.LightGray);
            }

            if (htmlColor[0] == '#' && htmlColor.Length == 4)
            {
                char r = htmlColor[1], g = htmlColor[2], b = htmlColor[3];
                htmlColor = new string(new char[] { '#', r, r, g, g, b, b });
            }
#if NETCORE
            return(ColorConverterCommon.ConvertFromString(htmlColor, CultureInfo.CurrentCulture));
#else
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(Color));
            return((Color)converter.ConvertFromString(htmlColor));
#endif
        }
Exemplo n.º 4
0
        /// <summary>
        /// Translates an Html color representation to a GDI+ <see cref='Color'/>.
        /// </summary>
        public static Color FromHtml(string htmlColor)
        {
            Color c = Color.Empty;

            // empty color
            if ((htmlColor == null) || (htmlColor.Length == 0))
            {
                return(c);
            }

            // #RRGGBB or #RGB
            if ((htmlColor[0] == '#') &&
                ((htmlColor.Length == 7) || (htmlColor.Length == 4)))
            {
                if (htmlColor.Length == 7)
                {
                    c = Color.FromArgb(Convert.ToInt32(htmlColor.Substring(1, 2), 16),
                                       Convert.ToInt32(htmlColor.Substring(3, 2), 16),
                                       Convert.ToInt32(htmlColor.Substring(5, 2), 16));
                }
                else
                {
                    string r = char.ToString(htmlColor[1]);
                    string g = char.ToString(htmlColor[2]);
                    string b = char.ToString(htmlColor[3]);

                    c = Color.FromArgb(Convert.ToInt32(r + r, 16),
                                       Convert.ToInt32(g + g, 16),
                                       Convert.ToInt32(b + b, 16));
                }
            }

            // special case. Html requires LightGrey, but .NET uses LightGray
            if (c.IsEmpty && string.Equals(htmlColor, "LightGrey", StringComparison.OrdinalIgnoreCase))
            {
                c = Color.LightGray;
            }

            // System color
            if (c.IsEmpty)
            {
                if (s_htmlSysColorTable == null)
                {
                    InitializeHtmlSysColorTable();
                }

                s_htmlSysColorTable !.TryGetValue(htmlColor.ToLowerInvariant(), out c);
            }

            // resort to type converter which will handle named colors
            if (c.IsEmpty)
            {
                try
                {
                    c = ColorConverterCommon.ConvertFromString(htmlColor, CultureInfo.CurrentCulture);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException(ex.Message, nameof(htmlColor), ex);
                }
            }

            return(c);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Translates an Html color representation to a GDI+ <see cref='Color'/>.
        /// </summary>
        public static Color FromHtml(string htmlColor)
        {
            Color c = Color.Empty;

            // empty color
            if ((htmlColor == null) || (htmlColor.Length == 0))
            {
                return(c);
            }

            // #RRGGBB or #RGB
            if ((htmlColor[0] == '#') &&
                ((htmlColor.Length == 7) || (htmlColor.Length == 4)))
            {
                if (htmlColor.Length == 7)
                {
                    c = Color.FromArgb(Convert.ToInt32(htmlColor.Substring(1, 2), 16),
                                       Convert.ToInt32(htmlColor.Substring(3, 2), 16),
                                       Convert.ToInt32(htmlColor.Substring(5, 2), 16));
                }
                else
                {
                    string r = Char.ToString(htmlColor[1]);
                    string g = Char.ToString(htmlColor[2]);
                    string b = Char.ToString(htmlColor[3]);

                    c = Color.FromArgb(Convert.ToInt32(r + r, 16),
                                       Convert.ToInt32(g + g, 16),
                                       Convert.ToInt32(b + b, 16));
                }
            }

            // special case. Html requires LightGrey, but .NET uses LightGray
            if (c.IsEmpty && String.Equals(htmlColor, "LightGrey", StringComparison.OrdinalIgnoreCase))
            {
                c = Color.LightGray;
            }

            // System color
            if (c.IsEmpty)
            {
                if (s_htmlSysColorTable == null)
                {
                    InitializeHtmlSysColorTable();
                }

                object o = s_htmlSysColorTable[htmlColor.ToLower(CultureInfo.InvariantCulture)];
                if (o != null)
                {
                    c = (Color)o;
                }
            }

            // resort to type converter which will handle named colors
            if (c.IsEmpty)
            {
                c = ColorConverterCommon.ConvertFromString(htmlColor, CultureInfo.CurrentCulture);
            }

            return(c);
        }