Esempio n. 1
0
        public static NamedColor ParseColor(string value)
        {
            string val = value.Trim();

            if (string.IsNullOrEmpty(val))
            {
                return(new NamedColor(value, Windows.UI.Colors.Transparent));
            }

            if (val.StartsWith("#"))
            {
                val = val.Replace("#", "");

                byte a   = 0xff; // default opaque.
                byte r   = 0x00;
                byte g   = 0x00;
                byte b   = 0x00;
                byte pos = 0;

                if (val.Length == 8)
                {
                    a   = System.Convert.ToByte(val.Substring(pos, 2), 16);
                    pos = 2;
                }

                if (val.Length >= pos + 2)
                {
                    r    = System.Convert.ToByte(val.Substring(pos, 2), 16);
                    pos += 2;
                }

                if (val.Length >= pos + 2)
                {
                    g    = System.Convert.ToByte(val.Substring(pos, 2), 16);
                    pos += 2;
                }

                if (val.Length >= pos + 2)
                {
                    b = System.Convert.ToByte(val.Substring(pos, 2), 16);
                }

                return(new NamedColor(value, Color.FromArgb(a, r, g, b)));
            }
            else
            {
                Colors global = Colors.Instance;
                return(global.FindColor(value));
            }
        }