// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Drawing.Brushes { using System; using System.Numerics; using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; using SixLabors.Primitives; /// /// Provides an implementation of a solid brush for painting solid color areas. /// /// The pixel format. public class SolidBrush : IBrush where TPixel : struct, IPixel { /// /// The color to paint. /// private readonly TPixel color; /// /// Initializes a new instance of the class. /// /// The color. public SolidBrush(TPixel color) { this.color = color; } /// /// Gets the color. /// /// /// The color. /// public TPixel Color => this.color; /// public BrushApplicator CreateApplicator(ImageBase source, RectangleF region, GraphicsOptions options) { return new SolidBrushApplicator(source, this.color, options); } /// /// The solid brush applicator. /// private class SolidBrushApplicator : BrushApplicator { /// /// Initializes a new instance of the class. /// /// The source image. /// The color. /// The options public SolidBrushApplicator(ImageBase source, TPixel color, GraphicsOptions options) : base(source, options) { this.Colors = new Buffer(source.Width); for (int i = 0; i < this.Colors.Length; i++) { this.Colors[i] = color; } } /// /// Gets the colors. /// protected Buffer Colors { get; } /// /// Gets the color for a single pixel. /// /// The x. /// The y. /// /// The color /// internal override TPixel this[int x, int y] => this.Colors[x]; /// public override void Dispose() { this.Colors.Dispose(); } /// internal override void Apply(Span scanline, int x, int y) { Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); using (var amountBuffer = new Buffer(scanline.Length)) { for (int i = 0; i < scanline.Length; i++) { amountBuffer[i] = scanline[i] * this.Options.BlendPercentage; } this.Blender.Blend(destinationRow, destinationRow, this.Colors, amountBuffer); } } } } }