Exemplo n.º 1
0
        private GradientLine GetGradientLine(SKRectI boxBounds, double angleDegrees)
        {
            // Calculation
            // https://medium.com/@patrickbrosset/do-you-really-understand-css-linear-gradients-631d9a895caf

            var angleRadians = GradientMath.ToRadians(GradientMath.FromDegrees(angleDegrees));

            var lineLength =
                Math.Abs(boxBounds.Width * Math.Sin(angleRadians)) +
                Math.Abs(boxBounds.Height * Math.Cos(angleRadians));

            var center = new SKPoint(boxBounds.MidX, boxBounds.MidY);

            var yDiff = (float)(Math.Sin(angleRadians - Math.PI / 2) * lineLength / 2);
            var xDiff = (float)(Math.Cos(angleRadians - Math.PI / 2) * lineLength / 2);

            return(new GradientLine
            {
                Start = new SKPoint(
                    center.X - xDiff,
                    center.Y - yDiff),
                End = new SKPoint(
                    center.X + xDiff,
                    center.Y + yDiff),
                Length = lineLength,
                Angle = angleRadians
            });
        }
Exemplo n.º 2
0
        internal bool TryConvertTurnToAngle(string token, out double angle)
        {
            if (token.TryExtractNumber("turn", out var turn))
            {
                angle = GradientMath.FromDegrees(360 * turn);
                return(true);
            }

            angle = 0;
            return(false);
        }
Exemplo n.º 3
0
        internal bool TryConvertDegreeToAngle(string token, out double angle)
        {
            if (token.TryExtractNumber("deg", out var degrees))
            {
                angle = GradientMath.FromDegrees(degrees);
                return(true);
            }

            // For "0" deg is optional
            if (token.Equals("0", StringComparison.OrdinalIgnoreCase))
            {
                angle = GradientMath.FromDegrees(0);
                return(true);
            }

            angle = 0;
            return(false);
        }
Exemplo n.º 4
0
        private string GetGradient(Gradient gradient)
        {
            if (gradient is LinearGradient linear)
            {
                var type  = linear.IsRepeating ? "repeating-linear-gradient" : "linear-gradient";
                var angle = $"{GradientMath.FromDegrees(linear.Angle)}deg";

                return($"{type}({angle}, {GetColors(linear)})");
            }

            if (gradient is RadialGradient radial)
            {
                var type = radial.IsRepeating ? "repeating-radial-gradient" : "radial-gradient";

                return($"{type}({GetShapeAndSize(radial)} {GetPosition(radial)}, {GetColors(radial)})");
            }

            return(string.Empty);
        }