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); }
private bool Equals(ColorRgb other) { return(this._r == other._r && this._g == other._g && this._b == other._b); }
public string ToWebColorString() { return(ColorRgb.ToWebColorString(this._r, this._g, this._b)); }
public ColorRgb(uint rgb) { ColorRgb.GetRgbBytes(rgb, out this._r, out this._g, out this._b); }
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); }