Skip to content

Commit 4720c39

Browse files
committed
Add 'Clear' extensions matching Fill
1 parent 655d629 commit 4720c39

55 files changed

Lines changed: 832 additions & 7 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 graphicoptions and applies changes required to force clearing.
16+
/// </summary>
17+
/// <param name="options">The optinos 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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</returns>
69+
public static IImageProcessingContext Clear(this IImageProcessingContext source, Color color) =>
70+
source.Clear(new SolidBrush(color));
71+
}
72+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
{/// <summary>
14+
/// Clones the graphicoptions and applies changes required to force clearing.
15+
/// </summary>
16+
/// <param name="shapeOptions">The optinos to clone</param>
17+
/// <returns>A clone of shapeOptions with ColorBlendingMode, AlphaCompositionMode, and BlendPercentage set</returns>
18+
internal static ShapeGraphicsOptions CloneForClearOperation(this ShapeGraphicsOptions shapeOptions)
19+
{
20+
var options = shapeOptions.GraphicsOptions.DeepClone();
21+
options.ColorBlendingMode = PixelFormats.PixelColorBlendingMode.Normal;
22+
options.AlphaCompositionMode = PixelFormats.PixelAlphaCompositionMode.Src;
23+
options.BlendPercentage = 1;
24+
25+
return new ShapeGraphicsOptions(options, shapeOptions.ShapeOptions);
26+
}
27+
28+
/// <summary>
29+
/// Flood fills the image in the shape of the provided polygon 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 brush.</param>
34+
/// <param name="path">The shape.</param>
35+
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
36+
public static IImageProcessingContext Clear(
37+
this IImageProcessingContext source,
38+
ShapeGraphicsOptions options,
39+
IBrush brush,
40+
IPath path) =>
41+
source.Fill(options.CloneForClearOperation(), brush, path);
42+
43+
/// <summary>
44+
/// Flood fills the image in the shape of the provided polygon with the specified brush without any blending.
45+
/// </summary>
46+
/// <param name="source">The image this method extends.</param>
47+
/// <param name="brush">The brush.</param>
48+
/// <param name="path">The path.</param>
49+
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
50+
public static IImageProcessingContext Clear(this IImageProcessingContext source, IBrush brush, IPath path) =>
51+
source.Clear(source.GetShapeGraphicsOptions(), brush, path);
52+
53+
/// <summary>
54+
/// Flood fills the image in the shape of the provided polygon with the specified brush without any blending.
55+
/// </summary>
56+
/// <param name="source">The image this method extends.</param>
57+
/// <param name="options">The options.</param>
58+
/// <param name="color">The color.</param>
59+
/// <param name="path">The path.</param>
60+
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
61+
public static IImageProcessingContext Clear(
62+
this IImageProcessingContext source,
63+
ShapeGraphicsOptions options,
64+
Color color,
65+
IPath path) =>
66+
source.Clear(options, new SolidBrush(color), path);
67+
68+
/// <summary>
69+
/// Flood fills the image in the shape of the provided polygon with the specified brush without any blending.
70+
/// </summary>
71+
/// <param name="source">The image this method extends.</param>
72+
/// <param name="color">The color.</param>
73+
/// <param name="path">The path.</param>
74+
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
75+
public static IImageProcessingContext Clear(this IImageProcessingContext source, Color color, IPath path) =>
76+
source.Clear(new SolidBrush(color), path);
77+
}
78+
}
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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</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="Image{TPixel}"/>.</returns>
61+
public static IImageProcessingContext Clear(this IImageProcessingContext source, Color color, Region region) =>
62+
source.Clear(new SolidBrush(color), region);
63+
}
64+
}

0 commit comments

Comments
 (0)