forked from SixLabors/ImageSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearGradientBrush.cs
More file actions
211 lines (179 loc) · 8.29 KB
/
Copy pathLinearGradientBrush.cs
File metadata and controls
211 lines (179 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.PixelFormats.PixelBlenders;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
{
/// <summary>
/// Provides an implementation of a brush for painting gradients within areas.
/// Supported right now:
/// - a set of colors in relative distances to each other.
/// - two points to gradient along.
/// </summary>
/// <typeparam name="TPixel">The pixel format</typeparam>
public class LinearGradientBrush<TPixel> : IBrush<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private readonly Point p1;
private readonly Point p2;
private readonly Tuple<float, TPixel>[] keyColors;
/// <summary>
/// Initializes a new instance of the <see cref="LinearGradientBrush{TPixel}"/> class.
/// </summary>
/// <param name="p1">Start point</param>
/// <param name="p2">End point</param>
/// <param name="keyColors">a set of color keys and where they are. The double must be in range [0..1] and is relative between p1 and p2.</param>
public LinearGradientBrush(Point p1, Point p2, params Tuple<float, TPixel>[] keyColors)
{
this.p1 = p1;
this.p2 = p2;
this.keyColors = keyColors;
}
/// <inheritdoc />
public BrushApplicator<TPixel> CreateApplicator(ImageFrame<TPixel> source, RectangleF region, GraphicsOptions options)
=> new LinearGradientBrushApplicator(source, this.p1, this.p2, this.keyColors, region, options);
/// <summary>
/// The linear gradient brush applicator.
/// </summary>
private class LinearGradientBrushApplicator : BrushApplicator<TPixel>
{
private readonly Point start;
private readonly Point end;
private readonly Tuple<float, TPixel>[] colorStops;
/// <summary>
/// the vector along the gradient, x component
/// </summary>
private readonly float alongX;
/// <summary>
/// the vector along the gradient, y component
/// </summary>
private readonly float alongY;
/// <summary>
/// the vector perpendicular to the gradient, y component
/// </summary>
private readonly float acrossY;
/// <summary>
/// the vector perpendicular to the gradient, x component
/// </summary>
private readonly float acrossX;
/// <summary>
/// helper to speed up calculation as these dont't change
/// </summary>
private readonly float aYcX;
/// <summary>
/// helper to speed up calculation as these dont't change
/// </summary>
private readonly float aXcY;
/// <summary>
/// helper to speed up calculation as these dont't change
/// </summary>
private readonly float aXcX;
/// <summary>
/// Initializes a new instance of the <see cref="LinearGradientBrushApplicator" /> class.
/// </summary>
/// <param name="source">The source</param>
/// <param name="start">start point of the gradient</param>
/// <param name="end">end point of the gradient</param>
/// <param name="colorStops">tuple list of colors and their respective position between 0 and 1 on the line</param>
/// <param name="region">the region, copied from SolidColorBrush, not sure if necessary! TODO</param>
/// <param name="options">the graphics options</param>
public LinearGradientBrushApplicator(
ImageFrame<TPixel> source,
Point start,
Point end,
Tuple<float, TPixel>[] colorStops,
RectangleF region, // TODO: use region, compare with other Brushes for reference.
GraphicsOptions options)
: base(source, options)
{
this.start = start;
this.end = end;
this.colorStops = colorStops; // TODO: requires colorStops to be sorted by Item1!
// the along vector:
this.alongX = this.start.X - this.end.X;
this.alongY = this.start.Y - this.end.Y;
// the cross vector:
this.acrossX = this.alongY;
this.acrossY = -this.alongX;
// some helpers:
this.aYcX = this.alongY * this.acrossX;
this.aXcY = this.alongX * this.acrossY;
this.aXcX = this.alongX * this.acrossX;
}
/// <summary>
/// Gets the color for a single pixel
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
internal override TPixel this[int x, int y]
{
get
{
// the following formula is the result of the linear equation system that forms the vector.
// TODO: this formula should be abstracted as it's the only difference between linear and radial gradient!
float onCompleteGradient = this.RatioOnGradient(x, y);
var localGradientFrom = this.colorStops[0];
Tuple<float, TPixel> localGradientTo = null;
// TODO: ensure colorStops has at least 2 items (technically 1 would be okay, but that's no gradient)
foreach (var colorStop in this.colorStops)
{
localGradientTo = colorStop;
if (colorStop.Item1 >= onCompleteGradient)
{
// we're done here, so break it!
break;
}
localGradientFrom = localGradientTo;
}
TPixel resultColor = default;
if (localGradientFrom.Item2.Equals(localGradientTo.Item2))
{
resultColor = localGradientFrom.Item2;
}
else
{
var fromAsVector = localGradientFrom.Item2.ToVector4();
var toAsVector = localGradientTo.Item2.ToVector4();
float onLocalGradient = (onCompleteGradient - localGradientFrom.Item1) / localGradientTo.Item1; // TODO:
Vector4 result = PorterDuffFunctions.Normal(
fromAsVector,
toAsVector,
onLocalGradient);
// TODO: when resultColor is a struct, what does PackFromVector4 do here?
resultColor.PackFromVector4(result);
}
return resultColor;
}
}
private float RatioOnGradient(int x, int y)
{
return ((x / this.acrossX) - (this.alongX * y / this.aYcX))
/ (1 - (this.aXcY / this.aXcX));
}
internal override void Apply(Span<float> scanline, int x, int y)
{
base.Apply(scanline, x, y);
// Span<TPixel> destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
// MemoryManager memoryManager = this.Target.MemoryManager;
// using (IBuffer<float> amountBuffer = memoryManager.Allocate<float>(scanline.Length))
// {
// Span<float> amountSpan = amountBuffer.Span;
//
// for (int i = 0; i < scanline.Length; i++)
// {
// amountSpan[i] = scanline[i] * this.Options.BlendPercentage;
// }
//
// this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, amountSpan);
// }
}
/// <inheritdoc />
public override void Dispose()
{
}
}
}
}