Skip to content

Commit 9f3864c

Browse files
committed
Introduce PenOptinos
1 parent b47e520 commit 9f3864c

7 files changed

Lines changed: 77 additions & 48 deletions

File tree

src/ImageSharp.Drawing/Processing/Pen.cs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,18 @@ public sealed class Pen
2020
{
2121
private readonly float[] pattern;
2222

23-
/// <summary>
24-
/// Initializes a new instance of the <see cref="Pen"/> class.
25-
/// </summary>
26-
/// <param name="color">The color.</param>
27-
/// <param name="width">The width.</param>
28-
/// <param name="pattern">The pattern.</param>
29-
public Pen(Color color, float width, float[] pattern)
30-
: this(new SolidBrush(color), width, pattern)
23+
public Pen(Brush strokeFill)
24+
: this(strokeFill, 0)
3125
{
3226
}
3327

34-
/// <summary>
35-
/// Initializes a new instance of the <see cref="Pen"/> class.
36-
/// </summary>
37-
/// <param name="brush">The brush.</param>
38-
/// <param name="width">The width.</param>
39-
/// <param name="pattern">The pattern.</param>
40-
public Pen(Brush brush, float width, float[] pattern)
28+
public Pen(Brush strokeFill, float strokeWidth)
29+
: this(strokeFill, strokeWidth, Pens.EmptyPattern)
4130
{
42-
Guard.MustBeGreaterThan(width, 0, nameof(width));
43-
44-
this.StrokeFill = brush;
45-
this.StrokeWidth = width;
46-
this.pattern = pattern;
4731
}
4832

49-
/// <summary>
50-
/// Initializes a new instance of the <see cref="Pen"/> class.
51-
/// </summary>
52-
/// <param name="color">The color.</param>
53-
/// <param name="width">The width.</param>
54-
public Pen(Color color, float width)
55-
: this(new SolidBrush(color), width)
33+
public Pen(PenOptions options)
34+
: this(options.StrokeFill, options.StrokeWidth)
5635
{
5736
}
5837

@@ -61,9 +40,14 @@ public Pen(Color color, float width)
6140
/// </summary>
6241
/// <param name="brush">The brush.</param>
6342
/// <param name="width">The width.</param>
64-
public Pen(Brush brush, float width)
65-
: this(brush, width, Pens.EmptyPattern)
43+
/// <param name="pattern">The pattern.</param>
44+
private Pen(Brush brush, float width, float[] pattern)
6645
{
46+
Guard.MustBeGreaterThan(width, 0, nameof(width));
47+
48+
this.StrokeFill = brush;
49+
this.StrokeWidth = width;
50+
this.pattern = pattern;
6751
}
6852

6953
/// <inheritdoc/>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Collections.Generic;
5+
6+
namespace SixLabors.ImageSharp.Drawing.Processing
7+
{
8+
/// <summary>
9+
/// Options for the Pen
10+
/// </summary>
11+
public class PenOptions
12+
{
13+
public PenOptions(float strokeWidth)
14+
: this(Color.Black, strokeWidth)
15+
{
16+
}
17+
18+
public PenOptions(Color color, float strokeWidth)
19+
: this(color, strokeWidth, null)
20+
{
21+
}
22+
23+
public PenOptions(Color color, float strokeWidth, IEnumerable<float>? strokePattern)
24+
: this(new SolidBrush(color), strokeWidth, strokePattern)
25+
{
26+
}
27+
28+
public PenOptions(Brush strokeFill, float strokeWidth, IEnumerable<float>? strokePattern)
29+
{
30+
this.StrokeFill = strokeFill;
31+
this.StrokeWidth = strokeWidth;
32+
this.StrokePattern = strokePattern;
33+
}
34+
35+
public Brush StrokeFill { get; set; } // defaults to black solid brush when undefined
36+
37+
public float StrokeWidth { get; set; }
38+
39+
IEnumerable<float>? StrokePattern { get; set; }
40+
41+
public JointStyle? JointStyle { get; set; }
42+
43+
public EndCapStyle? EndCap { get; set; }
44+
}
45+
}

src/ImageSharp.Drawing/Processing/Pens.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static class Pens
2222
/// <param name="color">The color.</param>
2323
/// <param name="width">The width.</param>
2424
/// <returns>The Pen</returns>
25-
public static Pen Solid(Color color, float width) => new Pen(color, width);
25+
public static Pen Solid(Color color, float width) => new Pen(new PenOptions(color, width));
2626

2727
/// <summary>
2828
/// Create a solid pen with out any drawing patterns
@@ -38,62 +38,62 @@ public static class Pens
3838
/// <param name="color">The color.</param>
3939
/// <param name="width">The width.</param>
4040
/// <returns>The Pen</returns>
41-
public static Pen Dash(Color color, float width) => new Pen(color, width, DashedPattern);
41+
public static Pen Dash(Color color, float width) => new Pen(new PenOptions(color, width, DashedPattern));
4242

4343
/// <summary>
4444
/// Create a pen with a 'Dash' drawing patterns
4545
/// </summary>
4646
/// <param name="brush">The brush.</param>
4747
/// <param name="width">The width.</param>
4848
/// <returns>The Pen</returns>
49-
public static Pen Dash(Brush brush, float width) => new Pen(brush, width, DashedPattern);
49+
public static Pen Dash(Brush brush, float width) => new Pen(new PenOptions(brush, width, DashedPattern));
5050

5151
/// <summary>
5252
/// Create a pen with a 'Dot' drawing patterns
5353
/// </summary>
5454
/// <param name="color">The color.</param>
5555
/// <param name="width">The width.</param>
5656
/// <returns>The Pen</returns>
57-
public static Pen Dot(Color color, float width) => new Pen(color, width, DottedPattern);
57+
public static Pen Dot(Color color, float width) => new Pen(new PenOptions(color, width, DottedPattern));
5858

5959
/// <summary>
6060
/// Create a pen with a 'Dot' drawing patterns
6161
/// </summary>
6262
/// <param name="brush">The brush.</param>
6363
/// <param name="width">The width.</param>
6464
/// <returns>The Pen</returns>
65-
public static Pen Dot(Brush brush, float width) => new Pen(brush, width, DottedPattern);
65+
public static Pen Dot(Brush brush, float width) => new Pen(new PenOptions(brush, width, DottedPattern));
6666

6767
/// <summary>
6868
/// Create a pen with a 'Dash Dot' drawing patterns
6969
/// </summary>
7070
/// <param name="color">The color.</param>
7171
/// <param name="width">The width.</param>
7272
/// <returns>The Pen</returns>
73-
public static Pen DashDot(Color color, float width) => new Pen(color, width, DashDotPattern);
73+
public static Pen DashDot(Color color, float width) => new Pen(new PenOptions(color, width, DashDotPattern));
7474

7575
/// <summary>
7676
/// Create a pen with a 'Dash Dot' drawing patterns
7777
/// </summary>
7878
/// <param name="brush">The brush.</param>
7979
/// <param name="width">The width.</param>
8080
/// <returns>The Pen</returns>
81-
public static Pen DashDot(Brush brush, float width) => new Pen(brush, width, DashDotPattern);
81+
public static Pen DashDot(Brush brush, float width) => new Pen(new PenOptions(brush, width, DashDotPattern));
8282

8383
/// <summary>
8484
/// Create a pen with a 'Dash Dot Dot' drawing patterns
8585
/// </summary>
8686
/// <param name="color">The color.</param>
8787
/// <param name="width">The width.</param>
8888
/// <returns>The Pen</returns>
89-
public static Pen DashDotDot(Color color, float width) => new Pen(color, width, DashDotDotPattern);
89+
public static Pen DashDotDot(Color color, float width) => new Pen(new PenOptions(color, width, DashDotDotPattern));
9090

9191
/// <summary>
9292
/// Create a pen with a 'Dash Dot Dot' drawing patterns
9393
/// </summary>
9494
/// <param name="brush">The brush.</param>
9595
/// <param name="width">The width.</param>
9696
/// <returns>The Pen</returns>
97-
public static Pen DashDotDot(Brush brush, float width) => new Pen(brush, width, DashDotDotPattern);
97+
public static Pen DashDotDot(Brush brush, float width) => new Pen(new PenOptions(brush, width, DashDotDotPattern));
9898
}
9999
}

tests/ImageSharp.Drawing.Tests/Drawing/DrawLinesTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void DrawLines_Simple<TPixel>(TestImageProvider<TPixel> provider, string
2121
where TPixel : unmanaged, IPixel<TPixel>
2222
{
2323
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
24-
var pen = new Pen(color, thickness);
24+
var pen = new Pen(new PenOptions(color, thickness));
2525

2626
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
2727
}
@@ -76,7 +76,7 @@ public void DrawLines_EndCapRound<TPixel>(TestImageProvider<TPixel> provider, st
7676
where TPixel : unmanaged, IPixel<TPixel>
7777
{
7878
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
79-
Pen pen = new Pen(color, thickness, new float[] { 3f, 3f });
79+
Pen pen = new Pen(new PenOptions(color, thickness, new float[] { 3f, 3f }));
8080
pen.EndCapStyle = EndCapStyle.Round;
8181

8282
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
@@ -88,7 +88,7 @@ public void DrawLines_EndCapButt<TPixel>(TestImageProvider<TPixel> provider, str
8888
where TPixel : unmanaged, IPixel<TPixel>
8989
{
9090
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
91-
Pen pen = new Pen(color, thickness, new float[] { 3f, 3f });
91+
Pen pen = new Pen(new PenOptions(color, thickness, new float[] { 3f, 3f }));
9292
pen.EndCapStyle = EndCapStyle.Butt;
9393

9494
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
@@ -100,7 +100,7 @@ public void DrawLines_EndCapSquare<TPixel>(TestImageProvider<TPixel> provider, s
100100
where TPixel : unmanaged, IPixel<TPixel>
101101
{
102102
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
103-
Pen pen = new Pen(color, thickness, new float[] { 3f, 3f });
103+
Pen pen = new Pen(new PenOptions(color, thickness, new float[] { 3f, 3f }));
104104
pen.EndCapStyle = EndCapStyle.Square;
105105

106106
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
@@ -112,7 +112,7 @@ public void DrawLines_JointStyleRound<TPixel>(TestImageProvider<TPixel> provider
112112
where TPixel : unmanaged, IPixel<TPixel>
113113
{
114114
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
115-
var pen = new Pen(color, thickness);
115+
var pen = new Pen(new PenOptions(color, thickness));
116116
pen.JointStyle = JointStyle.Round;
117117

118118
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
@@ -124,7 +124,7 @@ public void DrawLines_JointStyleSquare<TPixel>(TestImageProvider<TPixel> provide
124124
where TPixel : unmanaged, IPixel<TPixel>
125125
{
126126
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
127-
var pen = new Pen(color, thickness);
127+
var pen = new Pen(new PenOptions(color, thickness));
128128
pen.JointStyle = JointStyle.Square;
129129

130130
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
@@ -136,7 +136,7 @@ public void DrawLines_JointStyleMiter<TPixel>(TestImageProvider<TPixel> provider
136136
where TPixel : unmanaged, IPixel<TPixel>
137137
{
138138
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
139-
var pen = new Pen(color, thickness);
139+
var pen = new Pen(new PenOptions(color, thickness));
140140
pen.JointStyle = JointStyle.Miter;
141141

142142
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);

tests/ImageSharp.Drawing.Tests/Drawing/FillPathTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void FillPathCanvasArcs<TPixel>(TestImageProvider<TPixel> provider)
9999
}
100100
else
101101
{
102-
image.Mutate(x => x.Draw(new Pen(Color.Black, 1), pb.Build()));
102+
image.Mutate(x => x.Draw(new Pen(new PenOptions(Color.Black, 1)), pb.Build()));
103103
}
104104
}
105105
}

tests/ImageSharp.Drawing.Tests/Issues/Issue_54.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void PenMustHaveAWidthGraterThanZero()
4848
{
4949
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() =>
5050
{
51-
Pen pen = new Pen(Color.White, 0);
51+
Pen pen = new Pen(new PenOptions(Color.White, 0));
5252
});
5353

5454
Assert.StartsWith("Parameter \"width\" (System.Single) must be greater than 0, was 0", ex.Message);

tests/ImageSharp.Drawing.Tests/Processing/FillPathProcessorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void DrawOffCanvas()
4444
using (var img = new Image<Rgba32>(10, 10))
4545
{
4646
img.Mutate(x => x.DrawLines(
47-
new Pen(Color.Black, 10),
47+
new Pen(new PenOptions(Color.Black, 10)),
4848
new Vector2(-10, 5),
4949
new Vector2(20, 5)));
5050
}

0 commit comments

Comments
 (0)