// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable MemberCanBeProtected.Global
// ReSharper disable ReturnTypeCanBeEnumerable.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Drawing;
using CUE.NET.Devices.Generic;
using CUE.NET.Gradients;
namespace CUE.NET.Brushes
{
///
/// Represents a brush drawing a conical gradient.
///
public class ConicalGradientBrush : AbstractBrush, IGradientBrush
{
#region Properties & Fields
///
/// Gets or sets the origin (radian-angle) the brush is drawn to. (default: -π/2)
///
public float Origin { get; set; } = (float)Math.Atan2(-1, 0);
///
/// Gets or sets the center point (as percentage in the range [0..1]) of the gradient drawn by the brush. (default: 0.5f, 0.5f)
///
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 ConicalGradientBrush()
{ }
///
/// Initializes a new instance of the class.
///
/// The gradient drawn by the brush.
public ConicalGradientBrush(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 ConicalGradientBrush(PointF center, IGradient gradient)
{
this.Center = center;
this.Gradient = gradient;
}
///
/// Initializes a new instance of the class.
///
/// The center point (as percentage in the range [0..1]).
/// The origin (radian-angle) the brush is drawn to.
/// The gradient drawn by the brush.
public ConicalGradientBrush(PointF center, float origin, IGradient gradient)
{
this.Center = center;
this.Origin = origin;
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)
{
float centerX = rectangle.Width * Center.X;
float centerY = rectangle.Height * Center.Y;
double angle = Math.Atan2(renderTarget.Point.Y - centerY, renderTarget.Point.X - centerX) - Origin;
if (angle < 0) angle += Math.PI * 2;
float offset = (float)(angle / (Math.PI * 2));
return Gradient.GetColor(offset);
}
#endregion
}
}