Skip to content

Commit ff023b8

Browse files
Fix #1044
1 parent f7eed91 commit ff023b8

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

src/ImageSharp.Drawing/Processing/GradientBrush.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5-
5+
using System.Numerics;
66
using SixLabors.ImageSharp.PixelFormats;
77
using SixLabors.Primitives;
88

@@ -120,10 +120,8 @@ protected GradientBrushApplicator(
120120
float onLocalGradient = (positionOnCompleteGradient - from.Ratio) / (to.Ratio - from.Ratio);
121121

122122
// TODO: this should be changeble for different gradienting functions.
123-
// TODO: Why not use Blender property?
124-
return PixelOperations<TPixel>
125-
.Instance.GetPixelBlender(PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.SrcOver)
126-
.Blend(from.Color.ToPixel<TPixel>(), to.Color.ToPixel<TPixel>(), onLocalGradient);
123+
// TODO: Is the comment above still relevent?
124+
return new Color(Vector4.Lerp((Vector4)from.Color, (Vector4)to.Color, onLocalGradient)).ToPixel<TPixel>();
127125
}
128126
}
129127
}

src/ImageSharp/ImageSharp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33

44
<PropertyGroup>
@@ -119,7 +119,7 @@
119119
</ItemGroup>
120120

121121
<ItemGroup>
122-
<!--<InternalsVisibleTo Include="SixLabors.ImageSharp.Drawing" />-->
122+
<InternalsVisibleTo Include="SixLabors.ImageSharp.Drawing" />
123123
</ItemGroup>
124124

125125
<ItemGroup>

tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors and contributors.
1+
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -16,6 +16,8 @@ namespace SixLabors.ImageSharp.Tests.Drawing
1616
{
1717
using SixLabors.ImageSharp.Advanced;
1818
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
19+
using SixLabors.Primitives;
20+
using SixLabors.Shapes;
1921

2022
[GroupOutput("Drawing/GradientBrushes")]
2123
public class FillLinearGradientBrushTests
@@ -392,5 +394,43 @@ public void MultiplePointGradients<TPixel>(
392394
false,
393395
false);
394396
}
397+
398+
[Theory]
399+
[WithBlankImages(200, 200, PixelTypes.Rgba32)]
400+
public void GradientsWithTransparencyOnExistingBackground<TPixel>(TestImageProvider<TPixel> provider)
401+
where TPixel : struct, IPixel<TPixel>
402+
{
403+
provider.VerifyOperation(
404+
image =>
405+
{
406+
image.Mutate(i => i.Fill(Color.Red));
407+
image.Mutate(ApplyGloss);
408+
409+
});
410+
411+
void ApplyGloss(IImageProcessingContext ctx)
412+
{
413+
Size size = ctx.GetCurrentSize();
414+
IPathCollection glossPath = BuildGloss(size.Width, size.Height);
415+
var graphicsOptions = new GraphicsOptions(true)
416+
{
417+
ColorBlendingMode = PixelColorBlendingMode.Normal,
418+
AlphaCompositionMode = PixelAlphaCompositionMode.SrcAtop
419+
};
420+
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)));
421+
ctx.Fill(graphicsOptions, linearGradientBrush, glossPath);
422+
}
423+
424+
IPathCollection BuildGloss(int imageWidth, int imageHeight)
425+
{
426+
var pathBuilder = new PathBuilder();
427+
pathBuilder.AddLine(new PointF(0, 0), new PointF(imageWidth, 0));
428+
pathBuilder.AddLine(new PointF(imageWidth, 0), new PointF(imageWidth, imageHeight * 0.4f));
429+
pathBuilder.AddBezier(new PointF(imageWidth, imageHeight * 0.4f), new PointF(imageWidth / 2, imageHeight * 0.6f), new PointF(0, imageHeight * 0.4f));
430+
pathBuilder.CloseFigure();
431+
return new PathCollection(pathBuilder.Build());
432+
}
433+
}
434+
395435
}
396-
}
436+
}

0 commit comments

Comments
 (0)