Skip to content

Commit c022bb0

Browse files
committed
Added OffIfOpaque to AlphaOption.
1 parent d7e2e59 commit c022bb0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Magick.NET.Core/Enums/AlphaOption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,9 @@ public enum AlphaOption
101101
/// RGB data still intact, but fully transparent.
102102
/// </summary>
103103
Transparent,
104+
105+
/// <summary>
106+
/// Removes the alpha channel when the alpha value is opaque for all pixels.
107+
/// </summary>
108+
OffIfOpaque,
104109
}

tests/Magick.NET.Tests/MagickImageTests/TheAlphaMethod.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,35 @@ public void ShouldUseTheBackgroundColor()
3636
Assert.False(image.HasAlpha);
3737
ColorAssert.Equal(new MagickColor(Quantum.Max, 0, 0), image, 0, 0);
3838
}
39+
40+
[Fact]
41+
public void ShouldRemoveAlphaChannelIfAllPixelsAreOpaque()
42+
{
43+
using var image = new MagickImage(MagickColors.Red, 2, 2);
44+
image.Alpha(AlphaOption.On);
45+
46+
Assert.True(image.HasAlpha);
47+
48+
image.Alpha(AlphaOption.OffIfOpaque);
49+
50+
Assert.False(image.HasAlpha);
51+
}
52+
53+
[Fact]
54+
public void ShouldKeepAlphaChannelIfSinglePixesIsNotOpaque()
55+
{
56+
using var image = new MagickImage(MagickColors.Red, 2, 2);
57+
image.Alpha(AlphaOption.On);
58+
59+
Assert.True(image.HasAlpha);
60+
61+
using var pixels = image.GetPixels();
62+
var pixel = pixels.GetPixel(1, 1);
63+
pixel[3] = 0;
64+
65+
image.Alpha(AlphaOption.OffIfOpaque);
66+
67+
Assert.True(image.HasAlpha);
68+
}
3969
}
4070
}

0 commit comments

Comments
 (0)