// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using RGB.NET.Core;
using RGB.NET.Presets.Helper;
using RGB.NET.Presets.Textures.Gradients;
namespace RGB.NET.Presets.Textures;
///
///
/// Represents a texture drawing a radial gradient around a center point.
///
public sealed class RadialGradientTexture : AbstractGradientTexture
{
#region Properties & Fields
private float _referenceDistance = GradientHelper.CalculateDistance(new Point(0.5f, 0.5f), new Point(0, 0));
private Point _center = new(0.5f, 0.5f);
///
/// Gets or sets the center (as percentage in the range [0..1]) around which the should be drawn. (default: 0.5, 0.5)
///
public Point Center
{
get => _center;
set
{
if (SetProperty(ref _center, value))
CalculateReferenceDistance();
}
}
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The size of the texture.
/// The gradient drawn by the brush.
public RadialGradientTexture(Size size, IGradient gradient)
: base(size, gradient)
{ }
///
/// Initializes a new instance of the class.
///
/// The size of the texture.
/// The gradient drawn by the brush.
/// The center point (as percentage in the range [0..1]).
public RadialGradientTexture(Size size, IGradient gradient, Point center)
: base(size, gradient)
{
this.Center = center;
}
#endregion
#region Methods
private void CalculateReferenceDistance()
{
float referenceX = Center.X < 0.5f ? 1 : 0;
float referenceY = Center.Y < 0.5f ? 1 : 0;
_referenceDistance = GradientHelper.CalculateDistance(new Point(referenceX, referenceY), Center);
}
///
protected override Color GetColor(Point point)
{
float distance = GradientHelper.CalculateDistance(point, Center);
float offset = distance / _referenceDistance;
return Gradient.GetColor(offset);
}
#endregion
}