public static ValueTask <FluidValue> ColorExtract(FluidValue input, FilterArguments arguments, TemplateContext context) { var value = input.ToStringValue(); RgbColor rgbColor; HslColor hslColor; if (HexColor.TryParse(value, out HexColor hexColor)) { rgbColor = (RgbColor)hexColor; hslColor = (HslColor)hexColor; } else if (RgbColor.TryParse(value, out rgbColor)) { hslColor = (HslColor)rgbColor; } else if (HslColor.TryParse(value, out hslColor)) { rgbColor = (RgbColor)hslColor; } else { return(NilValue.Empty); } return(arguments.At(0).ToStringValue() switch { "alpha" => new StringValue(rgbColor.A.ToString()), "red" => new StringValue(rgbColor.R.ToString()), "green" => new StringValue(rgbColor.G.ToString()), "blue" => new StringValue(rgbColor.B.ToString()), "hue" => new StringValue(hslColor.H.ToString()), "saturation" => new StringValue(Convert.ToInt32(hslColor.S * 100.0).ToString()), "lightness" => new StringValue(Convert.ToInt32(hslColor.L * 100.0).ToString()), _ => NilValue.Empty, });
public static ValueTask <FluidValue> ToHsl(FluidValue input, FilterArguments arguments, TemplateContext context) { var value = input.ToStringValue(); if (HexColor.TryParse(value, out HexColor hexColor)) { var hslColor = (HslColor)hexColor; return(new StringValue(hslColor.ToString())); } else if (RgbColor.TryParse(value, out RgbColor rgbColor)) { var hslColor = (HslColor)rgbColor; return(new StringValue(hslColor.ToString())); } else { return(NilValue.Empty); } }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { return(Task.FromResult(HexColor.TryParse(input, out var hc) ? TypeReaderResult.FromSuccess(hc) : TypeReaderResult.FromError(CommandError.ParseFailed, "input string didn't match hex color template"))); }