Skip to content

Commit 67bac23

Browse files
Merge pull request #3073 from SixLabors/js/antialis-threshold-
Replace AntialiasSubpixelDepth with AntialiasThreshold
2 parents 4ed9b72 + e6acbab commit 67bac23

3 files changed

Lines changed: 45 additions & 45 deletions

File tree

src/ImageSharp/GraphicsOptions.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
namespace SixLabors.ImageSharp;
77

88
/// <summary>
9-
/// Options for influencing the drawing functions.
9+
/// Provides configuration for controlling how graphics operations are rendered,
10+
/// including antialiasing, pixel blending, alpha composition, and coverage thresholding.
1011
/// </summary>
1112
public class GraphicsOptions : IDeepCloneable<GraphicsOptions>
1213
{
13-
private int antialiasSubpixelDepth = 16;
14+
private float antialiasThreshold = .5F;
1415
private float blendPercentage = 1F;
1516

1617
/// <summary>
@@ -24,61 +25,62 @@ private GraphicsOptions(GraphicsOptions source)
2425
{
2526
this.AlphaCompositionMode = source.AlphaCompositionMode;
2627
this.Antialias = source.Antialias;
27-
this.AntialiasSubpixelDepth = source.AntialiasSubpixelDepth;
28+
this.AntialiasThreshold = source.AntialiasThreshold;
2829
this.BlendPercentage = source.BlendPercentage;
2930
this.ColorBlendingMode = source.ColorBlendingMode;
3031
}
3132

3233
/// <summary>
3334
/// Gets or sets a value indicating whether antialiasing should be applied.
34-
/// Defaults to true.
35+
/// When <see langword="true"/>, edges are rendered with smooth sub-pixel coverage.
36+
/// When <see langword="false"/>, coverage is snapped to binary (fully opaque or fully transparent)
37+
/// using <see cref="AntialiasThreshold"/> as the cutoff.
38+
/// Defaults to <see langword="true"/>.
3539
/// </summary>
3640
public bool Antialias { get; set; } = true;
3741

3842
/// <summary>
39-
/// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled.
40-
/// Defaults to 16.
43+
/// Gets or sets the coverage threshold used when <see cref="Antialias"/> is <see langword="false"/>.
44+
/// Pixels with antialiased coverage above this value are rendered as fully opaque;
45+
/// pixels below are discarded. Valid range is 0 to 1. Lower values preserve more
46+
/// thin features at small sizes. Defaults to <c>0.5F</c>.
4147
/// </summary>
42-
public int AntialiasSubpixelDepth
48+
public float AntialiasThreshold
4349
{
44-
get
45-
{
46-
return this.antialiasSubpixelDepth;
47-
}
50+
get => this.antialiasThreshold;
4851

4952
set
5053
{
51-
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.AntialiasSubpixelDepth));
52-
this.antialiasSubpixelDepth = value;
54+
Guard.MustBeBetweenOrEqualTo(value, 0F, 1F, nameof(this.AntialiasThreshold));
55+
this.antialiasThreshold = value;
5356
}
5457
}
5558

5659
/// <summary>
57-
/// Gets or sets a value between indicating the blending percentage to apply to the drawing operation.
58-
/// Range 0..1; Defaults to 1.
60+
/// Gets or sets the blending percentage applied to the drawing operation.
61+
/// A value of <c>1.0</c> applies the operation at full strength; <c>0.0</c> makes it invisible.
62+
/// Valid range is 0 to 1. Defaults to <c>1.0F</c>.
5963
/// </summary>
6064
public float BlendPercentage
6165
{
62-
get
63-
{
64-
return this.blendPercentage;
65-
}
66+
get => this.blendPercentage;
6667

6768
set
6869
{
69-
Guard.MustBeBetweenOrEqualTo(value, 0, 1F, nameof(this.BlendPercentage));
70+
Guard.MustBeBetweenOrEqualTo(value, 0F, 1F, nameof(this.BlendPercentage));
7071
this.blendPercentage = value;
7172
}
7273
}
7374

7475
/// <summary>
75-
/// Gets or sets a value indicating the color blending mode to apply to the drawing operation.
76+
/// Gets or sets the color blending mode used to combine source and destination pixel colors.
7677
/// Defaults to <see cref="PixelColorBlendingMode.Normal"/>.
7778
/// </summary>
7879
public PixelColorBlendingMode ColorBlendingMode { get; set; } = PixelColorBlendingMode.Normal;
7980

8081
/// <summary>
81-
/// Gets or sets a value indicating the alpha composition mode to apply to the drawing operation
82+
/// Gets or sets the alpha composition mode that determines how source and destination alpha
83+
/// channels are combined using Porter-Duff operators.
8284
/// Defaults to <see cref="PixelAlphaCompositionMode.SrcOver"/>.
8385
/// </summary>
8486
public PixelAlphaCompositionMode AlphaCompositionMode { get; set; } = PixelAlphaCompositionMode.SrcOver;

tests/ImageSharp.Tests/GraphicsOptionsTests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class GraphicsOptionsTests
1313
private readonly GraphicsOptions cloneGraphicsOptions = new GraphicsOptions().DeepClone();
1414

1515
[Fact]
16-
public void CloneGraphicsOptionsIsNotNull() => Assert.True(this.cloneGraphicsOptions != null);
16+
public void CloneGraphicsOptionsIsNotNull() => Assert.NotNull(this.cloneGraphicsOptions);
1717

1818
[Fact]
1919
public void DefaultGraphicsOptionsAntialias()
@@ -23,35 +23,35 @@ public void DefaultGraphicsOptionsAntialias()
2323
}
2424

2525
[Fact]
26-
public void DefaultGraphicsOptionsAntialiasSuppixelDepth()
26+
public void DefaultGraphicsOptionsAntialiasThreshold()
2727
{
28-
const int Expected = 16;
29-
Assert.Equal(Expected, this.newGraphicsOptions.AntialiasSubpixelDepth);
30-
Assert.Equal(Expected, this.cloneGraphicsOptions.AntialiasSubpixelDepth);
28+
const float expected = .5F;
29+
Assert.Equal(expected, this.newGraphicsOptions.AntialiasThreshold);
30+
Assert.Equal(expected, this.cloneGraphicsOptions.AntialiasThreshold);
3131
}
3232

3333
[Fact]
3434
public void DefaultGraphicsOptionsBlendPercentage()
3535
{
36-
const float Expected = 1F;
37-
Assert.Equal(Expected, this.newGraphicsOptions.BlendPercentage);
38-
Assert.Equal(Expected, this.cloneGraphicsOptions.BlendPercentage);
36+
const float expected = 1F;
37+
Assert.Equal(expected, this.newGraphicsOptions.BlendPercentage);
38+
Assert.Equal(expected, this.cloneGraphicsOptions.BlendPercentage);
3939
}
4040

4141
[Fact]
4242
public void DefaultGraphicsOptionsColorBlendingMode()
4343
{
44-
const PixelColorBlendingMode Expected = PixelColorBlendingMode.Normal;
45-
Assert.Equal(Expected, this.newGraphicsOptions.ColorBlendingMode);
46-
Assert.Equal(Expected, this.cloneGraphicsOptions.ColorBlendingMode);
44+
const PixelColorBlendingMode expected = PixelColorBlendingMode.Normal;
45+
Assert.Equal(expected, this.newGraphicsOptions.ColorBlendingMode);
46+
Assert.Equal(expected, this.cloneGraphicsOptions.ColorBlendingMode);
4747
}
4848

4949
[Fact]
5050
public void DefaultGraphicsOptionsAlphaCompositionMode()
5151
{
52-
const PixelAlphaCompositionMode Expected = PixelAlphaCompositionMode.SrcOver;
53-
Assert.Equal(Expected, this.newGraphicsOptions.AlphaCompositionMode);
54-
Assert.Equal(Expected, this.cloneGraphicsOptions.AlphaCompositionMode);
52+
const PixelAlphaCompositionMode expected = PixelAlphaCompositionMode.SrcOver;
53+
Assert.Equal(expected, this.newGraphicsOptions.AlphaCompositionMode);
54+
Assert.Equal(expected, this.cloneGraphicsOptions.AlphaCompositionMode);
5555
}
5656

5757
[Fact]
@@ -61,7 +61,7 @@ public void NonDefaultClone()
6161
{
6262
AlphaCompositionMode = PixelAlphaCompositionMode.DestAtop,
6363
Antialias = false,
64-
AntialiasSubpixelDepth = 23,
64+
AntialiasThreshold = .33F,
6565
BlendPercentage = .25F,
6666
ColorBlendingMode = PixelColorBlendingMode.HardLight,
6767
};
@@ -79,7 +79,7 @@ public void CloneIsDeep()
7979

8080
actual.AlphaCompositionMode = PixelAlphaCompositionMode.DestAtop;
8181
actual.Antialias = false;
82-
actual.AntialiasSubpixelDepth = 23;
82+
actual.AntialiasThreshold = .67F;
8383
actual.BlendPercentage = .25F;
8484
actual.ColorBlendingMode = PixelColorBlendingMode.HardLight;
8585

tests/ImageSharp.Tests/TestUtilities/GraphicsOptionsComparer.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities;
66
public class GraphicsOptionsComparer : IEqualityComparer<GraphicsOptions>
77
{
88
public bool Equals(GraphicsOptions x, GraphicsOptions y)
9-
{
10-
return x.AlphaCompositionMode == y.AlphaCompositionMode
11-
&& x.Antialias == y.Antialias
12-
&& x.AntialiasSubpixelDepth == y.AntialiasSubpixelDepth
13-
&& x.BlendPercentage == y.BlendPercentage
14-
&& x.ColorBlendingMode == y.ColorBlendingMode;
15-
}
9+
=> x.AlphaCompositionMode == y.AlphaCompositionMode
10+
&& x.Antialias == y.Antialias
11+
&& x.AntialiasThreshold == y.AntialiasThreshold
12+
&& x.BlendPercentage == y.BlendPercentage
13+
&& x.ColorBlendingMode == y.ColorBlendingMode;
1614

1715
public int GetHashCode(GraphicsOptions obj) => obj.GetHashCode();
1816
}

0 commit comments

Comments
 (0)