private static Brightness ParseBrightness(string str) { var match = Regex.Match(str, @"^(?<Percentage>\d{1,3})%$"); if (match.Success) { return(Brightness.FromPercentage(Int32.Parse(match.Groups["Percentage"].Value))); } int value; if (Int32.TryParse(str, out value)) { return(Brightness.FromValue(value)); } throw new FormatException("Unrecognized brightness value."); }