Exemplo n.º 1
0
        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,
            });
Exemplo n.º 2
0
        public void TryParse()
        {
            foreach (HtmlColorSample sample in HtmlColorSamples.All)
            {
                Assert.AreEqual(true, RgbColor.TryParse(sample.Hex, out RgbColor color1));
                Assert.AreEqual(sample.Rgb.Css, color1.ToCss(), sample.Name);
            }

            foreach (HtmlColorSample sample in HtmlColorSamples.All)
            {
                Assert.AreEqual(false, RgbColor.TryParse(sample.Name, out RgbColor _));
            }
        }
Exemplo n.º 3
0
        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);
            }
        }