Skip to content

Commit 0dfa2f9

Browse files
PoyoJimBobSquarePants
authored andcommitted
Change existing gradient brushes to accept PointF (#865)
* Change existing gradient brushes to accept PointF * Change PositionOnGradient to accept float * Remove invalid assert.
1 parent a7f9a8e commit 0dfa2f9

File tree

6 files changed

+32
-36
lines changed

6 files changed

+32
-36
lines changed

src/ImageSharp.Drawing/Processing/EllipticGradientBrush{TPixel}.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ namespace SixLabors.ImageSharp.Processing
1818
public sealed class EllipticGradientBrush<TPixel> : GradientBrushBase<TPixel>
1919
where TPixel : struct, IPixel<TPixel>
2020
{
21-
private readonly Point center;
21+
private readonly PointF center;
2222

23-
private readonly Point referenceAxisEnd;
23+
private readonly PointF referenceAxisEnd;
2424

2525
private readonly float axisRatio;
2626

@@ -35,8 +35,8 @@ public sealed class EllipticGradientBrush<TPixel> : GradientBrushBase<TPixel>
3535
/// <param name="repetitionMode">Defines how the colors of the gradients are repeated.</param>
3636
/// <param name="colorStops">the color stops as defined in base class.</param>
3737
public EllipticGradientBrush(
38-
Point center,
39-
Point referenceAxisEnd,
38+
PointF center,
39+
PointF referenceAxisEnd,
4040
float axisRatio,
4141
GradientRepetitionMode repetitionMode,
4242
params ColorStop<TPixel>[] colorStops)
@@ -64,9 +64,9 @@ public override BrushApplicator<TPixel> CreateApplicator(
6464
/// <inheritdoc />
6565
private sealed class RadialGradientBrushApplicator : GradientBrushApplicatorBase
6666
{
67-
private readonly Point center;
67+
private readonly PointF center;
6868

69-
private readonly Point referenceAxisEnd;
69+
private readonly PointF referenceAxisEnd;
7070

7171
private readonly float axisRatio;
7272

@@ -99,8 +99,8 @@ private sealed class RadialGradientBrushApplicator : GradientBrushApplicatorBase
9999
public RadialGradientBrushApplicator(
100100
ImageFrame<TPixel> target,
101101
GraphicsOptions options,
102-
Point center,
103-
Point referenceAxisEnd,
102+
PointF center,
103+
PointF referenceAxisEnd,
104104
float axisRatio,
105105
ColorStop<TPixel>[] colorStops,
106106
GradientRepetitionMode repetitionMode)
@@ -129,7 +129,7 @@ public override void Dispose()
129129
}
130130

131131
/// <inheritdoc />
132-
protected override float PositionOnGradient(int xt, int yt)
132+
protected override float PositionOnGradient(float xt, float yt)
133133
{
134134
float x0 = xt - this.center.X;
135135
float y0 = yt - this.center.Y;

src/ImageSharp.Drawing/Processing/GradientBrushBase{TPixel}.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected GradientBrushApplicatorBase(
8181
{
8282
get
8383
{
84-
float positionOnCompleteGradient = this.PositionOnGradient(x, y);
84+
float positionOnCompleteGradient = this.PositionOnGradient(x + 0.5f, y + 0.5f);
8585

8686
switch (this.repetitionMode)
8787
{
@@ -137,18 +137,18 @@ protected GradientBrushApplicatorBase(
137137
}
138138

139139
/// <summary>
140-
/// calculates the position on the gradient for a given pixel.
140+
/// calculates the position on the gradient for a given point.
141141
/// This method is abstract as it's content depends on the shape of the gradient.
142142
/// </summary>
143-
/// <param name="x">The x coordinate of the pixel</param>
144-
/// <param name="y">The y coordinate of the pixel</param>
143+
/// <param name="x">The x coordinate of the point</param>
144+
/// <param name="y">The y coordinate of the point</param>
145145
/// <returns>
146-
/// The position the given pixel has on the gradient.
146+
/// The position the given point has on the gradient.
147147
/// The position is not bound to the [0..1] interval.
148148
/// Values outside of that interval may be treated differently,
149149
/// e.g. for the <see cref="GradientRepetitionMode" /> enum.
150150
/// </returns>
151-
protected abstract float PositionOnGradient(int x, int y);
151+
protected abstract float PositionOnGradient(float x, float y);
152152

153153
private (ColorStop<TPixel> from, ColorStop<TPixel> to) GetGradientSegment(
154154
float positionOnCompleteGradient)

src/ImageSharp.Drawing/Processing/LinearGradientBrush{TPixel}.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ namespace SixLabors.ImageSharp.Processing
1717
public sealed class LinearGradientBrush<TPixel> : GradientBrushBase<TPixel>
1818
where TPixel : struct, IPixel<TPixel>
1919
{
20-
private readonly Point p1;
20+
private readonly PointF p1;
2121

22-
private readonly Point p2;
22+
private readonly PointF p2;
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="LinearGradientBrush{TPixel}"/> class.
@@ -29,8 +29,8 @@ public sealed class LinearGradientBrush<TPixel> : GradientBrushBase<TPixel>
2929
/// <param name="repetitionMode">defines how colors are repeated.</param>
3030
/// <param name="colorStops"><inheritdoc /></param>
3131
public LinearGradientBrush(
32-
Point p1,
33-
Point p2,
32+
PointF p1,
33+
PointF p2,
3434
GradientRepetitionMode repetitionMode,
3535
params ColorStop<TPixel>[] colorStops)
3636
: base(repetitionMode, colorStops)
@@ -48,9 +48,9 @@ public override BrushApplicator<TPixel> CreateApplicator(ImageFrame<TPixel> sour
4848
/// </summary>
4949
private sealed class LinearGradientBrushApplicator : GradientBrushApplicatorBase
5050
{
51-
private readonly Point start;
51+
private readonly PointF start;
5252

53-
private readonly Point end;
53+
private readonly PointF end;
5454

5555
/// <summary>
5656
/// the vector along the gradient, x component
@@ -93,8 +93,8 @@ private sealed class LinearGradientBrushApplicator : GradientBrushApplicatorBase
9393
/// <param name="options">the graphics options</param>
9494
public LinearGradientBrushApplicator(
9595
ImageFrame<TPixel> source,
96-
Point start,
97-
Point end,
96+
PointF start,
97+
PointF end,
9898
ColorStop<TPixel>[] colorStops,
9999
GradientRepetitionMode repetitionMode,
100100
GraphicsOptions options)
@@ -116,15 +116,15 @@ public LinearGradientBrushApplicator(
116116
this.length = (float)Math.Sqrt(this.alongsSquared);
117117
}
118118

119-
protected override float PositionOnGradient(int x, int y)
119+
protected override float PositionOnGradient(float x, float y)
120120
{
121121
if (this.acrossX == 0)
122122
{
123-
return (x - this.start.X) / (float)(this.end.X - this.start.X);
123+
return (x - this.start.X) / (this.end.X - this.start.X);
124124
}
125125
else if (this.acrossY == 0)
126126
{
127-
return (y - this.start.Y) / (float)(this.end.Y - this.start.Y);
127+
return (y - this.start.Y) / (this.end.Y - this.start.Y);
128128
}
129129
else
130130
{

src/ImageSharp.Drawing/Processing/RadialGradientBrush{TPixel}.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Processing
1515
public sealed class RadialGradientBrush<TPixel> : GradientBrushBase<TPixel>
1616
where TPixel : struct, IPixel<TPixel>
1717
{
18-
private readonly Point center;
18+
private readonly PointF center;
1919

2020
private readonly float radius;
2121

@@ -25,7 +25,7 @@ public sealed class RadialGradientBrush<TPixel> : GradientBrushBase<TPixel>
2525
/// <param name="repetitionMode">Defines how the colors in the gradient are repeated.</param>
2626
/// <param name="colorStops">the color stops as defined in base class.</param>
2727
public RadialGradientBrush(
28-
Point center,
28+
PointF center,
2929
float radius,
3030
GradientRepetitionMode repetitionMode,
3131
params ColorStop<TPixel>[] colorStops)
@@ -51,7 +51,7 @@ public override BrushApplicator<TPixel> CreateApplicator(
5151
/// <inheritdoc />
5252
private sealed class RadialGradientBrushApplicator : GradientBrushApplicatorBase
5353
{
54-
private readonly Point center;
54+
private readonly PointF center;
5555

5656
private readonly float radius;
5757

@@ -67,7 +67,7 @@ private sealed class RadialGradientBrushApplicator : GradientBrushApplicatorBase
6767
public RadialGradientBrushApplicator(
6868
ImageFrame<TPixel> target,
6969
GraphicsOptions options,
70-
Point center,
70+
PointF center,
7171
float radius,
7272
ColorStop<TPixel>[] colorStops,
7373
GradientRepetitionMode repetitionMode)
@@ -89,7 +89,7 @@ public override void Dispose()
8989
/// <param name="x">The X coordinate of the target pixel.</param>
9090
/// <param name="y">The Y coordinate of the target pixel.</param>
9191
/// <returns>the position on the color gradient.</returns>
92-
protected override float PositionOnGradient(int x, int y)
92+
protected override float PositionOnGradient(float x, float y)
9393
{
9494
float distance = (float)Math.Sqrt(Math.Pow(this.center.X - x, 2) + Math.Pow(this.center.Y - y, 2));
9595
return distance / this.radius;

tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,6 @@ public void DiagonalReturnsCorrectImages<TPixel>(
275275
int verticalSign = startY == 0 ? 1 : -1;
276276
int horizontalSign = startX == 0 ? 1 : -1;
277277

278-
// check first and last pixel, these are known:
279-
Assert.Equal(red, image[startX, startY]);
280-
Assert.Equal(yellow, image[endX, endY]);
281-
282278
for (int i = 0; i < image.Height; i++)
283279
{
284280
// it's diagonal, so for any (a, a) on the gradient line, for all (a-x, b+x) - +/- depending on the diagonal direction - must be the same color)

tests/Images/External

Submodule External updated 49 files

0 commit comments

Comments
 (0)