Skip to content

Commit deb6cde

Browse files
committed
SixLabors#542: add test creating black-white patterns by gradients
1 parent 53088a6 commit deb6cde

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/ImageSharp.Drawing/Processing/Drawing/Brushes/LinearGradientBrush.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ public LinearGradientBrushApplicator(
169169
foreach (var colorStop in this.colorStops)
170170
{
171171
localGradientTo = colorStop;
172-
if (colorStop.Ratio >= onCompleteGradient)
172+
173+
if (colorStop.Ratio > onCompleteGradient)
173174
{
174175
// we're done here, so break it!
175176
break;

tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs

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

4-
using System;
5-
4+
using System.Linq;
65
using SixLabors.ImageSharp.Processing;
76
using SixLabors.ImageSharp.Processing.Drawing.Brushes;
87

@@ -81,6 +80,54 @@ public void HorizontalLinearGradientBrushReturnsUnicolorColumns()
8180
}
8281
}
8382

83+
[Theory]
84+
[InlineData(new[] { 0.5f })]
85+
[InlineData(new[] { 0.2f, 0.4f, 0.6f, 0.8f })]
86+
[InlineData(new[] { 0.1f, 0.3f, 0.6f })]
87+
public void LinearGradientsWithDoubledStopsProduceDashedPatterns(
88+
float[] pattern)
89+
{
90+
int width = 20;
91+
int height = 10;
92+
93+
// ensure the input data is valid
94+
Assert.True(pattern.Length > 0);
95+
96+
// create the input pattern: 0, followed by each of the arguments twice, followed by 1.0 - toggling black and white.
97+
LinearGradientBrush<Rgba32>.ColorStop[] colorStops =
98+
Enumerable.Repeat(new LinearGradientBrush<Rgba32>.ColorStop(0, Rgba32.Black), 1)
99+
.Concat(
100+
pattern
101+
.SelectMany((f, index) => new[]
102+
{
103+
new LinearGradientBrush<Rgba32>.ColorStop(f, index % 2 == 0 ? Rgba32.Black : Rgba32.White),
104+
new LinearGradientBrush<Rgba32>.ColorStop(f, index % 2 == 0 ? Rgba32.White : Rgba32.Black)
105+
}))
106+
.Concat(Enumerable.Repeat(new LinearGradientBrush<Rgba32>.ColorStop(1, pattern.Length % 2 == 0 ? Rgba32.Black : Rgba32.White), 1))
107+
.ToArray();
108+
109+
string path = TestEnvironment.CreateOutputDirectory("Fill", "LinearGradientBrush");
110+
using (var image = new Image<Rgba32>(width, height))
111+
{
112+
LinearGradientBrush<Rgba32> unicolorLinearGradientBrush =
113+
new LinearGradientBrush<Rgba32>(
114+
new SixLabors.Primitives.Point(0, 0),
115+
new SixLabors.Primitives.Point(width, 0),
116+
colorStops);
117+
118+
image.Mutate(x => x.Fill(unicolorLinearGradientBrush));
119+
image.Save($"{path}/blackAndWhite{pattern[0]}.png");
120+
121+
using (PixelAccessor<Rgba32> sourcePixels = image.Lock())
122+
{
123+
// the result must be a black and white pattern, no other color should occur:
124+
Assert.All(
125+
Enumerable.Range(0, width).Select(i => sourcePixels[i, 0]),
126+
color => Assert.True(color == Rgba32.Black || color == Rgba32.White));
127+
}
128+
}
129+
}
130+
84131
[Fact]
85132
public void VerticalLinearGradientBrushReturnsUnicolorColumns()
86133
{
@@ -131,7 +178,6 @@ public void DiagonalLinearGradientBrushReturnsUnicolorColumns(
131178
int startX, int startY, int endX, int endY)
132179
{
133180
int size = 500;
134-
int lastIndex = size - 1;
135181

136182
string path = TestEnvironment.CreateOutputDirectory("Fill", "LinearGradientBrush");
137183
using (var image = new Image<Rgba32>(size, size))

0 commit comments

Comments
 (0)