示例#1
0
文件: Color.cs 项目: xivk/Mapsui
        /// <summary>
        /// Converts a string in Mapbox GL format to a Mapsui Color
        ///
        /// This function assumes, that alpha is a float in range from 0.0 to 1.0.
        /// It converts this float in Maspui Color alpha without rounding.
        /// The following colors could be converted:
        /// - Named colors with known Html names
        /// - Colors as Html color values with leading '#' and 6 or 3 numbers
        /// - Function rgb(r,g,b) with values for red, green and blue
        /// - Function rgba(r,g,b,a) with values for red, green, blue and alpha. Here alpha is between 0.0 and 1.0 like opacity.
        /// - Function hsl(h,s,l) with values hue (0.0 to 360.0), saturation (0.0% - 100.0%) and lightness (0.0% - 100.0%)
        /// - Function hsla(h,s,l,a) with values hue (0.0 to 360.0), saturation (0.0% - 100.0%), lightness (0.0% - 100.0%) and alpha. Here alpha is between 0.0 and 1.0 like opacity.
        /// </summary>
        /// <param name="from">String with HTML color representation or function like rgb() or hsl()</param>
        /// <returns>Converted Mapsui Color</returns>
        public static Color FromString(string from)
        {
            Color result = null;

            from = from.Trim().ToLower();

            // Check, if it is a known color
            if (KnownColors.ContainsKey(from))
            {
                from = KnownColors[from];
            }

            if (from.StartsWith("#"))
            {
                if (from.Length == 7)
                {
                    var color = int.Parse(from.Substring(1), NumberStyles.AllowHexSpecifier,
                                          CultureInfo.InvariantCulture);
                    result = new Color(color >> 16 & 0xFF, color >> 8 & 0xFF, color & 0xFF);
                }
                else if (from.Length == 4)
                {
                    var color = int.Parse(from.Substring(1), NumberStyles.AllowHexSpecifier,
                                          CultureInfo.InvariantCulture);
                    var r = (color >> 8 & 0xF) * 16 + (color >> 8 & 0xF);
                    var g = (color >> 4 & 0xF) * 16 + (color >> 4 & 0xF);
                    var b = (color & 0xF) * 16 + (color & 0xF);
                    result = new Color(r, g, b);
                }
            }
            else if (from.StartsWith("rgba"))
            {
                var split = from.Substring(from.IndexOf('(') + 1).TrimEnd(')').Split(',');

                if (split.Length != 4)
                {
                    throw new ArgumentException($"color {from} isn't a valid color");
                }

                var r = int.Parse(split[0].Trim(), CultureInfo.InvariantCulture);
                var g = int.Parse(split[1].Trim(), CultureInfo.InvariantCulture);
                var b = int.Parse(split[2].Trim(), CultureInfo.InvariantCulture);
                var a = float.Parse(split[3].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);

                result = new Color(r, g, b, (int)(a * 255));
            }
            else if (from.StartsWith("rgb"))
            {
                var split = from.Substring(from.IndexOf('(') + 1).TrimEnd(')').Split(',');

                if (split.Length != 3)
                {
                    throw new ArgumentException($"color {from} isn't a valid color");
                }

                var r = int.Parse(split[0].Trim(), CultureInfo.InvariantCulture);
                var g = int.Parse(split[1].Trim(), CultureInfo.InvariantCulture);
                var b = int.Parse(split[2].Trim(), CultureInfo.InvariantCulture);

                result = new Color(r, g, b);
            }
            else if (from.StartsWith("hsla"))
            {
                var split = from.Substring(from.IndexOf('(') + 1).TrimEnd(')').Split(',');

                if (split.Length != 4)
                {
                    throw new ArgumentException($"color {from} isn't a valid color");
                }

                var h = float.Parse(split[0].Trim(), CultureInfo.InvariantCulture);
                var s = float.Parse(split[1].Trim().Replace("%", ""), CultureInfo.InvariantCulture);
                var l = float.Parse(split[2].Trim().Replace("%", ""), CultureInfo.InvariantCulture);
                var a = float.Parse(split[3].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);

                result = FromHsl(h / 360.0f, s / 100.0f, l / 100.0f, (int)(a * 255));
            }
            else if (from.StartsWith("hsl"))
            {
                var split = from.Substring(from.IndexOf('(') + 1).TrimEnd(')').Split(',');

                if (split.Length != 3)
                {
                    throw new ArgumentException($"color {from} isn't a valid color");
                }

                var h = float.Parse(split[0].Trim(), CultureInfo.InvariantCulture);
                var s = float.Parse(split[1].Trim().Replace("%", ""), CultureInfo.InvariantCulture);
                var l = float.Parse(split[2].Trim().Replace("%", ""), CultureInfo.InvariantCulture);

                result = FromHsl(h / 360.0f, s / 100.0f, l / 100.0f);
            }

            return(result);
        }