Ejemplo n.º 1
0
        public static LinearGradient Parse(ReadOnlySpan <char> text)
        {
            if (text.StartsWith("linear-gradient(".AsSpan()))
            {
                text = text.Slice(16, text.Length - 17);
            }

            if (text.Length == 0)
            {
                throw new ArgumentException("May not be empty", nameof(text));
            }

            double?angle = null;
            LinearGradientDirection direction = default;

            if (char.IsDigit(text[0]) || text[0] == '-')
            {
                angle = ReadAngle(text, out int read);

                text = text.Slice(read);
            }
            else if (LinearGradientDirectionHelper.TryParse(text, out direction, out int read))
            {
                text = text.Slice(read);
            }

            var colorStops = new List <ColorStop>();

            while (text.Length > 0)
            {
                if (text[0] == ',')
                {
                    text = text.Slice(1);
                }

                if (text.TryReadWhitespace(out int read))
                {
                    text = text.Slice(read);
                }

                if (text[0] == ',')
                {
                    text = text.Slice(1);
                }

                var colorStop = ColorStop.Read(text, out read);

                text = text.Slice(read);

                colorStops.Add(colorStop);
            }

            // TODO: Set the default stops

            // // [ <angle> | to [top | bottom] || [left | right] ],]? <color-stop>[, <color-stop>]+);

            return(new LinearGradient(direction, angle, colorStops.ToArray()));
        }
Ejemplo n.º 2
0
        public static bool TryParse(ReadOnlySpan <char> text, out LinearGradientDirection result, out int read)
        {
            read = 0;

            // NOTE: The legacy prefix did not include too

            if (text.StartsWith("to ".AsSpan()))
            {
                read += 3;

                text = text.Slice(3);
            }

            if (text.StartsWith("top".AsSpan()))
            {
                read += 3;

                text = text.Slice(3);

                if (text.StartsWith(" left".AsSpan()))
                {
                    read += 5;

                    result = TopLeft;
                }
                else if (text.StartsWith(" right".AsSpan()))
                {
                    read += 6;

                    result = TopRight;
                }
                else
                {
                    result = Top;
                }
            }
            else if (text.StartsWith("bottom".AsSpan()))
            {
                read += 6;

                text = text.Slice(6);

                if (text.StartsWith(" left".AsSpan()))
                {
                    read += 5;

                    result = Left;
                }
                else if (text.StartsWith(" right".AsSpan()))
                {
                    read += 6;

                    result = BottomRight;
                }
                else
                {
                    result = Bottom;
                }
            }
            else if (text.StartsWith("left".AsSpan()))
            {
                read += 4;

                result = Left;
            }
            else if (text.StartsWith("right".AsSpan()))
            {
                read += 5;

                result = Right;
            }
            else
            {
                result = default;

                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
 public static string Canonicalize(LinearGradientDirection value) => value switch
 {
Ejemplo n.º 4
0
 public LinearGradient(LinearGradientDirection direction, double?angle, ColorStop[] colorStops)
 {
     Direction = direction;
     Angle     = angle;
     Stops     = colorStops;
 }