// ReSharper disable CollectionNeverUpdated.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable MemberCanBeProtected.Global
// ReSharper disable ReturnTypeCanBeEnumerable.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
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 linear gradient.
///
public class LinearGradientBrush : AbstractBrush, IGradientBrush
{
#region Properties & Fields
///
/// Gets or sets the start point (as percentage in the range [0..1]) of the gradient drawn by the brush. (default: 0f, 0.5f)
///
public PointF StartPoint { get; set; } = new PointF(0f, 0.5f);
///
/// Gets or sets the end point (as percentage in the range [0..1]) of the gradient drawn by the brush. (default: 1f, 0.5f)
///
public PointF EndPoint { get; set; } = new PointF(1f, 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 Constructor
///
/// Initializes a new instance of the class.
///
public LinearGradientBrush()
{ }
///
/// Initializes a new instance of the class.
///
/// The gradient drawn by the brush.
public LinearGradientBrush(IGradient gradient)
{
this.Gradient = gradient;
}
///
/// Initializes a new instance of the class.
///
/// The start point (as percentage in the range [0..1]).
/// The end point (as percentage in the range [0..1]).
/// The gradient drawn by the brush.
public LinearGradientBrush(PointF startPoint, PointF endPoint, IGradient gradient)
{
this.StartPoint = startPoint;
this.EndPoint = endPoint;
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 startPoint = new PointF(StartPoint.X * rectangle.Width, StartPoint.Y * rectangle.Height);
PointF endPoint = new PointF(EndPoint.X * rectangle.Width, EndPoint.Y * rectangle.Height);
float offset = GradientHelper.CalculateLinearGradientOffset(startPoint, endPoint, renderTarget.Point);
return Gradient.GetColor(offset);
}
#endregion
}
}