static Optional <Color> TryParseColorHex111(string hex) { return (ScalarParser.ParseHex(hex.Substring(0, 1)).SelectMany(r => ScalarParser.ParseHex(hex.Substring(1, 1)).SelectMany(g => ScalarParser.ParseHex(hex.Substring(2, 1)).Select(b => new Color(r, g, b, 1))))); }
static Optional <Color> TryParseColorHex2222(string hex) { return (ScalarParser.ParseHex(hex.Substring(0, 2)).SelectMany(r => ScalarParser.ParseHex(hex.Substring(2, 2)).SelectMany(g => ScalarParser.ParseHex(hex.Substring(4, 2)).SelectMany(b => ScalarParser.ParseHex(hex.Substring(6, 2)).Select(a => new Color(r, g, b, a)))))); }
// Percentages public static Parsed <Percentages> TryParsePercentages(this string str) { if (!str.EndsWith("%")) { return(ScalarParser.TryParseDouble(str).Select(d => new Percentages(d))); } double value; if (double.TryParse(str.StripSuffix("%"), out value)) { return(Parsed.Success(new Percentages(value), str)); } return(Parsed.Failure <Percentages>(str)); }
public static Parsed <Color> TryParseColor(string str) { if (str.StartsWith("#")) { return new Parsed <Color> { String = str, Value = TryParseColorHex(str.AfterFirst("#")), } } ; var vector = str.Split(",").Select(v => v.Trim()).ToArray(); switch (vector.Length) { case 3: return(new Parsed <Color> { String = str, Value = ScalarParser.TryParseFloat(vector[0]).Value.SelectMany(r => ScalarParser.TryParseFloat(vector[1]).Value.SelectMany(g => ScalarParser.TryParseFloat(vector[2]).Value.Select(b => new Color(r, g, b, 1)))) }); case 4: return(new Parsed <Color> { String = str, Value = ScalarParser.TryParseFloat(vector[0]).Value.SelectMany(r => ScalarParser.TryParseFloat(vector[1]).Value.SelectMany(g => ScalarParser.TryParseFloat(vector[2]).Value.SelectMany(b => ScalarParser.TryParseFloat(vector[3]).Value.Select(a => new Color(r, g, b, a))))) }); } return(Parsed.Failure <Color>(str)); }