Skip to content

Commit 0659c58

Browse files
authored
Merge pull request #43 from SixLabors/sw/clear
'Clear()' image operation
2 parents 25c3921 + 8928ef6 commit 0659c58

68 files changed

Lines changed: 897 additions & 71 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using SixLabors.ImageSharp.Drawing.Processing.Processors.Drawing;
5+
using SixLabors.ImageSharp.Processing;
6+
7+
namespace SixLabors.ImageSharp.Drawing.Processing
8+
{
9+
/// <summary>
10+
/// Adds extensions that allow the clearing of regions with various brushes to the <see cref="Image{TPixel}"/> type.
11+
/// </summary>
12+
public static class ClearExtensions
13+
{
14+
/// <summary>
15+
/// Clones the graphic options and applies changes required to force clearing.
16+
/// </summary>
17+
/// <param name="options">The options to clone</param>
18+
/// <returns>A clone of option with ColorBlendingMode, AlphaCompositionMode, and BlendPercentage set</returns>
19+
internal static GraphicsOptions CloneForClearOperation(this GraphicsOptions options)
20+
{
21+
options = options.DeepClone();
22+
options.ColorBlendingMode = PixelFormats.PixelColorBlendingMode.Normal;
23+
options.AlphaCompositionMode = PixelFormats.PixelAlphaCompositionMode.Src;
24+
options.BlendPercentage = 1;
25+
return options;
26+
}
27+
28+
/// <summary>
29+
/// Flood fills the image with the specified brush without any blending.
30+
/// </summary>
31+
/// <param name="source">The image this method extends.</param>
32+
/// <param name="options">The graphics options.</param>
33+
/// <param name="brush">The details how to fill the region of interest.</param>
34+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
35+
public static IImageProcessingContext Clear(
36+
this IImageProcessingContext source,
37+
GraphicsOptions options,
38+
IBrush brush)
39+
=> source.Fill(options.CloneForClearOperation(), brush);
40+
41+
/// <summary>
42+
/// Flood fills the image with the specified brush without any blending.
43+
/// </summary>
44+
/// <param name="source">The image this method extends.</param>
45+
/// <param name="brush">The details how to fill the region of interest.</param>
46+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
47+
public static IImageProcessingContext Clear(this IImageProcessingContext source, IBrush brush) =>
48+
source.Clear(source.GetGraphicsOptions(), brush);
49+
50+
/// <summary>
51+
/// Flood fills the image with the specified color without any blending.
52+
/// </summary>
53+
/// <param name="source">The image this method extends.</param>
54+
/// <param name="options">The graphics options.</param>
55+
/// <param name="color">The color.</param>
56+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
57+
public static IImageProcessingContext Clear(
58+
this IImageProcessingContext source,
59+
GraphicsOptions options,
60+
Color color) =>
61+
source.Clear(options, new SolidBrush(color));
62+
63+
/// <summary>
64+
/// Flood fills the image with the specified color without any blending.
65+
/// </summary>
66+
/// <param name="source">The image this method extends.</param>
67+
/// <param name="color">The color.</param>
68+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
69+
public static IImageProcessingContext Clear(this IImageProcessingContext source, Color color) =>
70+
source.Clear(new SolidBrush(color));
71+
}
72+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using SixLabors.ImageSharp.Drawing.Processing.Processors.Drawing;
5+
using SixLabors.ImageSharp.Processing;
6+
7+
namespace SixLabors.ImageSharp.Drawing.Processing
8+
{
9+
/// <summary>
10+
/// Adds extensions that allow the filling of polygon outlines to the <see cref="Image{TPixel}"/> type.
11+
/// </summary>
12+
public static class ClearPathExtensions
13+
{
14+
/// <summary>
15+
/// Clones the shape graphic options and applies changes required to force clearing.
16+
/// </summary>
17+
/// <param name="shapeOptions">The options to clone</param>
18+
/// <returns>A clone of shapeOptions with ColorBlendingMode, AlphaCompositionMode, and BlendPercentage set</returns>
19+
internal static ShapeGraphicsOptions CloneForClearOperation(this ShapeGraphicsOptions shapeOptions)
20+
{
21+
var options = shapeOptions.GraphicsOptions.DeepClone();
22+
options.ColorBlendingMode = PixelFormats.PixelColorBlendingMode.Normal;
23+
options.AlphaCompositionMode = PixelFormats.PixelAlphaCompositionMode.Src;
24+
options.BlendPercentage = 1;
25+
26+
return new ShapeGraphicsOptions(options, shapeOptions.ShapeOptions);
27+
}
28+
29+
/// <summary>
30+
/// Flood fills the image in the shape of the provided polygon with the specified brush without any blending.
31+
/// </summary>
32+
/// <param name="source">The image this method extends.</param>
33+
/// <param name="options">The graphics options.</param>
34+
/// <param name="brush">The brush.</param>
35+
/// <param name="path">The shape.</param>
36+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
37+
public static IImageProcessingContext Clear(
38+
this IImageProcessingContext source,
39+
ShapeGraphicsOptions options,
40+
IBrush brush,
41+
IPath path) =>
42+
source.Fill(options.CloneForClearOperation(), brush, path);
43+
44+
/// <summary>
45+
/// Flood fills the image in the shape of the provided polygon with the specified brush without any blending.
46+
/// </summary>
47+
/// <param name="source">The image this method extends.</param>
48+
/// <param name="brush">The brush.</param>
49+
/// <param name="path">The path.</param>
50+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
51+
public static IImageProcessingContext Clear(this IImageProcessingContext source, IBrush brush, IPath path) =>
52+
source.Clear(source.GetShapeGraphicsOptions(), brush, path);
53+
54+
/// <summary>
55+
/// Flood fills the image in the shape of the provided polygon with the specified brush without any blending.
56+
/// </summary>
57+
/// <param name="source">The image this method extends.</param>
58+
/// <param name="options">The options.</param>
59+
/// <param name="color">The color.</param>
60+
/// <param name="path">The path.</param>
61+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
62+
public static IImageProcessingContext Clear(
63+
this IImageProcessingContext source,
64+
ShapeGraphicsOptions options,
65+
Color color,
66+
IPath path) =>
67+
source.Clear(options, new SolidBrush(color), path);
68+
69+
/// <summary>
70+
/// Flood fills the image in the shape of the provided polygon with the specified brush without any blending.
71+
/// </summary>
72+
/// <param name="source">The image this method extends.</param>
73+
/// <param name="color">The color.</param>
74+
/// <param name="path">The path.</param>
75+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
76+
public static IImageProcessingContext Clear(this IImageProcessingContext source, Color color, IPath path) =>
77+
source.Clear(new SolidBrush(color), path);
78+
}
79+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using SixLabors.ImageSharp.Processing;
5+
6+
namespace SixLabors.ImageSharp.Drawing.Processing
7+
{
8+
/// <summary>
9+
/// Adds extensions that allow the filling of rectangles to the <see cref="Image{TPixel}"/> type.
10+
/// </summary>
11+
public static class ClearRectangleExtensions
12+
{
13+
/// <summary>
14+
/// Flood fills the image in the shape of the provided rectangle with the specified brush without any blending.
15+
/// </summary>
16+
/// <param name="source">The image this method extends.</param>
17+
/// <param name="options">The options.</param>
18+
/// <param name="brush">The brush.</param>
19+
/// <param name="shape">The shape.</param>
20+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
21+
public static IImageProcessingContext Clear(
22+
this IImageProcessingContext source,
23+
ShapeGraphicsOptions options,
24+
IBrush brush,
25+
RectangleF shape) =>
26+
source.Clear(options, brush, new RectangularPolygon(shape.X, shape.Y, shape.Width, shape.Height));
27+
28+
/// <summary>
29+
/// Flood fills the image in the shape of the provided rectangle with the specified brush without any blending.
30+
/// </summary>
31+
/// <param name="source">The image this method extends.</param>
32+
/// <param name="brush">The brush.</param>
33+
/// <param name="shape">The shape.</param>
34+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
35+
public static IImageProcessingContext Clear(this IImageProcessingContext source, IBrush brush, RectangleF shape) =>
36+
source.Clear(brush, new RectangularPolygon(shape.X, shape.Y, shape.Width, shape.Height));
37+
38+
/// <summary>
39+
/// Flood fills the image in the shape of the provided rectangle with the specified brush without any blending.
40+
/// </summary>
41+
/// <param name="source">The image this method extends.</param>
42+
/// <param name="options">The options.</param>
43+
/// <param name="color">The color.</param>
44+
/// <param name="shape">The shape.</param>
45+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
46+
public static IImageProcessingContext Clear(
47+
this IImageProcessingContext source,
48+
ShapeGraphicsOptions options,
49+
Color color,
50+
RectangleF shape) =>
51+
source.Clear(options, new SolidBrush(color), shape);
52+
53+
/// <summary>
54+
/// Flood fills the image in the shape of the provided rectangle with the specified brush without any blending.
55+
/// </summary>
56+
/// <param name="source">The image this method extends.</param>
57+
/// <param name="color">The color.</param>
58+
/// <param name="shape">The shape.</param>
59+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
60+
public static IImageProcessingContext Clear(this IImageProcessingContext source, Color color, RectangleF shape) =>
61+
source.Clear(new SolidBrush(color), shape);
62+
}
63+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using SixLabors.ImageSharp.Drawing.Processing.Processors.Drawing;
5+
using SixLabors.ImageSharp.Processing;
6+
7+
namespace SixLabors.ImageSharp.Drawing.Processing
8+
{
9+
/// <summary>
10+
/// Adds extensions that allow the filling of regions with various brushes to the <see cref="Image{TPixel}"/> type.
11+
/// </summary>
12+
public static class ClearRegionExtensions
13+
{
14+
/// <summary>
15+
/// Flood fills the image with in the region with the specified brush without any blending.
16+
/// </summary>
17+
/// <param name="source">The image this method extends.</param>
18+
/// <param name="brush">The brush.</param>
19+
/// <param name="region">The region.</param>
20+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
21+
public static IImageProcessingContext Clear(this IImageProcessingContext source, IBrush brush, Region region) =>
22+
source.Clear(source.GetShapeGraphicsOptions(), brush, region);
23+
24+
/// <summary>
25+
/// Flood fills the image with in the region with the specified brush without any blending.
26+
/// </summary>
27+
/// <param name="source">The image this method extends.</param>
28+
/// <param name="options">The graphics options.</param>
29+
/// <param name="brush">The brush.</param>
30+
/// <param name="region">The region.</param>
31+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
32+
public static IImageProcessingContext Clear(
33+
this IImageProcessingContext source,
34+
ShapeGraphicsOptions options,
35+
IBrush brush,
36+
Region region) =>
37+
source.Fill(options.CloneForClearOperation(), brush, region);
38+
39+
/// <summary>
40+
/// Flood fills the image with in the region with the specified color without any blending.
41+
/// </summary>
42+
/// <param name="source">The image this method extends.</param>
43+
/// <param name="options">The options.</param>
44+
/// <param name="color">The color.</param>
45+
/// <param name="region">The region.</param>
46+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
47+
public static IImageProcessingContext Clear(
48+
this IImageProcessingContext source,
49+
ShapeGraphicsOptions options,
50+
Color color,
51+
Region region) =>
52+
source.Clear(options, new SolidBrush(color), region);
53+
54+
/// <summary>
55+
/// Flood fills the image with in the region with the specified color without any blending.
56+
/// </summary>
57+
/// <param name="source">The image this method extends.</param>
58+
/// <param name="color">The color.</param>
59+
/// <param name="region">The region.</param>
60+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
61+
public static IImageProcessingContext Clear(this IImageProcessingContext source, Color color, Region region) =>
62+
source.Clear(new SolidBrush(color), region);
63+
}
64+
}

src/ImageSharp.Drawing/Processing/Extensions/DrawBezierExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static class DrawBezierExtensions
1717
/// <param name="options">The options.</param>
1818
/// <param name="pen">The pen.</param>
1919
/// <param name="points">The points.</param>
20-
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
20+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
2121
public static IImageProcessingContext DrawBeziers(
2222
this IImageProcessingContext source,
2323
ShapeGraphicsOptions options,
@@ -31,7 +31,7 @@ public static IImageProcessingContext DrawBeziers(
3131
/// <param name="source">The image this method extends.</param>
3232
/// <param name="pen">The pen.</param>
3333
/// <param name="points">The points.</param>
34-
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
34+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
3535
public static IImageProcessingContext DrawBeziers(
3636
this IImageProcessingContext source,
3737
IPen pen,
@@ -46,7 +46,7 @@ public static IImageProcessingContext DrawBeziers(
4646
/// <param name="brush">The brush.</param>
4747
/// <param name="thickness">The thickness.</param>
4848
/// <param name="points">The points.</param>
49-
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
49+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
5050
public static IImageProcessingContext DrawBeziers(
5151
this IImageProcessingContext source,
5252
ShapeGraphicsOptions options,
@@ -62,7 +62,7 @@ public static IImageProcessingContext DrawBeziers(
6262
/// <param name="brush">The brush.</param>
6363
/// <param name="thickness">The thickness.</param>
6464
/// <param name="points">The points.</param>
65-
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
65+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
6666
public static IImageProcessingContext DrawBeziers(
6767
this IImageProcessingContext source,
6868
IBrush brush,
@@ -77,7 +77,7 @@ public static IImageProcessingContext DrawBeziers(
7777
/// <param name="color">The color.</param>
7878
/// <param name="thickness">The thickness.</param>
7979
/// <param name="points">The points.</param>
80-
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
80+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
8181
public static IImageProcessingContext DrawBeziers(
8282
this IImageProcessingContext source,
8383
Color color,
@@ -93,7 +93,7 @@ public static IImageProcessingContext DrawBeziers(
9393
/// <param name="color">The color.</param>
9494
/// <param name="thickness">The thickness.</param>
9595
/// <param name="points">The points.</param>
96-
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
96+
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
9797
public static IImageProcessingContext DrawBeziers(
9898
this IImageProcessingContext source,
9999
ShapeGraphicsOptions options,

0 commit comments

Comments
 (0)