From ff023b833e3dee44c6bd01b6b17099afc397d435 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 8 Nov 2019 17:10:31 +1100 Subject: [PATCH] Fix #1044 --- .../Processing/GradientBrush.cs | 8 ++-- src/ImageSharp/ImageSharp.csproj | 4 +- .../Drawing/FillLinearGradientBrushTests.cs | 44 ++++++++++++++++++- tests/Images/External | 2 +- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp.Drawing/Processing/GradientBrush.cs b/src/ImageSharp.Drawing/Processing/GradientBrush.cs index 044f4e2ee8..69659431d0 100644 --- a/src/ImageSharp.Drawing/Processing/GradientBrush.cs +++ b/src/ImageSharp.Drawing/Processing/GradientBrush.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; - +using System.Numerics; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -120,10 +120,8 @@ protected GradientBrushApplicator( float onLocalGradient = (positionOnCompleteGradient - from.Ratio) / (to.Ratio - from.Ratio); // TODO: this should be changeble for different gradienting functions. - // TODO: Why not use Blender property? - return PixelOperations - .Instance.GetPixelBlender(PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.SrcOver) - .Blend(from.Color.ToPixel(), to.Color.ToPixel(), onLocalGradient); + // TODO: Is the comment above still relevent? + return new Color(Vector4.Lerp((Vector4)from.Color, (Vector4)to.Color, onLocalGradient)).ToPixel(); } } } diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index d9e9a8d219..86b0848663 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -1,4 +1,4 @@ - + @@ -119,7 +119,7 @@ - + diff --git a/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs b/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs index 361e7e70d1..8aae342cba 100644 --- a/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; @@ -16,6 +16,8 @@ namespace SixLabors.ImageSharp.Tests.Drawing { using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; + using SixLabors.Primitives; + using SixLabors.Shapes; [GroupOutput("Drawing/GradientBrushes")] public class FillLinearGradientBrushTests @@ -392,5 +394,43 @@ public void MultiplePointGradients( false, false); } + + [Theory] + [WithBlankImages(200, 200, PixelTypes.Rgba32)] + public void GradientsWithTransparencyOnExistingBackground(TestImageProvider provider) + where TPixel : struct, IPixel + { + provider.VerifyOperation( + image => + { + image.Mutate(i => i.Fill(Color.Red)); + image.Mutate(ApplyGloss); + + }); + + void ApplyGloss(IImageProcessingContext ctx) + { + Size size = ctx.GetCurrentSize(); + IPathCollection glossPath = BuildGloss(size.Width, size.Height); + var graphicsOptions = new GraphicsOptions(true) + { + ColorBlendingMode = PixelColorBlendingMode.Normal, + AlphaCompositionMode = PixelAlphaCompositionMode.SrcAtop + }; + var linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, size.Height / 2), GradientRepetitionMode.Repeat, new ColorStop(0, Color.White.WithAlpha(0.5f)), new ColorStop(1, Color.White.WithAlpha(0.25f))); + ctx.Fill(graphicsOptions, linearGradientBrush, glossPath); + } + + IPathCollection BuildGloss(int imageWidth, int imageHeight) + { + var pathBuilder = new PathBuilder(); + pathBuilder.AddLine(new PointF(0, 0), new PointF(imageWidth, 0)); + pathBuilder.AddLine(new PointF(imageWidth, 0), new PointF(imageWidth, imageHeight * 0.4f)); + pathBuilder.AddBezier(new PointF(imageWidth, imageHeight * 0.4f), new PointF(imageWidth / 2, imageHeight * 0.6f), new PointF(0, imageHeight * 0.4f)); + pathBuilder.CloseFigure(); + return new PathCollection(pathBuilder.Build()); + } + } + } -} \ No newline at end of file +} diff --git a/tests/Images/External b/tests/Images/External index 563ec6f777..f0c4033667 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit 563ec6f7774734ba39924174c8961705a1ea6fa2 +Subproject commit f0c4033667bd23ad9dde82ccb625c232d402ee05