示例#1
0
        public static ColorRgb ParseWebColor(string webcolor)
        {
            var c = ColorRgb.TryParseWebColor(webcolor);

            if (!c.HasValue)
            {
                string s = string.Format("Failed to parse color string \"{0}\"", webcolor);
                throw new System.FormatException(s);
            }

            return(c.Value);
        }
示例#2
0
 private bool Equals(ColorRgb other)
 {
     return(this._r == other._r && this._g == other._g && this._b == other._b);
 }
示例#3
0
 public string ToWebColorString()
 {
     return(ColorRgb.ToWebColorString(this._r, this._g, this._b));
 }
示例#4
0
 public ColorRgb(uint rgb)
 {
     ColorRgb.GetRgbBytes(rgb, out this._r, out this._g, out this._b);
 }
示例#5
0
        public static ColorRgb?TryParseWebColor(string webcolor)
        {
            // fail if string is null
            if (webcolor == null)
            {
                return(null);
            }

            // fail if string is empty
            if (webcolor.Length < 1)
            {
                return(null);
            }

            // clean any leading or trailing whitespace
            webcolor = webcolor.Trim();

            // fail if string is empty
            if (webcolor.Length < 1)
            {
                return(null);
            }

            // strip leading # if it is there
            while (webcolor.StartsWith("#"))
            {
                webcolor = webcolor.Substring(1);
            }

            // clean any leading or trailing whitespace
            webcolor = webcolor.Trim();

            // fail if string is empty
            if (webcolor.Length < 1)
            {
                return(null);
            }

            // fail if string doesn't have exactly 6 digits
            if (webcolor.Length != 6)
            {
                return(null);
            }

            int  current_color;
            bool result = int.TryParse(webcolor, NumberStyles.HexNumber, null, out current_color);

            if (!result)
            {
                // fail if parsing didn't work
                return(null);
            }

            // at this point parsing worked

            // the integer value is converted directly to an rgb value

            var the_color = new ColorRgb(current_color);

            return(the_color);
        }