private Image getPlanetSurface()
        {
            int startx = rand.Next(0, 94544);
            int starty = rand.Next(0, 94544);

            Color[,] colors = new Color[800, 600];
            NoiseMap mapr = Utils.getNoiseMap(startx, starty, 12, 12, 3.1, 0.9);

            for (int i = 0; i < 800; i++)
            {
                for (int j = 0; j < 600; j++)
                {
                    byte r = Utils.NormNoise(mapr.GetValue(i, j));
                    colors[i, j] = new Color(r, r, r);
                }
            }
            GradientBuilder grad = new GradientBuilder();
            Color           grey = new Color(150, 150, 150);

            grad.AddGradient(new Gradient(0, grey));
            grad.AddGradient(new Gradient(255, Color.White));
            grad.PrepareGradients();
            grad.SourceImage = new Image(colors);
            return(grad.Render());
        }
Пример #2
0
 public static IGradientSource ToSource(this GradientBuilder builder)
 {
     return(new GradientCollection
     {
         Gradients = new GradientElements <Gradient>(builder.Build())
     });
 }
 public override void AddGradient(GradientBuilder builder)
 {
     builder.AddRadialGradient(
         new Point(0.5, 0.5),
         RadialGradientShape.Circle,
         RadialGradientSize.ClosestSide);
 }
Пример #4
0
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var isRepeating    = reader.Read().Trim() == CssToken.RepeatingRadialGradient;
            var token          = reader.ReadNext().Trim();
            var internalReader = new CssReader(token, ' ');

            var(hasShape, shape)     = GetShape(internalReader);
            var(hasStretch, stretch) = GetStretch(internalReader);
            var(hasRadius, radius)   = GeRadius(internalReader, shape);
            var(hasPos, position)    = GetPosition(internalReader);

            builder.UseBuilder(new RadialGradientBuilder
            {
                Center      = position,
                Shape       = shape,
                Stretch     = stretch,
                Radius      = radius,
                IsRepeating = isRepeating
            });

            if (!hasShape && !hasStretch && !hasRadius && !hasPos)
            {
                reader.Rollback();
            }
        }
Пример #5
0
        private Image getPlanetSurface()
        {
            int startx = rand.Next(0, 94544);
            int starty = rand.Next(0, 94544);

            Color[,] colors = new Color[800, 600];
            NoiseMap mapr = Utils.getNoiseMap(startx, starty, 6, 6, 2.7, 0.8);

            for (int i = 0; i < 800; i++)
            {
                for (int j = 0; j < 600; j++)
                {
                    byte r = Utils.NormNoise(mapr.GetValue(i, j));
                    colors[i, j] = new Color(r, r, r);
                }
            }
            GradientBuilder grad = new GradientBuilder();

            grad.AddGradient(new Gradient(0, Color.Gray));
            grad.AddGradient(new Gradient(20, Color.OrangeRed));
            grad.AddGradient(new Gradient(225, Color.Orange));
            grad.AddGradient(new Gradient(255, Color.LightGray));
            grad.PrepareGradients();
            grad.SourceImage = new Image(colors);
            return(grad.Render());
        }
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var isRepeating    = reader.Read().Trim() == CssToken.RepeatingRadialGradient;
            var token          = reader.ReadNext().Trim();
            var internalReader = new CssReader(token, ' ');

            var flags = None;

            var(hasShape, shape)   = GetShape(internalReader);
            var(hasSize, size)     = GetSize(internalReader);
            var(hasRadius, radius) = GeRadius(internalReader, shape, ref flags);
            var(hasPos, position)  = GetPosition(internalReader, ref flags);

            builder.UseBuilder(new RadialGradientBuilder
            {
                Center      = position,
                Shape       = shape,
                Size        = size,
                RadiusX     = radius.Width,
                RadiusY     = radius.Height,
                Flags       = flags,
                IsRepeating = isRepeating
            });

            if (!hasShape && !hasSize && !hasRadius && !hasPos)
            {
                reader.Rollback();
            }
        }
Пример #7
0
        public static T Source <T>(this T control, Action <GradientBuilder> build) where T : IGradientControl
        {
            var builder = new GradientBuilder();

            build.Invoke(builder);
            control.GradientSource = builder.BuildSource();

            return(control);
        }
        public void AddCssGradient_EmptyCss_NothingBuilt(string css)
        {
            // Arrange & Act
            var gradients = new GradientBuilder()
                            .AddCssGradient(css)
                            .Build();

            // Assert
            gradients.Should().BeEmpty();
        }
        public void AddCssGradient_InvalidCss_InvalidOperationException(string css)
        {
            // Arrange
            var builder = new GradientBuilder().AddCssGradient(css);

            // Act
            Action action = () => builder.Build();

            // Assert
            action.Should().Throw <InvalidOperationException>();
        }
        public void Parse_HslAndHsla_WithDoubleValues_NoExceptionsThrown(string color)
        {
            // Arrange
            var reader     = new CssReader(color);
            var builder    = new GradientBuilder();
            var definition = new ColorChannelDefinition();

            // Act
            Action action = () => definition.Parse(reader, builder);

            // Assert
            action.Should().NotThrow <Exception>();
        }
Пример #11
0
        public void UseStaticParseMethod_CssParsedAsGradients()
        {
            // Arrange
            var css      = "radial-gradient(blue, yellow)";
            var expected = new GradientBuilder()
                           .AddRadialGradient(g => g.AddStops(Color.Blue, Color.Yellow))
                           .Build().First();

            // Act
            var source = CssGradientSource.Parse(css);

            // Assert
            AssertSourceHasExpected(source, expected);
        }
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var color = (Color)ColorConverter.ConvertFromInvariantString(GetColorString(reader));

            if (TryConvertPercentToOffset(reader.ReadNext(), out var offset))
            {
                builder.AddStop(color, offset);
            }
            else
            {
                builder.AddStop(color);
                reader.Rollback();
            }
        }
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var isRepeating = reader.Read().Trim() == CssToken.RepeatingRadialGradient;

            var token          = reader.ReadNext().Trim();
            var internalReader = new CssReader(token, ' ');

            var shape     = GetShape(internalReader);
            var shapeSize = GetShapeSize(internalReader);

            var(position, flags) = GetPositionWithFlags(internalReader);

            builder.AddRadialGradient(position, shape, shapeSize, flags, isRepeating);
        }
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var parts = reader.Read().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var color = (Color)ColorConverter.ConvertFromInvariantString(parts[0]);

            if (parts.TryConvertOffsets(out var offsets))
            {
                builder.AddStops(color, offsets);
            }
            else
            {
                builder.AddStop(color);
            }
        }
        public void AddCssGradient_MultipleCssGradients_MultipleGradientsBuilt()
        {
            // Arrange & Act
            var gradients = new GradientBuilder()
                            .AddCssGradient("linear-gradient(black,white), radial-gradient(pink,brown)")
                            .Build();

            // Assert
            using (new AssertionScope())
            {
                gradients.Should().HaveCount(2);
                gradients[0].Should().BeOfType <RadialGradient>();
                gradients[1].Should().BeOfType <LinearGradient>();
            }
        }
Пример #16
0
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var parts   = reader.Read().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var color   = GetNamedColor(parts[0]);
            var offsets = GetOffsets(parts);

            if (offsets.Any())
            {
                builder.AddStops(color, offsets);
            }
            else
            {
                builder.AddStop(color);
            }
        }
        public void FluentApi_BuildSource_GenericGradientWithGradients()
        {
            // Act
            var source = new GradientBuilder()
                         .AddLinearGradient(g => g
                                            .Rotate(20)
                                            .AddStops(Colors.Red, Colors.Green, Colors.Blue))
                         .BuildSource();

            // Assert
            using (new AssertionScope())
            {
                source.Should().BeOfType <GenericGradientSource>();
                source.GetGradients().Should().HaveCount(1);
            }
        }
Пример #18
0
        public void SetStylesheetProperty_CssParsedAsGradients()
        {
            // Arrange
            var css      = "linear-gradient(red, green)";
            var expected = new GradientBuilder()
                           .AddLinearGradient(g => g.AddStops(Color.Red, Color.Green))
                           .Build().First();

            // Act
            var source = new CssGradientSource();

            source.Stylesheet = css;

            // Assert
            AssertSourceHasExpected(source, expected);
        }
Пример #19
0
        private void DrawBorder(RenderContext context)
        {
            var gradient = new GradientBuilder().AddCssGradient("linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%)").Build().First();

            gradient.InstantiateShader(_control);
            gradient.Measure(context.RenderRect.Width, context.RenderRect.Height);

            using var paint = new SKPaint
                  {
                      StrokeWidth = 10,
                      Style       = SKPaintStyle.Stroke,
                      Color       = SKColors.Yellow,
                      Shader      = gradient.Shader.Create(context)
                  };

            context.Canvas.DrawRect(context.RenderRect, paint);
        }
Пример #20
0
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var parts = reader.Read().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            var color = parts.Length > 0
                ? (Color)ColorConverter.ConvertFromInvariantString(parts[0])
                : Color.Black;

            if (parts.Length > 1 && TryConvertPercentToOffset(parts[1], out var offset))
            {
                builder.AddStop(color, offset);
            }
            else
            {
                builder.AddStop(color);
            }
        }
        public void AddCssGradient_ValidCss_GradientParsed()
        {
            // Arrange & Act
            var gradients = new GradientBuilder()
                            .AddCssGradient("linear-gradient(red, orange)")
                            .Build();

            // Assert
            using (new AssertionScope())
            {
                gradients.Should().HaveCount(1);
                gradients[0].Should().BeOfType <LinearGradient>();
                gradients[0].GetStops().Should().HaveCount(2);
                gradients[0].GetStops()[0].Color.Should().Be(Colors.Red);
                gradients[0].GetStops()[1].Color.Should().Be(Colors.Orange);
            }
        }
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var colorString = GetColorString(reader);
            var color       = (Color)ColorConverter.ConvertFromInvariantString(colorString);
            var parts       = reader.ReadNext().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var offsets     = GetOffsets(parts);

            if (offsets.Any())
            {
                builder.AddStops(color, offsets);
            }
            else
            {
                builder.AddStop(color);
                reader.Rollback();
            }
        }
Пример #23
0
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var direction = reader.ReadNext().Trim();

            if (TryConvertDegreeToAngle(direction, out var degreeToAngle))
            {
                builder.AddLinearGradient(degreeToAngle);
            }
            else if (TryConvertNamedDirectionToAngle(direction, out var directionToAngle))
            {
                builder.AddLinearGradient(directionToAngle);
            }
            else
            {
                builder.AddLinearGradient(0);
                reader.Rollback();
            }
        }
        public void FluentApi_BuildLinearNoOptions_ProperDefaultValues()
        {
            // Act
            var gradients = new GradientBuilder()
                            .AddLinearGradient()
                            .Build();

            // Assert
            using (new AssertionScope())
            {
                gradients.Should().HaveCount(1);
                gradients[0].Should().BeAssignableTo <LinearGradient>();

                var linear = (LinearGradient)gradients[0];
                linear.Angle.Should().Be(0);
                linear.IsRepeating.Should().BeFalse();
            }
        }
        public void AddGradient_AndStops_SingleGradientWithStops(GradientBuilderTestCase testCase)
        {
            // Arrange
            var builder = new GradientBuilder();

            // Act
            testCase.AddGradientTo(builder);
            builder.AddStop(Colors.White);
            builder.AddStop(Colors.Black);

            var gradients = builder.Build();

            // Assert
            using (new AssertionScope())
            {
                gradients.Should().HaveCount(1);
                gradients[0].GetStops().Should().HaveCount(2);
            }
        }
        public void AddOnlyStops_DefaultGradientWithStops()
        {
            // Arrange
            var builder = new GradientBuilder();

            // Act
            builder.AddStop(Colors.White);
            builder.AddStop(Colors.Black);

            var gradients = builder.Build();

            // Assert
            using (new AssertionScope())
            {
                gradients.Should().HaveCount(1);
                gradients[0].Should().BeOfType <LinearGradient>();
                gradients[0].GetStops().Should().HaveCount(2);
            }
        }
Пример #27
0
        public LinearViewModel()
        {
            LinearGradient = new GradientBuilder()
                             .AddLinearGradient(g => g
                                                .Rotate(45)
                                                .AddStops(Colors.Red, Colors.Orange, Colors.Yellow))
                             .BuildSource();

            Rainbow = new CssGradientSource("linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%)");

            Burst = new CssGradientSource("linear-gradient(201deg, rgba(148, 148, 148, 0.07) 0%, rgba(148, 148, 148, 0.07) 50%,rgba(83, 83, 83, 0.07) 50%, rgba(83, 83, 83, 0.07) 100%),linear-gradient(192deg, rgba(176, 176, 176, 0.08) 0%, rgba(176, 176, 176, 0.08) 50%,rgba(180, 180, 180, 0.08) 50%, rgba(180, 180, 180, 0.08) 100%),linear-gradient(48deg, rgba(185, 185, 185, 0.1) 0%, rgba(185, 185, 185, 0.1) 50%,rgba(243, 243, 243, 0.1) 50%, rgba(243, 243, 243, 0.1) 100%),linear-gradient(65deg, rgba(172, 172, 172, 0.09) 0%, rgba(172, 172, 172, 0.09) 50%,rgba(209, 209, 209, 0.09) 50%, rgba(209, 209, 209, 0.09) 100%),linear-gradient(4deg, rgba(224, 224, 224, 0.03) 0%, rgba(224, 224, 224, 0.03) 50%,rgba(49, 49, 49, 0.03) 50%, rgba(49, 49, 49, 0.03) 100%),linear-gradient(228deg, rgba(152, 152, 152, 0.03) 0%, rgba(152, 152, 152, 0.03) 50%,rgba(130, 130, 130, 0.03) 50%, rgba(130, 130, 130, 0.03) 100%),linear-gradient(163deg, rgba(170, 170, 170, 0.08) 0%, rgba(170, 170, 170, 0.08) 50%,rgba(232, 232, 232, 0.08) 50%, rgba(232, 232, 232, 0.08) 100%),linear-gradient(152deg, rgba(12, 12, 12, 0.05) 0%, rgba(12, 12, 12, 0.05) 50%,rgba(161, 161, 161, 0.05) 50%, rgba(161, 161, 161, 0.05) 100%),linear-gradient(302deg, rgba(48, 48, 48, 0.1) 0%, rgba(48, 48, 48, 0.1) 50%,rgba(195, 195, 195, 0.1) 50%, rgba(195, 195, 195, 0.1) 100%),linear-gradient(90deg, rgb(144, 14, 253),rgb(74, 115, 255))");

            Angular = new GradientBuilder()
                      .AddCssGradient("linear-gradient(142deg, rgba(250, 250, 250, 0.05) 0%, rgba(250, 250, 250, 0.05) 53%,rgba(64, 64, 64, 0.05) 53%, rgba(64, 64, 64, 0.05) 100%),linear-gradient(29deg, rgba(10, 10, 10, 0.05) 0%, rgba(10, 10, 10, 0.05) 27%,rgba(94, 94, 94, 0.05) 27%, rgba(94, 94, 94, 0.05) 100%),linear-gradient(118deg, rgba(4, 4, 4, 0.05) 0%, rgba(4, 4, 4, 0.05) 18%,rgba(188, 188, 188, 0.05) 18%, rgba(188, 188, 188, 0.05) 100%),linear-gradient(90deg, rgb(10, 143, 251),rgb(35, 61, 210))")
                      .BuildSource();

            Diamonds = new GradientBuilder()
                       .AddCssGradient("linear-gradient(45deg, rgba(22, 31, 43, 0.5) 0%, rgba(22, 31, 43, 0.5) 12.5%,rgba(53, 28, 54, 0.5) 12.5%, rgba(53, 28, 54, 0.5) 25%,rgba(83, 25, 65, 0.5) 25%, rgba(83, 25, 65, 0.5) 37.5%,rgba(114, 22, 76, 0.5) 37.5%, rgba(114, 22, 76, 0.5) 50%,rgba(144, 20, 86, 0.5) 50%, rgba(144, 20, 86, 0.5) 62.5%,rgba(175, 17, 97, 0.5) 62.5%, rgba(175, 17, 97, 0.5) 75%,rgba(205, 14, 108, 0.5) 75%, rgba(205, 14, 108, 0.5) 87.5%,rgba(236, 11, 119, 0.5) 87.5%, rgba(236, 11, 119, 0.5) 100%),linear-gradient(135deg, rgb(188, 0, 159) 0%, rgb(188, 0, 159) 12.5%,rgb(173, 4, 150) 12.5%, rgb(173, 4, 150) 25%,rgb(158, 7, 141) 25%, rgb(158, 7, 141) 37.5%,rgb(143, 11, 132) 37.5%, rgb(143, 11, 132) 50%,rgb(129, 15, 124) 50%, rgb(129, 15, 124) 62.5%,rgb(114, 19, 115) 62.5%, rgb(114, 19, 115) 75%,rgb(99, 22, 106) 75%, rgb(99, 22, 106) 87.5%,rgb(84, 26, 97) 87.5%, rgb(84, 26, 97) 100%)")
                       .BuildSource();
        }
Пример #28
0
        public void Parse(CssReader reader, GradientBuilder builder)
        {
            var repeating = reader.Read().Trim() == CssToken.RepeatingLinearGradient;
            var direction = reader.ReadNext().Trim();
            var angle     = 0d;

            var hasAngle = TryConvertDegreeToAngle(direction, out angle) ||
                           TryConvertTurnToAngle(direction, out angle) ||
                           TryConvertNamedDirectionToAngle(direction, out angle);

            if (hasAngle)
            {
                builder.AddLinearGradient(angle, repeating);
            }
            else
            {
                builder.AddLinearGradient(0, repeating);
                reader.Rollback();
            }
        }
        public void Parse_ValidGradientCss_ExpectedGradientReturned(string css, LinearGradient expectedGradient)
        {
            // Arrange
            var reader     = new CssReader(css);
            var builder    = new GradientBuilder();
            var definition = new LinearGradientDefinition();

            // Act
            definition.Parse(reader, builder);

            // Assert
            var result = builder.Build();

            using (new AssertionScope())
            {
                result.Should().HaveCount(1);
                var linearGradient = result[0] as LinearGradient;
                linearGradient.Should().NotBeNull();
                linearGradient.Should().BeEquivalentTo(expectedGradient);
            }
        }
        public void FluentApi_BuildRadialNoOptions_ProperDefaultValues()
        {
            // Act
            var gradients = new GradientBuilder()
                            .AddRadialGradient()
                            .Build();

            // Assert
            using (new AssertionScope())
            {
                gradients.Should().HaveCount(1);
                gradients[0].Should().BeAssignableTo <RadialGradient>();

                var radial = (RadialGradient)gradients[0];
                radial.Center.Should().BeEquivalentTo(Position.Prop(0.5, 0.5));
                radial.Radius.Should().BeEquivalentTo(Dimensions.Zero);
                radial.Shape.Should().Be(RadialGradientShape.Ellipse);
                radial.Stretch.Should().Be(RadialGradientStretch.FarthestCorner);
                radial.IsRepeating.Should().BeFalse();
            }
        }