Skip to content

Commit 51d12c5

Browse files
authored
Merge pull request SixLabors#908 from SixLabors/af/general-color-type
Introduce representation-agnostic Color type
2 parents a7bd41e + 0f239da commit 51d12c5

9 files changed

Lines changed: 70 additions & 55 deletions

tests/ImageSharp.Tests/Drawing/BeziersTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public void ImageShouldBeOverlayedByBezierLine()
1919
string path = TestEnvironment.CreateOutputDirectory("Drawing", "BezierLine");
2020
using (var image = new Image<Rgba32>(500, 500))
2121
{
22+
image.Mutate(x => x.BackgroundColor(Color.Blue));
2223
image.Mutate(
23-
x => x.BackgroundColor(Rgba32.Blue).DrawBeziers(
24+
x => x.DrawBeziers(
2425
Rgba32.HotPink,
2526
5,
2627
new SixLabors.Primitives.PointF[]
@@ -56,8 +57,9 @@ public void ImageShouldBeOverlayedBezierLineWithOpacity()
5657

5758
using (var image = new Image<Rgba32>(500, 500))
5859
{
60+
image.Mutate(x => x.BackgroundColor(Color.Blue));
5961
image.Mutate(
60-
x => x.BackgroundColor(Rgba32.Blue).DrawBeziers(
62+
x => x.DrawBeziers(
6163
color,
6264
10,
6365
new SixLabors.Primitives.PointF[]

tests/ImageSharp.Tests/Drawing/DrawImageTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void ImageShouldDrawTransformedImage<TPixel>(TestImageProvider<TPixel> pr
8080

8181
// Apply a background color so we can see the translation.
8282
blend.Mutate(x => x.Transform(builder));
83-
blend.Mutate(x => x.BackgroundColor(NamedColors<TPixel>.HotPink));
83+
blend.Mutate(x => x.BackgroundColor(Color.HotPink));
8484

8585
// Lets center the matrix so we can tell whether any cut-off issues we may have belong to the drawing processor
8686
var position = new Point((image.Width - blend.Width) / 2, (image.Height - blend.Height) / 2);

tests/ImageSharp.Tests/Drawing/DrawPathTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public void ImageShouldBeOverlayedByPath()
3030
new Vector2(10, 400));
3131

3232
var p = new Path(linerSegemnt, bazierSegment);
33-
34-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, p));
33+
34+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
35+
image.Mutate(x => x.Draw(Rgba32.HotPink, 5, p));
3536
image.Save($"{path}/Simple.png");
3637

3738
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
@@ -67,7 +68,8 @@ public void ImageShouldBeOverlayedPathWithOpacity()
6768

6869
using (var image = new Image<Rgba32>(500, 500))
6970
{
70-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(color, 10, p));
71+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
72+
image.Mutate(x => x.Draw(color, 10, p));
7173
image.Save($"{path}/Opacity.png");
7274

7375
//shift background color towards forground color by the opacity amount

tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public void ImageShouldBeOverlayedByPolygonOutline()
3131

3232
using (var image = new Image<Rgba32>(500, 500))
3333
{
34-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1)));
34+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
35+
image.Mutate(x => x.Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1)));
3536
image.Save($"{path}/Simple.png");
3637

3738
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
@@ -73,7 +74,8 @@ public void ImageShouldBeOverlayedByPolygonOutlineNoOverlapping()
7374

7475
using (var image = new Image<Rgba32>(500, 500))
7576
{
76-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1)));
77+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
78+
image.Mutate(x => x.Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1)));
7779
image.Save($"{path}/SimpleVanishHole.png");
7880

7981
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
@@ -113,7 +115,8 @@ public void ImageShouldBeOverlayedByPolygonOutlineOverlapping()
113115

114116
using (var image = new Image<Rgba32>(500, 500))
115117
{
116-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1)));
118+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
119+
image.Mutate(x => x.Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1)));
117120
image.Save($"{path}/SimpleOverlapping.png");
118121

119122
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
@@ -147,9 +150,8 @@ public void ImageShouldBeOverlayedByPolygonOutlineDashed()
147150

148151
using (var image = new Image<Rgba32>(500, 500))
149152
{
150-
image.Mutate(x => x
151-
.BackgroundColor(Rgba32.Blue)
152-
.Draw(Pens.Dash(Rgba32.HotPink, 5), simplePath.Clip(hole1)));
153+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
154+
image.Mutate(x => x.Draw(Pens.Dash(Rgba32.HotPink, 5), simplePath.Clip(hole1)));
153155
image.Save($"{path}/Dashed.png");
154156
}
155157
}
@@ -171,7 +173,8 @@ public void ImageShouldBeOverlayedPolygonOutlineWithOpacity()
171173

172174
using (var image = new Image<Rgba32>(500, 500))
173175
{
174-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(color, 5, simplePath.Clip(hole1)));
176+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
177+
image.Mutate(x => x.Draw(color, 5, simplePath.Clip(hole1)));
175178
image.Save($"{path}/Opacity.png");
176179

177180
//shift background color towards forground color by the opacity amount

tests/ImageSharp.Tests/Drawing/LineTests.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public void ImageShouldBeOverlayedByPath()
1919
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
2020
using (var image = new Image<Rgba32>(500, 500))
2121
{
22+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
2223
image.Mutate(
23-
x => x.BackgroundColor(Rgba32.Blue).DrawLines(
24+
x => x.DrawLines(
2425
Rgba32.HotPink,
2526
5,
2627
new Vector2(10, 10),
@@ -43,8 +44,9 @@ public void ImageShouldBeOverlayedByPath_NoAntialias()
4344
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
4445
using (var image = new Image<Rgba32>(500, 500))
4546
{
47+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
4648
image.Mutate(
47-
x => x.BackgroundColor(Rgba32.Blue).DrawLines(
49+
x => x.DrawLines(
4850
new GraphicsOptions(false),
4951
Rgba32.HotPink,
5052
5,
@@ -68,9 +70,8 @@ public void ImageShouldBeOverlayedByPathDashed()
6870
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
6971
using (var image = new Image<Rgba32>(500, 500))
7072
{
71-
image.Mutate(x => x
72-
.BackgroundColor(Rgba32.Blue)
73-
.DrawLines(Pens.Dash(Rgba32.HotPink, 5),
73+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
74+
image.Mutate(x => x.DrawLines(Pens.Dash(Rgba32.HotPink, 5),
7475
new SixLabors.Primitives.PointF[] {
7576
new Vector2(10, 10),
7677
new Vector2(200, 150),
@@ -86,9 +87,8 @@ public void ImageShouldBeOverlayedByPathDotted()
8687
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
8788
using (var image = new Image<Rgba32>(500, 500))
8889
{
89-
image.Mutate(x => x
90-
.BackgroundColor(Rgba32.Blue)
91-
.DrawLines(Pens.Dot(Rgba32.HotPink, 5),
90+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
91+
image.Mutate(x => x.DrawLines(Pens.Dot(Rgba32.HotPink, 5),
9292
new SixLabors.Primitives.PointF[] {
9393
new Vector2(10, 10),
9494
new Vector2(200, 150),
@@ -104,9 +104,8 @@ public void ImageShouldBeOverlayedByPathDashDot()
104104
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
105105
using (var image = new Image<Rgba32>(500, 500))
106106
{
107-
image.Mutate(x => x
108-
.BackgroundColor(Rgba32.Blue)
109-
.DrawLines(Pens.DashDot(Rgba32.HotPink, 5),
107+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
108+
image.Mutate(x => x.DrawLines(Pens.DashDot(Rgba32.HotPink, 5),
110109
new SixLabors.Primitives.PointF[] {
111110
new Vector2(10, 10),
112111
new Vector2(200, 150),
@@ -122,9 +121,9 @@ public void ImageShouldBeOverlayedByPathDashDotDot()
122121
string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines");
123122
var image = new Image<Rgba32>(500, 500);
124123

125-
image.Mutate(x => x
126-
.BackgroundColor(Rgba32.Blue)
127-
.DrawLines(Pens.DashDotDot(Rgba32.HotPink, 5), new SixLabors.Primitives.PointF[] {
124+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
125+
image.Mutate(x => x.DrawLines(Pens.DashDotDot(Rgba32.HotPink, 5),
126+
new SixLabors.Primitives.PointF[] {
128127
new Vector2(10, 10),
129128
new Vector2(200, 150),
130129
new Vector2(50, 300)
@@ -141,8 +140,9 @@ public void ImageShouldBeOverlayedPathWithOpacity()
141140

142141
var image = new Image<Rgba32>(500, 500);
143142

143+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
144144
image.Mutate(
145-
x => x.BackgroundColor(Rgba32.Blue).DrawLines(
145+
x => x.DrawLines(
146146
color,
147147
10,
148148
new Vector2(10, 10),
@@ -169,8 +169,9 @@ public void ImageShouldBeOverlayedByPathOutline()
169169

170170
var image = new Image<Rgba32>(500, 500);
171171

172+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
172173
image.Mutate(
173-
x => x.BackgroundColor(Rgba32.Blue).DrawLines(
174+
x => x.DrawLines(
174175
Rgba32.HotPink,
175176
10,
176177
new Vector2(10, 10),

tests/ImageSharp.Tests/Drawing/PolygonTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public void ImageShouldBeOverlayedByPolygonOutline()
2020

2121
using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
2222
{
23+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
2324
image.Mutate(
24-
x => x.BackgroundColor(Rgba32.Blue).DrawPolygon(
25+
x => x.DrawPolygon(
2526
Rgba32.HotPink,
2627
5,
2728
new Vector2(10, 10),
@@ -54,7 +55,8 @@ public void ImageShouldBeOverlayedPolygonOutlineWithOpacity()
5455

5556
using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
5657
{
57-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).DrawPolygon(color, 10, simplePath));
58+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
59+
image.Mutate(x => x.DrawPolygon(color, 10, simplePath));
5860
image.Save($"{path}/Opacity.png");
5961

6062
//shift background color towards forground color by the opacity amount
@@ -79,8 +81,9 @@ public void ImageShouldBeOverlayedByRectangleOutline()
7981

8082
using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
8183
{
84+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
8285
image.Mutate(
83-
x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 10, new Rectangle(10, 10, 190, 140)));
86+
x => x.Draw(Rgba32.HotPink, 10, new Rectangle(10, 10, 190, 140)));
8487
image.Save($"{path}/Rectangle.png");
8588

8689
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();

tests/ImageSharp.Tests/Drawing/SolidBezierTests.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ public void FilledBezier<TPixel>(TestImageProvider<TPixel> provider)
2424
new Vector2(300, 400)
2525
};
2626

27-
TPixel blue = NamedColors<TPixel>.Blue;
28-
TPixel hotPink = NamedColors<TPixel>.HotPink;
27+
Color blue = Color.Blue;
28+
Color hotPink = Color.HotPink;
2929

3030
using (Image<TPixel> image = provider.GetImage())
3131
{
32-
33-
image.Mutate(x => x
34-
.BackgroundColor(blue)
35-
.Fill(hotPink, new Polygon(new CubicBezierLineSegment(simplePath))));
32+
image.Mutate(x => x.BackgroundColor(blue));
33+
image.Mutate(x => x.Fill(hotPink.ToPixel<TPixel>(), new Polygon(new CubicBezierLineSegment(simplePath))));
3634
image.DebugSave(provider);
3735
image.CompareToReferenceOutput(provider);
3836
}
@@ -55,9 +53,9 @@ public void OverlayByFilledPolygonOpacity<TPixel>(TestImageProvider<TPixel> prov
5553

5654
using (var image = provider.GetImage() as Image<Rgba32>)
5755
{
58-
image.Mutate(x => x
59-
.BackgroundColor(Rgba32.Blue)
60-
.Fill(color, new Polygon(new CubicBezierLineSegment(simplePath))));
56+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
57+
58+
image.Mutate(x => x.Fill(color, new Polygon(new CubicBezierLineSegment(simplePath))));
6159
image.DebugSave(provider);
6260
image.CompareToReferenceOutput(provider);
6361
}

tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public void ImageShouldBeOverlayedByPolygonOutline()
3131
// var clipped = new Rectangle(10, 10, 100, 100).Clip(new Rectangle(20, 0, 20, 20));
3232
using (var image = new Image<Rgba32>(500, 500))
3333
{
34-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, clipped));
34+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
35+
image.Mutate(x => x.Fill(Rgba32.HotPink, clipped));
3536
image.Save($"{path}/Simple.png");
3637

3738
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
@@ -59,7 +60,8 @@ public void ImageShouldBeOverlayedPolygonOutlineWithOverlap()
5960

6061
using (var image = new Image<Rgba32>(500, 500))
6162
{
62-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, simplePath.Clip(hole1)));
63+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
64+
image.Mutate(x => x.Fill(Rgba32.HotPink, simplePath.Clip(hole1)));
6365
image.Save($"{path}/SimpleOverlapping.png");
6466

6567
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
@@ -88,7 +90,8 @@ public void ImageShouldBeOverlayedPolygonOutlineWithOpacity()
8890

8991
using (var image = new Image<Rgba32>(500, 500))
9092
{
91-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(color, simplePath.Clip(hole1)));
93+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
94+
image.Mutate(x => x.Fill(color, simplePath.Clip(hole1)));
9295
image.Save($"{path}/Opacity.png");
9396

9497
//shift background color towards forground color by the opacity amount

tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ public void ImageShouldBeOverlayedByFilledPolygonNoAntialias()
6868

6969
using (var image = new Image<Rgba32>(500, 500))
7070
{
71+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
7172
image.Mutate(
72-
x => x.BackgroundColor(Rgba32.Blue).FillPolygon(
73+
x => x.FillPolygon(
7374
new GraphicsOptions(false),
7475
Rgba32.HotPink,
7576
simplePath));
@@ -101,9 +102,8 @@ public void ImageShouldBeOverlayedByFilledPolygonImage()
101102
{
102103
var brush = new ImageBrush<Rgba32>(brushImage);
103104

104-
image.Mutate(x => x
105-
.BackgroundColor(Rgba32.Blue)
106-
.FillPolygon(brush, simplePath));
105+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
106+
image.Mutate(x => x.FillPolygon(brush, simplePath));
107107
image.Save($"{path}/Image.png");
108108
}
109109
}
@@ -121,7 +121,8 @@ public void ImageShouldBeOverlayedByFilledPolygonOpacity()
121121

122122
using (var image = new Image<Rgba32>(500, 500))
123123
{
124-
image.Mutate(x => x.BackgroundColor(Rgba32.Blue).FillPolygon(color, simplePath));
124+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
125+
image.Mutate(x => x.FillPolygon(color, simplePath));
125126
image.Save($"{path}/Opacity.png");
126127

127128
//shift background color towards forground color by the opacity amount
@@ -140,8 +141,9 @@ public void ImageShouldBeOverlayedByFilledRectangle()
140141

141142
using (var image = new Image<Rgba32>(500, 500))
142143
{
144+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
143145
image.Mutate(
144-
x => x.BackgroundColor(Rgba32.Blue).Fill(
146+
x => x.Fill(
145147
Rgba32.HotPink,
146148
new SixLabors.Shapes.RectangularPolygon(10, 10, 190, 140)));
147149
image.Save($"{path}/Rectangle.png");
@@ -166,8 +168,9 @@ public void ImageShouldBeOverlayedByFilledTriangle()
166168

167169
using (var image = new Image<Rgba32>(100, 100))
168170
{
171+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
169172
image.Mutate(
170-
x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, new RegularPolygon(50, 50, 3, 30)));
173+
x => x.Fill(Rgba32.HotPink, new RegularPolygon(50, 50, 3, 30)));
171174
image.Save($"{path}/Triangle.png");
172175

173176
Buffer2D<Rgba32> sourcePixels = image.GetRootFramePixelBuffer();
@@ -186,9 +189,9 @@ public void ImageShouldBeOverlayedByFilledSeptagon()
186189
config.MaxDegreeOfParallelism = 1;
187190
using (var image = new Image<Rgba32>(config, 100, 100))
188191
{
189-
image.Mutate(x => x
190-
.BackgroundColor(Rgba32.Blue)
191-
.Fill(Rgba32.HotPink, new RegularPolygon(50, 50, 7, 30, -(float)Math.PI)));
192+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
193+
image.Mutate(x => x.Fill(Rgba32.HotPink,
194+
new RegularPolygon(50, 50, 7, 30, -(float)Math.PI)));
192195
image.Save($"{path}/Septagon.png");
193196
}
194197
}
@@ -202,8 +205,8 @@ public void ImageShouldBeOverlayedByFilledEllipse()
202205
config.MaxDegreeOfParallelism = 1;
203206
using (var image = new Image<Rgba32>(config, 100, 100))
204207
{
208+
image.Mutate(x => x.BackgroundColor(Rgba32.Blue));
205209
image.Mutate(x => x
206-
.BackgroundColor(Rgba32.Blue)
207210
.Fill(Rgba32.HotPink, new EllipsePolygon(50, 50, 30, 50)
208211
.Rotate((float)(Math.PI / 3))));
209212
image.Save($"{path}/ellipse.png");

0 commit comments

Comments
 (0)