Esempio n. 1
0
        public override string ToString()
        {
            var sb = StringBuilderCache.Aquire();

            sb.Append("linear-gradient(");

            if (Angle != null)
            {
                sb.Append(Angle.Value);
                sb.Append("deg");
            }
            else if (Direction != default)
            {
                sb.Append("to ");

                sb.Append(LinearGradientDirectionHelper.Canonicalize(Direction));
            }

            foreach (var stop in Stops)
            {
                sb.Append(", ");

                sb.Append(stop.Color.ToString());

                if (stop.Position is double position)
                {
                    sb.Append(' ');
                    sb.Append(position.ToString("0.##%"));
                }
            }

            sb.Append(')');

            return(StringBuilderCache.ExtractAndRelease(sb));
        }
Esempio n. 2
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()));
        }