|
1 | 1 | // Copyright (c) Six Labors and contributors. |
2 | 2 | // Licensed under the Apache License, Version 2.0. |
3 | 3 |
|
4 | | -using System.Numerics; |
5 | 4 | using SixLabors.ImageSharp.PixelFormats; |
6 | 5 | using SixLabors.ImageSharp.Processing; |
7 | 6 | using SixLabors.ImageSharp.Processing.Drawing; |
8 | | -using SixLabors.ImageSharp.Processing.Overlays; |
| 7 | +using SixLabors.ImageSharp.Primitives; |
| 8 | +using SixLabors.ImageSharp.Processing.Drawing.Brushes; |
| 9 | +using SixLabors.Shapes; |
9 | 10 | using Xunit; |
| 11 | +// ReSharper disable InconsistentNaming |
10 | 12 |
|
11 | 13 | namespace SixLabors.ImageSharp.Tests.Drawing |
12 | 14 | { |
13 | | - public class FillSolidBrushTests : FileTestBase |
| 15 | + |
| 16 | + |
| 17 | + [GroupOutput("Drawing")] |
| 18 | + public class FillSolidBrushTests |
14 | 19 | { |
15 | | - [Fact] |
16 | | - public void ImageShouldBeFloodFilledWithColorOnDefaultBackground() |
| 20 | + [Theory] |
| 21 | + [WithBlankImages(1, 1, PixelTypes.Rgba32)] |
| 22 | + [WithBlankImages(7, 4, PixelTypes.Rgba32)] |
| 23 | + [WithBlankImages(16, 7, PixelTypes.Rgba32)] |
| 24 | + [WithBlankImages(33, 32, PixelTypes.Rgba32)] |
| 25 | + [WithBlankImages(400, 500, PixelTypes.Rgba32)] |
| 26 | + public void DoesNotDependOnSize<TPixel>(TestImageProvider<TPixel> provider) |
| 27 | + where TPixel : struct, IPixel<TPixel> |
17 | 28 | { |
18 | | - string path = TestEnvironment.CreateOutputDirectory("Fill", "SolidBrush"); |
19 | | - using (var image = new Image<Rgba32>(500, 500)) |
| 29 | + using (Image<TPixel> image = provider.GetImage()) |
20 | 30 | { |
21 | | - image.Mutate(x => x.Fill(Rgba32.HotPink)); |
22 | | - image.Save($"{path}/DefaultBack.png"); |
23 | | - |
24 | | - using (PixelAccessor<Rgba32> sourcePixels = image.Lock()) |
25 | | - { |
26 | | - Assert.Equal(Rgba32.HotPink, sourcePixels[9, 9]); |
| 31 | + TPixel color = NamedColors<TPixel>.HotPink; |
| 32 | + image.Mutate(c => c.Fill(color)); |
27 | 33 |
|
28 | | - Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); |
29 | | - } |
| 34 | + image.DebugSave(provider, appendPixelTypeToFileName: false); |
| 35 | + image.ComparePixelBufferTo(color); |
30 | 36 | } |
31 | 37 | } |
32 | 38 |
|
33 | | - [Fact] |
34 | | - public void ImageShouldBeFloodFilledWithColor() |
| 39 | + [Theory] |
| 40 | + [WithBlankImages(16, 16, PixelTypes.Rgba32 | PixelTypes.Argb32 | PixelTypes.RgbaVector)] |
| 41 | + public void DoesNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) |
| 42 | + where TPixel : struct, IPixel<TPixel> |
35 | 43 | { |
36 | | - string path = TestEnvironment.CreateOutputDirectory("Fill", "SolidBrush"); |
37 | | - using (var image = new Image<Rgba32>(500, 500)) |
| 44 | + using (Image<TPixel> image = provider.GetImage()) |
38 | 45 | { |
39 | | - image.Mutate(x => x |
40 | | - .BackgroundColor(Rgba32.Blue) |
41 | | - .Fill(Rgba32.HotPink)); |
42 | | - image.Save($"{path}/Simple.png"); |
| 46 | + TPixel color = NamedColors<TPixel>.HotPink; |
| 47 | + image.Mutate(c => c.Fill(color)); |
43 | 48 |
|
44 | | - using (PixelAccessor<Rgba32> sourcePixels = image.Lock()) |
45 | | - { |
46 | | - Assert.Equal(Rgba32.HotPink, sourcePixels[9, 9]); |
47 | | - |
48 | | - Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); |
49 | | - } |
| 49 | + image.DebugSave(provider, appendSourceFileOrDescription: false); |
| 50 | + image.ComparePixelBufferTo(color); |
50 | 51 | } |
51 | 52 | } |
52 | 53 |
|
53 | | - [Fact] |
54 | | - public void ImageShouldBeFloodFilledWithColorOpacity() |
| 54 | + [Theory] |
| 55 | + [WithSolidFilledImages(16, 16, "Red", PixelTypes.Rgba32, "Blue")] |
| 56 | + [WithSolidFilledImages(16, 16, "Yellow", PixelTypes.Rgba32, "Khaki")] |
| 57 | + public void WhenColorIsOpaque_OverridePreviousColor<TPixel>(TestImageProvider<TPixel> provider, string newColorName) |
| 58 | + where TPixel : struct, IPixel<TPixel> |
55 | 59 | { |
56 | | - string path = TestEnvironment.CreateOutputDirectory("Fill", "SolidBrush"); |
57 | | - using (var image = new Image<Rgba32>(500, 500)) |
| 60 | + using (Image<TPixel> image = provider.GetImage()) |
58 | 61 | { |
59 | | - var color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); |
| 62 | + TPixel color = TestUtils.GetPixelOfNamedColor<TPixel>(newColorName); |
| 63 | + image.Mutate(c => c.Fill(color)); |
| 64 | + |
| 65 | + image.DebugSave(provider, newColorName, appendPixelTypeToFileName: false, appendSourceFileOrDescription: false); |
| 66 | + image.ComparePixelBufferTo(color); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static readonly TheoryData<bool, string, float, PixelBlenderMode, float> BlendData = |
| 71 | + new TheoryData<bool, string, float, PixelBlenderMode, float>() |
| 72 | + { |
| 73 | + { false, "Blue", 0.5f, PixelBlenderMode.Normal, 1.0f }, |
| 74 | + { false, "Blue", 1.0f, PixelBlenderMode.Normal, 0.5f }, |
| 75 | + { false, "Green", 0.5f, PixelBlenderMode.Normal, 0.3f }, |
| 76 | + { false, "HotPink", 0.8f, PixelBlenderMode.Normal, 0.8f }, |
| 77 | + |
| 78 | + { false, "Blue", 0.5f, PixelBlenderMode.Multiply, 1.0f }, |
| 79 | + { false, "Blue", 1.0f, PixelBlenderMode.Multiply, 0.5f }, |
| 80 | + { false, "Green", 0.5f, PixelBlenderMode.Multiply, 0.3f }, |
| 81 | + { false, "HotPink", 0.8f, PixelBlenderMode.Multiply, 0.8f }, |
| 82 | + |
| 83 | + { false, "Blue", 0.5f, PixelBlenderMode.Add, 1.0f }, |
| 84 | + { false, "Blue", 1.0f, PixelBlenderMode.Add, 0.5f }, |
| 85 | + { false, "Green", 0.5f, PixelBlenderMode.Add, 0.3f }, |
| 86 | + { false, "HotPink", 0.8f, PixelBlenderMode.Add, 0.8f }, |
60 | 87 |
|
61 | | - image.Mutate(x => x |
62 | | - .BackgroundColor(Rgba32.Blue) |
63 | | - .Fill(color)); |
64 | | - image.Save($"{path}/Opacity.png"); |
| 88 | + { true, "Blue", 0.5f, PixelBlenderMode.Normal, 1.0f }, |
| 89 | + { true, "Blue", 1.0f, PixelBlenderMode.Normal, 0.5f }, |
| 90 | + { true, "Green", 0.5f, PixelBlenderMode.Normal, 0.3f }, |
| 91 | + { true, "HotPink", 0.8f, PixelBlenderMode.Normal, 0.8f }, |
65 | 92 |
|
66 | | - //shift background color towards forground color by the opacity amount |
67 | | - var mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); |
| 93 | + { true, "Blue", 0.5f, PixelBlenderMode.Multiply, 1.0f }, |
| 94 | + { true, "Blue", 1.0f, PixelBlenderMode.Multiply, 0.5f }, |
| 95 | + { true, "Green", 0.5f, PixelBlenderMode.Multiply, 0.3f }, |
| 96 | + { true, "HotPink", 0.8f, PixelBlenderMode.Multiply, 0.8f }, |
68 | 97 |
|
| 98 | + { true, "Blue", 0.5f, PixelBlenderMode.Add, 1.0f }, |
| 99 | + { true, "Blue", 1.0f, PixelBlenderMode.Add, 0.5f }, |
| 100 | + { true, "Green", 0.5f, PixelBlenderMode.Add, 0.3f }, |
| 101 | + { true, "HotPink", 0.8f, PixelBlenderMode.Add, 0.8f }, |
| 102 | + }; |
69 | 103 |
|
70 | | - using (PixelAccessor<Rgba32> sourcePixels = image.Lock()) |
| 104 | + [Theory] |
| 105 | + [WithSolidFilledImages(nameof(BlendData), 16, 16, "Red", PixelTypes.Rgba32)] |
| 106 | + public void BlendFillColorOverBackround<TPixel>( |
| 107 | + TestImageProvider<TPixel> provider, |
| 108 | + bool triggerFillRegion, |
| 109 | + string newColorName, |
| 110 | + float alpha, |
| 111 | + PixelBlenderMode blenderMode, |
| 112 | + float blendPercentage) |
| 113 | + where TPixel : struct, IPixel<TPixel> |
| 114 | + { |
| 115 | + var vec = TestUtils.GetPixelOfNamedColor<RgbaVector>(newColorName).ToVector4(); |
| 116 | + vec.W = alpha; |
| 117 | + |
| 118 | + TPixel fillColor = default; |
| 119 | + fillColor.PackFromVector4(vec); |
| 120 | + |
| 121 | + using (Image<TPixel> image = provider.GetImage()) |
| 122 | + { |
| 123 | + TPixel bgColor = image[0, 0]; |
| 124 | + |
| 125 | + var options = new GraphicsOptions(false) |
| 126 | + { |
| 127 | + BlenderMode = blenderMode, |
| 128 | + BlendPercentage = blendPercentage |
| 129 | + }; |
| 130 | + |
| 131 | + if (triggerFillRegion) |
| 132 | + { |
| 133 | + var region = new ShapeRegion(new RectangularPolygon(0, 0, 16, 16)); |
| 134 | + |
| 135 | + image.Mutate(c => c.Fill(options, new SolidBrush<TPixel>(fillColor), region)); |
| 136 | + } |
| 137 | + else |
71 | 138 | { |
72 | | - Assert.Equal(mergedColor, sourcePixels[9, 9]); |
73 | | - Assert.Equal(mergedColor, sourcePixels[199, 149]); |
| 139 | + image.Mutate(c => c.Fill(options, new SolidBrush<TPixel>(fillColor))); |
74 | 140 | } |
| 141 | + |
| 142 | + var testOutputDetails = new |
| 143 | + { |
| 144 | + triggerFillRegion = triggerFillRegion, |
| 145 | + newColorName = newColorName, |
| 146 | + alpha = alpha, |
| 147 | + blenderMode = blenderMode, |
| 148 | + blendPercentage = blendPercentage |
| 149 | + }; |
| 150 | + |
| 151 | + image.DebugSave( |
| 152 | + provider, |
| 153 | + testOutputDetails, |
| 154 | + appendPixelTypeToFileName: false, |
| 155 | + appendSourceFileOrDescription: false); |
| 156 | + |
| 157 | + PixelBlender<TPixel> blender = PixelOperations<TPixel>.Instance.GetPixelBlender(blenderMode); |
| 158 | + TPixel expectedPixel = blender.Blend(bgColor, fillColor, blendPercentage); |
| 159 | + |
| 160 | + image.ComparePixelBufferTo(expectedPixel); |
75 | 161 | } |
76 | 162 | } |
77 | | - |
78 | 163 | } |
79 | 164 | } |
0 commit comments