mirror of https://github.com/SixLabors/ImageSharp
21 changed files with 711 additions and 603 deletions
@ -0,0 +1,166 @@ |
|||||
|
// <copyright file="Brushes`2.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Drawing.Brushes |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// A collection of methods for creating generic brushes.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
/// <returns>A Brush</returns>
|
||||
|
public class Brushes<TColor, TPacked> |
||||
|
where TColor : struct, IPackedPixel<TPacked> |
||||
|
where TPacked : struct |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Percent10 Hatch Pattern
|
||||
|
/// </summary>
|
||||
|
/// note 2d arrays when configured using initalizer look inverted
|
||||
|
/// ---> Y axis
|
||||
|
/// ^
|
||||
|
/// | X - axis
|
||||
|
/// |
|
||||
|
/// see PatternBrush for details about how to make new patterns work
|
||||
|
private static readonly bool[,] Percent10Pattern = |
||||
|
{ |
||||
|
{ true, false, false, false }, |
||||
|
{ false, false, false, false }, |
||||
|
{ false, false, true, false }, |
||||
|
{ false, false, false, false } |
||||
|
}; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Percent20 pattern.
|
||||
|
/// </summary>
|
||||
|
private static readonly bool[,] Percent20Pattern = |
||||
|
{ |
||||
|
{ true, false, true, false }, |
||||
|
{ false, false, false, false }, |
||||
|
{ false, true, false, true }, |
||||
|
{ false, false, false, false } |
||||
|
}; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Horizontal Hatch Pattern
|
||||
|
/// </summary>
|
||||
|
private static readonly bool[,] HorizontalPattern = |
||||
|
{ |
||||
|
{ false, true, false, false }, |
||||
|
}; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Min Pattern
|
||||
|
/// </summary>
|
||||
|
private static readonly bool[,] MinPattern = |
||||
|
{ |
||||
|
{ false, false, false, true }, |
||||
|
}; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Vertical Pattern
|
||||
|
/// </summary>
|
||||
|
private static readonly bool[,] VerticalPattern = |
||||
|
{ |
||||
|
{ false }, |
||||
|
{ true }, |
||||
|
{ false }, |
||||
|
{ false } |
||||
|
}; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Forward Diagonal Pattern
|
||||
|
/// </summary>
|
||||
|
private static readonly bool[,] ForwardDiagonalPattern = |
||||
|
{ |
||||
|
{ true, false, false, false }, |
||||
|
{ false, true, false, false }, |
||||
|
{ false, false, true, false }, |
||||
|
{ false, false, false, true } |
||||
|
}; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Backward Diagonal Pattern
|
||||
|
/// </summary>
|
||||
|
private static readonly bool[,] BackwardDiagonalPattern = |
||||
|
{ |
||||
|
{ false, false, false, true }, |
||||
|
{ false, false, true, false }, |
||||
|
{ false, true, false, false }, |
||||
|
{ true, false, false, false } |
||||
|
}; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Create as brush that will paint a solid color
|
||||
|
/// </summary>
|
||||
|
/// <param name="color">The color.</param>
|
||||
|
/// <returns>A Brush</returns>
|
||||
|
public static SolidBrush<TColor, TPacked> Solid(TColor color) |
||||
|
=> new SolidBrush<TColor, TPacked>(color); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Create as brush that will paint a Percent10 Hatch Pattern within the specified colors
|
||||
|
/// </summary>
|
||||
|
/// <param name="foreColor">Color of the foreground.</param>
|
||||
|
/// <param name="backColor">Color of the background.</param>
|
||||
|
/// <returns>A Brush</returns>
|
||||
|
public static PatternBrush<TColor, TPacked> Percent10(TColor foreColor, TColor backColor) |
||||
|
=> new PatternBrush<TColor, TPacked>(foreColor, backColor, Percent10Pattern); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Create as brush that will paint a Percent20 Hatch Pattern within the specified colors
|
||||
|
/// </summary>
|
||||
|
/// <param name="foreColor">Color of the foreground.</param>
|
||||
|
/// <param name="backColor">Color of the background.</param>
|
||||
|
/// <returns>A Brush</returns>
|
||||
|
public static PatternBrush<TColor, TPacked> Percent20(TColor foreColor, TColor backColor) |
||||
|
=> new PatternBrush<TColor, TPacked>(foreColor, backColor, Percent20Pattern); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Create as brush that will paint a Horizontal Hatch Pattern within the specified colors
|
||||
|
/// </summary>
|
||||
|
/// <param name="foreColor">Color of the foreground.</param>
|
||||
|
/// <param name="backColor">Color of the background.</param>
|
||||
|
/// <returns>A Brush</returns>
|
||||
|
public static PatternBrush<TColor, TPacked> Horizontal(TColor foreColor, TColor backColor) |
||||
|
=> new PatternBrush<TColor, TPacked>(foreColor, backColor, HorizontalPattern); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Create as brush that will paint a Min Hatch Pattern within the specified colors
|
||||
|
/// </summary>
|
||||
|
/// <param name="foreColor">Color of the foreground.</param>
|
||||
|
/// <param name="backColor">Color of the background.</param>
|
||||
|
/// <returns>A Brush</returns>
|
||||
|
public static PatternBrush<TColor, TPacked> Min(TColor foreColor, TColor backColor) |
||||
|
=> new PatternBrush<TColor, TPacked>(foreColor, backColor, MinPattern); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Create as brush that will paint a Vertical Hatch Pattern within the specified colors
|
||||
|
/// </summary>
|
||||
|
/// <param name="foreColor">Color of the foreground.</param>
|
||||
|
/// <param name="backColor">Color of the background.</param>
|
||||
|
/// <returns>A Brush</returns>
|
||||
|
public static PatternBrush<TColor, TPacked> Vertical(TColor foreColor, TColor backColor) |
||||
|
=> new PatternBrush<TColor, TPacked>(foreColor, backColor, VerticalPattern); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Create as brush that will paint a Forward Diagonal Hatch Pattern within the specified colors
|
||||
|
/// </summary>
|
||||
|
/// <param name="foreColor">Color of the foreground.</param>
|
||||
|
/// <param name="backColor">Color of the background.</param>
|
||||
|
/// <returns>A Brush</returns>
|
||||
|
public static PatternBrush<TColor, TPacked> ForwardDiagonal(TColor foreColor, TColor backColor) |
||||
|
=> new PatternBrush<TColor, TPacked>(foreColor, backColor, ForwardDiagonalPattern); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Create as brush that will paint a Backward Diagonal Hatch Pattern within the specified colors
|
||||
|
/// </summary>
|
||||
|
/// <param name="foreColor">Color of the foreground.</param>
|
||||
|
/// <param name="backColor">Color of the background.</param>
|
||||
|
/// <returns>A Brush</returns>
|
||||
|
public static PatternBrush<TColor, TPacked> BackwardDiagonal(TColor foreColor, TColor backColor) |
||||
|
=> new PatternBrush<TColor, TPacked>(foreColor, backColor, BackwardDiagonalPattern); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,108 @@ |
|||||
|
// <copyright file="ImageBrush`2.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Drawing.Brushes |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Numerics; |
||||
|
|
||||
|
using Processors; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Provides an implementation of an image brush for painting images within areas.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
public class ImageBrush<TColor, TPacked> : IBrush<TColor, TPacked> |
||||
|
where TColor : struct, IPackedPixel<TPacked> |
||||
|
where TPacked : struct |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The image to paint.
|
||||
|
/// </summary>
|
||||
|
private readonly IImageBase<TColor, TPacked> image; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="ImageBrush{TColor,TPacked}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="image">The image.</param>
|
||||
|
public ImageBrush(IImageBase<TColor, TPacked> image) |
||||
|
{ |
||||
|
this.image = image; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
public IBrushApplicator<TColor, TPacked> CreateApplicator(RectangleF region) |
||||
|
{ |
||||
|
return new ImageBrushApplicator(this.image, region); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The image brush applicator.
|
||||
|
/// </summary>
|
||||
|
private class ImageBrushApplicator : IBrushApplicator<TColor, TPacked> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The source pixel accessor.
|
||||
|
/// </summary>
|
||||
|
private readonly PixelAccessor<TColor, TPacked> source; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The y-length.
|
||||
|
/// </summary>
|
||||
|
private readonly int yLength; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The x-length.
|
||||
|
/// </summary>
|
||||
|
private readonly int xLength; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The offset.
|
||||
|
/// </summary>
|
||||
|
private readonly Vector2 offset; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="ImageBrushApplicator"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="image">
|
||||
|
/// The image.
|
||||
|
/// </param>
|
||||
|
/// <param name="region">
|
||||
|
/// The region.
|
||||
|
/// </param>
|
||||
|
public ImageBrushApplicator(IImageBase<TColor, TPacked> image, RectangleF region) |
||||
|
{ |
||||
|
this.source = image.Lock(); |
||||
|
this.xLength = image.Width; |
||||
|
this.yLength = image.Height; |
||||
|
this.offset = new Vector2((float)Math.Max(Math.Floor(region.Top), 0), (float)Math.Max(Math.Floor(region.Left), 0)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the color for a single pixel.
|
||||
|
/// </summary>
|
||||
|
/// <param name="point">The point.</param>
|
||||
|
/// <returns>
|
||||
|
/// The color
|
||||
|
/// </returns>
|
||||
|
public TColor GetColor(Vector2 point) |
||||
|
{ |
||||
|
// Offset the requested pixel by the value in the rectangle (the shapes position)
|
||||
|
point = point - this.offset; |
||||
|
int x = (int)point.X % this.xLength; |
||||
|
int y = (int)point.Y % this.yLength; |
||||
|
|
||||
|
return this.source[x, y]; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
public void Dispose() |
||||
|
{ |
||||
|
this.source.Dispose(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,158 @@ |
|||||
|
// <copyright file="PatternBrush`2.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Drawing.Brushes |
||||
|
{ |
||||
|
using System.Numerics; |
||||
|
|
||||
|
using Processors; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Provides an implementation of a pattern brush for painting patterns.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// The patterns that are used to create a custom pattern brush are made up of a repeating matrix of flags,
|
||||
|
/// where each flag denotes whether to draw the foreground color or the background color.
|
||||
|
/// so to create a new bool[,] with your flags
|
||||
|
/// <para>
|
||||
|
/// For example if you wanted to create a diagonal line that repeat every 4 pixels you would use a pattern like so
|
||||
|
/// 1000
|
||||
|
/// 0100
|
||||
|
/// 0010
|
||||
|
/// 0001
|
||||
|
/// </para>
|
||||
|
/// <para>
|
||||
|
/// or you want a horizontal stripe which is 3 pixels apart you would use a pattern like
|
||||
|
/// 1
|
||||
|
/// 0
|
||||
|
/// 0
|
||||
|
/// </para>
|
||||
|
/// Warning when use array initializer across multiple lines the bools look inverted i.e.
|
||||
|
/// new bool[,]{
|
||||
|
/// {true, false, false},
|
||||
|
/// {false,true, false}
|
||||
|
/// }
|
||||
|
/// would be
|
||||
|
/// 10
|
||||
|
/// 01
|
||||
|
/// 00
|
||||
|
/// </remarks>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
public class PatternBrush<TColor, TPacked> : IBrush<TColor, TPacked> |
||||
|
where TColor : struct, IPackedPixel<TPacked> |
||||
|
where TPacked : struct |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The pattern.
|
||||
|
/// </summary>
|
||||
|
private readonly TColor[][] pattern; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The stride width.
|
||||
|
/// </summary>
|
||||
|
private readonly int stride; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="PatternBrush{TColor, TPacked}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="foreColor">Color of the fore.</param>
|
||||
|
/// <param name="backColor">Color of the back.</param>
|
||||
|
/// <param name="pattern">The pattern.</param>
|
||||
|
public PatternBrush(TColor foreColor, TColor backColor, bool[,] pattern) |
||||
|
{ |
||||
|
this.stride = pattern.GetLength(1); |
||||
|
|
||||
|
// Convert the multidimension array into a jagged one.
|
||||
|
int height = pattern.GetLength(0); |
||||
|
this.pattern = new TColor[height][]; |
||||
|
for (int x = 0; x < height; x++) |
||||
|
{ |
||||
|
this.pattern[x] = new TColor[this.stride]; |
||||
|
for (int y = 0; y < this.stride; y++) |
||||
|
{ |
||||
|
if (pattern[x, y]) |
||||
|
{ |
||||
|
this.pattern[x][y] = foreColor; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
this.pattern[x][y] = backColor; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="PatternBrush{TColor, TPacked}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="brush">The brush.</param>
|
||||
|
internal PatternBrush(PatternBrush<TColor, TPacked> brush) |
||||
|
{ |
||||
|
this.pattern = brush.pattern; |
||||
|
this.stride = brush.stride; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
public IBrushApplicator<TColor, TPacked> CreateApplicator(RectangleF region) |
||||
|
{ |
||||
|
return new PatternBrushApplicator(this.pattern, this.stride); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The pattern brush applicator.
|
||||
|
/// </summary>
|
||||
|
private class PatternBrushApplicator : IBrushApplicator<TColor, TPacked> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The patter x-length.
|
||||
|
/// </summary>
|
||||
|
private readonly int xLength; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The stride width.
|
||||
|
/// </summary>
|
||||
|
private readonly int stride; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The pattern.
|
||||
|
/// </summary>
|
||||
|
private readonly TColor[][] pattern; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="PatternBrushApplicator" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="pattern">The pattern.</param>
|
||||
|
/// <param name="stride">The stride.</param>
|
||||
|
public PatternBrushApplicator(TColor[][] pattern, int stride) |
||||
|
{ |
||||
|
this.pattern = pattern; |
||||
|
this.xLength = pattern.Length; |
||||
|
this.stride = stride; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the color for a single pixel.
|
||||
|
/// </summary>
|
||||
|
/// <param name="point">The point.</param>
|
||||
|
/// <returns>
|
||||
|
/// The color
|
||||
|
/// </returns>
|
||||
|
public TColor GetColor(Vector2 point) |
||||
|
{ |
||||
|
int x = (int)point.X % this.xLength; |
||||
|
int y = (int)point.Y % this.stride; |
||||
|
|
||||
|
return this.pattern[x][y]; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
public void Dispose() |
||||
|
{ |
||||
|
// noop
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,87 @@ |
|||||
|
// <copyright file="SolidBrush`2.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Drawing.Brushes |
||||
|
{ |
||||
|
using System.Numerics; |
||||
|
|
||||
|
using Processors; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Provides an implementation of a solid brush for painting solid color areas.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TColor">The pixel format.</typeparam>
|
||||
|
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
|
||||
|
public class SolidBrush<TColor, TPacked> : IBrush<TColor, TPacked> |
||||
|
where TColor : struct, IPackedPixel<TPacked> |
||||
|
where TPacked : struct |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The color to paint.
|
||||
|
/// </summary>
|
||||
|
private readonly TColor color; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="SolidBrush{TColor, TPacked}"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="color">The color.</param>
|
||||
|
public SolidBrush(TColor color) |
||||
|
{ |
||||
|
this.color = color; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the color.
|
||||
|
/// </summary>
|
||||
|
/// <value>
|
||||
|
/// The color.
|
||||
|
/// </value>
|
||||
|
public TColor Color => this.color; |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
public IBrushApplicator<TColor, TPacked> CreateApplicator(RectangleF region) |
||||
|
{ |
||||
|
return new SolidBrushApplicator(this.color); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The solid brush applicator.
|
||||
|
/// </summary>
|
||||
|
private class SolidBrushApplicator : IBrushApplicator<TColor, TPacked> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The solid color.
|
||||
|
/// </summary>
|
||||
|
private readonly TColor color; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="SolidBrushApplicator"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="color">The color.</param>
|
||||
|
public SolidBrushApplicator(TColor color) |
||||
|
{ |
||||
|
this.color = color; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the color for a single pixel.
|
||||
|
/// </summary>
|
||||
|
/// <param name="point">The point.</param>
|
||||
|
/// <returns>
|
||||
|
/// The color
|
||||
|
/// </returns>
|
||||
|
public TColor GetColor(Vector2 point) |
||||
|
{ |
||||
|
return this.color; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
public void Dispose() |
||||
|
{ |
||||
|
// noop
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue