Task <Optional <DiscordColor> > IArgumentConverter <DiscordColor> .ConvertAsync(string value, CommandContext ctx) { Match m = ColorRegexHex.Match(value); if (m.Success && int.TryParse(m.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int clr)) { return(Task.FromResult(Optional.FromValue <DiscordColor>(clr))); } m = ColorRegexRgb.Match(value); if (m.Success) { bool p1 = byte.TryParse(m.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out byte r); bool p2 = byte.TryParse(m.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out byte g); bool p3 = byte.TryParse(m.Groups[3].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out byte b); if (!(p1 && p2 && p3)) { return(Task.FromResult(Optional.FromNoValue <DiscordColor>())); } return(Task.FromResult(Optional.FromValue(new DiscordColor(r, g, b)))); } if (ColorNames.ContainsKey(value.ToLower())) { return(Task.FromResult(Optional.FromValue(ColorNames[value.ToLower()]))); } return(Task.FromResult(Optional.FromNoValue <DiscordColor>())); }
public bool TryConvert(string value, CommandContext ctx, out DiscordColor result) { result = default; var m = ColorRegexHex.Match(value); if (m.Success && int.TryParse(m.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var clr)) { result = new DiscordColor(clr); return(true); } m = ColorRegexRgb.Match(value); if (m.Success) { var p1 = byte.TryParse(m.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var r); var p2 = byte.TryParse(m.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var g); var p3 = byte.TryParse(m.Groups[3].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var b); if (!(p1 && p2 && p3)) { return(false); } result = new DiscordColor(r, g, b); return(true); } return(false); }
Task <Optional <DiscordColor> > IArgumentConverter <DiscordColor> .ConvertAsync(string value, CommandContext ctx) { var m = ColorRegexHex.Match(value); if (m.Success && int.TryParse(m.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var clr)) { return(Task.FromResult(Optional.FromValue <DiscordColor>(clr))); } m = ColorRegexRgb.Match(value); if (m.Success) { var p1 = byte.TryParse(m.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var r); var p2 = byte.TryParse(m.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var g); var p3 = byte.TryParse(m.Groups[3].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var b); if (!(p1 && p2 && p3)) { return(Task.FromResult(Optional.FromNoValue <DiscordColor>())); } return(Task.FromResult(Optional.FromValue(new DiscordColor(r, g, b)))); } return(Task.FromResult(Optional.FromNoValue <DiscordColor>())); }