// ReSharper disable MemberCanBePrivate.Global // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global // ReSharper disable UnusedMember.Global using System; using System.Drawing; using CUE.NET.Devices.Generic; using CUE.NET.Gradients; using CUE.NET.Helper; namespace CUE.NET.Brushes { /// /// Represents a brush drawing a radial gradient around a center point. /// public class RadialGradientBrush : AbstractBrush, IGradientBrush { #region Properties & Fields /// /// Gets or sets the center point (as percentage in the range [0..1]) around which the brush should be drawn. /// public PointF Center { get; set; } = new PointF(0.5f, 0.5f); /// /// Gets or sets the gradient drawn by the brush. If null it will default to full transparent. /// public IGradient Gradient { get; set; } #endregion #region Constructors /// /// Initializes a new instance of the class. /// public RadialGradientBrush() { } /// /// Initializes a new instance of the class. /// /// The gradient drawn by the brush. public RadialGradientBrush(IGradient gradient) { this.Gradient = gradient; } /// /// Initializes a new instance of the class. /// /// The center point (as percentage in the range [0..1]). /// The gradient drawn by the brush. public RadialGradientBrush(PointF center, IGradient gradient) { this.Center = center; this.Gradient = gradient; } #endregion #region Methods /// /// Gets the color at an specific point assuming the brush is drawn into the given rectangle. /// /// The rectangle in which the brush should be drawn. /// The target (key/point) from which the color should be taken. /// The color at the specified point. protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget) { if(Gradient == null) return CorsairColor.Transparent; PointF centerPoint = new PointF(rectangle.X + rectangle.Width * Center.X, rectangle.Y + rectangle.Height * Center.Y); // Calculate the distance to the farthest point from the center as reference (this has to be a corner) // ReSharper disable once RedundantCast - never trust this ... float refDistance = (float)Math.Max(Math.Max(Math.Max(GradientHelper.CalculateDistance(rectangle.Location, centerPoint), GradientHelper.CalculateDistance(new PointF(rectangle.X + rectangle.Width, rectangle.Y), centerPoint)), GradientHelper.CalculateDistance(new PointF(rectangle.X, rectangle.Y + rectangle.Height), centerPoint)), GradientHelper.CalculateDistance(new PointF(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), centerPoint)); float distance = GradientHelper.CalculateDistance(renderTarget.Point, centerPoint); float offset = distance / refDistance; return Gradient.GetColor(offset); } #endregion } }