Changed all namespaces to file-scope

This commit is contained in:
DarthAffe committed 2021-11-13 17:35:08 +01:00
1 parent a0c6ccebea
commit 30624035f1
353 files changed
+35775 -36129

No files matched your search

@@ -1,57 +1,56 @@
using RGB.NET.Core;
using RGB.NET.Presets.Textures.Gradients;
namespace RGB.NET.Presets.Textures
namespace RGB.NET.Presets.Textures;
/// <inheritdoc cref="AbstractBindable" />
/// <inheritdoc cref="ITexture" />
/// <summary>
/// Represents a abstract texture containing a color-gradient.
/// </summary>
public abstract class AbstractGradientTexture : AbstractBindable, ITexture
{
/// <inheritdoc cref="AbstractBindable" />
/// <inheritdoc cref="ITexture" />
#region Properties & Fields
/// <summary>
/// Represents a abstract texture containing a color-gradient.
/// Gets the gradient used to generate this texture.
/// </summary>
public abstract class AbstractGradientTexture : AbstractBindable, ITexture
public IGradient Gradient { get; }
/// <inheritdoc />
public Size Size { get; }
/// <inheritdoc />
public Color this[in Point point] => GetColor(point);
/// <inheritdoc />
public Color this[in Rectangle rectangle] => GetColor(rectangle.Center);
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The gradient used to generate this texture.</param>
protected AbstractGradientTexture(Size size, IGradient gradient)
{
#region Properties & Fields
/// <summary>
/// Gets the gradient used to generate this texture.
/// </summary>
public IGradient Gradient { get; }
/// <inheritdoc />
public Size Size { get; }
/// <inheritdoc />
public Color this[in Point point] => GetColor(point);
/// <inheritdoc />
public Color this[in Rectangle rectangle] => GetColor(rectangle.Center);
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The gradient used to generate this texture.</param>
protected AbstractGradientTexture(Size size, IGradient gradient)
{
this.Size = size;
this.Gradient = gradient;
}
#endregion
#region Methods
/// <summary>
/// Gets the color at the specified location of the texture.
/// </summary>
/// <param name="point">The location to get the color from.</param>
/// <returns>The color at the specified location.</returns>
protected abstract Color GetColor(in Point point);
#endregion
this.Size = size;
this.Gradient = gradient;
}
}
#endregion
#region Methods
/// <summary>
/// Gets the color at the specified location of the texture.
/// </summary>
/// <param name="point">The location to get the color from.</param>
/// <returns>The color at the specified location.</returns>
protected abstract Color GetColor(in Point point);
#endregion
}
+63 -64
View File
@@ -2,73 +2,72 @@
using RGB.NET.Core;
using RGB.NET.Presets.Textures.Sampler;
namespace RGB.NET.Presets.Textures
namespace RGB.NET.Presets.Textures;
/// <inheritdoc />
/// <summary>
/// Represents a texture of byte-data pixels.
/// </summary>
public sealed class BytePixelTexture : PixelTexture<byte>
{
/// <inheritdoc />
#region Properties & Fields
private readonly byte[] _data;
/// <inheritdoc />
protected override ReadOnlySpan<byte> Data => _data;
/// <summary>
/// Represents a texture of byte-data pixels.
/// Gets the color format the data is in.
/// </summary>
public sealed class BytePixelTexture : PixelTexture<byte>
public ColorFormat ColorFormat { get; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="BytePixelTexture" /> class.
/// A <see cref="AverageColorSampler"/> is used.
/// </summary>
/// <param name="with">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="data">The pixel-data of the texture.</param>
/// <param name="colorFormat">The color format the data is in. (default: RGB)</param>
public BytePixelTexture(int with, int height, byte[] data, ColorFormat colorFormat = ColorFormat.RGB)
: this(with, height, data, new AverageByteSampler(), colorFormat)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="BytePixelTexture" /> class.
/// </summary>
/// <param name="with">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="data">The pixel-data of the texture.</param>
/// <param name="sampler">The sampler used to get the color of a region.</param>
/// <param name="colorFormat">The color format the data is in. (default: RGB)</param>
public BytePixelTexture(int with, int height, byte[] data, ISampler<byte> sampler, ColorFormat colorFormat = ColorFormat.RGB)
: base(with, height, 3, sampler)
{
#region Properties & Fields
this._data = data;
this.ColorFormat = colorFormat;
private readonly byte[] _data;
/// <inheritdoc />
protected override ReadOnlySpan<byte> Data => _data;
/// <summary>
/// Gets the color format the data is in.
/// </summary>
public ColorFormat ColorFormat { get; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="BytePixelTexture" /> class.
/// A <see cref="AverageColorSampler"/> is used.
/// </summary>
/// <param name="with">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="data">The pixel-data of the texture.</param>
/// <param name="colorFormat">The color format the data is in. (default: RGB)</param>
public BytePixelTexture(int with, int height, byte[] data, ColorFormat colorFormat = ColorFormat.RGB)
: this(with, height, data, new AverageByteSampler(), colorFormat)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="BytePixelTexture" /> class.
/// </summary>
/// <param name="with">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="data">The pixel-data of the texture.</param>
/// <param name="sampler">The sampler used to get the color of a region.</param>
/// <param name="colorFormat">The color format the data is in. (default: RGB)</param>
public BytePixelTexture(int with, int height, byte[] data, ISampler<byte> sampler, ColorFormat colorFormat = ColorFormat.RGB)
: base(with, height, 3, sampler)
{
this._data = data;
this.ColorFormat = colorFormat;
if (Data.Length != ((with * height) * 3)) throw new ArgumentException($"Data-Length {Data.Length} differs from the specified size {with}x{height} * 3 bytes ({with * height * 3}).");
}
#endregion
#region Methods
/// <inheritdoc />
protected override Color GetColor(in ReadOnlySpan<byte> pixel)
{
return ColorFormat switch
{
ColorFormat.RGB => new Color(pixel[0], pixel[1], pixel[2]),
ColorFormat.BGR => new Color(pixel[2], pixel[1], pixel[0]),
_ => throw new ArgumentOutOfRangeException()
};
}
#endregion
if (Data.Length != ((with * height) * 3)) throw new ArgumentException($"Data-Length {Data.Length} differs from the specified size {with}x{height} * 3 bytes ({with * height * 3}).");
}
}
#endregion
#region Methods
/// <inheritdoc />
protected override Color GetColor(in ReadOnlySpan<byte> pixel)
{
return ColorFormat switch
{
ColorFormat.RGB => new Color(pixel[0], pixel[1], pixel[2]),
ColorFormat.BGR => new Color(pixel[2], pixel[1], pixel[0]),
_ => throw new ArgumentOutOfRangeException()
};
}
#endregion
}
@@ -8,95 +8,94 @@ using System;
using RGB.NET.Core;
using RGB.NET.Presets.Textures.Gradients;
namespace RGB.NET.Presets.Textures
namespace RGB.NET.Presets.Textures;
/// <inheritdoc />
/// <summary>
/// Represents a texture drawing a conical gradient.
/// </summary>
public sealed class ConicalGradientTexture : AbstractGradientTexture
{
/// <inheritdoc />
#region Constants
private const float PI2 = MathF.PI * 2.0f;
#endregion
#region Properties & Fields
private float _origin = MathF.Atan2(-1, 0);
/// <summary>
/// Represents a texture drawing a conical gradient.
/// Gets or sets the origin (radian-angle) this <see cref="ConicalGradientTexture"/> is drawn to. (default: -π/2)
/// </summary>
public sealed class ConicalGradientTexture : AbstractGradientTexture
public float Origin
{
#region Constants
private const float PI2 = MathF.PI * 2.0f;
#endregion
#region Properties & Fields
private float _origin = MathF.Atan2(-1, 0);
/// <summary>
/// Gets or sets the origin (radian-angle) this <see cref="ConicalGradientTexture"/> is drawn to. (default: -π/2)
/// </summary>
public float Origin
{
get => _origin;
set => SetProperty(ref _origin, value);
}
private Point _center = new(0.5f, 0.5f);
/// <summary>
/// Gets or sets the center <see cref="Point"/> (as percentage in the range [0..1]) of the <see cref="IGradient"/> drawn by this <see cref="ConicalGradientTexture"/>. (default: 0.5, 0.5)
/// </summary>
public Point Center
{
get => _center;
set => SetProperty(ref _center, value);
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" />.</param>
public ConicalGradientTexture(Size size, IGradient gradient)
: base(size, gradient)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" />.</param>
/// <param name="center">The center <see cref="T:RGB.NET.Core.Point" /> (as percentage in the range [0..1]).</param>
public ConicalGradientTexture(Size size, IGradient gradient, Point center)
: base(size, gradient)
{
this.Center = center;
}
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" />.</param>
/// <param name="center">The center <see cref="T:RGB.NET.Core.Point" /> (as percentage in the range [0..1]).</param>
/// <param name="origin">The origin (radian-angle) the gradient is drawn to.</param>
public ConicalGradientTexture(Size size, IGradient gradient, Point center, float origin)
: base(size, gradient)
{
this.Center = center;
this.Origin = origin;
}
#endregion
#region Methods
/// <inheritdoc />
protected override Color GetColor(in Point point)
{
float angle = MathF.Atan2(point.Y - Center.Y, point.X - Center.X) - Origin;
if (angle < 0) angle += PI2;
float offset = angle / PI2;
return Gradient.GetColor(offset);
}
#endregion
get => _origin;
set => SetProperty(ref _origin, value);
}
}
private Point _center = new(0.5f, 0.5f);
/// <summary>
/// Gets or sets the center <see cref="Point"/> (as percentage in the range [0..1]) of the <see cref="IGradient"/> drawn by this <see cref="ConicalGradientTexture"/>. (default: 0.5, 0.5)
/// </summary>
public Point Center
{
get => _center;
set => SetProperty(ref _center, value);
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" />.</param>
public ConicalGradientTexture(Size size, IGradient gradient)
: base(size, gradient)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" />.</param>
/// <param name="center">The center <see cref="T:RGB.NET.Core.Point" /> (as percentage in the range [0..1]).</param>
public ConicalGradientTexture(Size size, IGradient gradient, Point center)
: base(size, gradient)
{
this.Center = center;
}
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.ConicalGradientTexture" />.</param>
/// <param name="center">The center <see cref="T:RGB.NET.Core.Point" /> (as percentage in the range [0..1]).</param>
/// <param name="origin">The origin (radian-angle) the gradient is drawn to.</param>
public ConicalGradientTexture(Size size, IGradient gradient, Point center, float origin)
: base(size, gradient)
{
this.Center = center;
this.Origin = origin;
}
#endregion
#region Methods
/// <inheritdoc />
protected override Color GetColor(in Point point)
{
float angle = MathF.Atan2(point.Y - Center.Y, point.X - Center.X) - Origin;
if (angle < 0) angle += PI2;
float offset = angle / PI2;
return Gradient.GetColor(offset);
}
#endregion
}
+9 -10
View File
@@ -1,13 +1,12 @@
#pragma warning disable 1591
namespace RGB.NET.Presets.Textures
namespace RGB.NET.Presets.Textures;
/// <summary>
/// Contains a list of possible color formats.
/// </summary>
public enum ColorFormat
{
/// <summary>
/// Contains a list of possible color formats.
/// </summary>
public enum ColorFormat
{
RGB,
BGR
}
}
RGB,
BGR
}
+63 -64
View File
@@ -2,73 +2,72 @@
using RGB.NET.Core;
using RGB.NET.Presets.Textures.Sampler;
namespace RGB.NET.Presets.Textures
namespace RGB.NET.Presets.Textures;
/// <inheritdoc />
/// <summary>
/// Represents a texture of float-data pixels.
/// </summary>
public sealed class FloatPixelTexture : PixelTexture<float>
{
/// <inheritdoc />
#region Properties & Fields
private readonly float[] _data;
/// <inheritdoc />
protected override ReadOnlySpan<float> Data => _data;
/// <summary>
/// Represents a texture of float-data pixels.
/// Gets the color format the data is in.
/// </summary>
public sealed class FloatPixelTexture : PixelTexture<float>
public ColorFormat ColorFormat { get; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="FloatPixelTexture" /> class.
/// A <see cref="AverageColorSampler"/> is used.
/// </summary>
/// <param name="with">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="data">The pixel-data of the texture.</param>
/// <param name="colorFormat">The color format the data is in. (default: RGB)</param>
public FloatPixelTexture(int with, int height, float[] data, ColorFormat colorFormat = ColorFormat.RGB)
: this(with, height, data, new AverageFloatSampler(), colorFormat)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="FloatPixelTexture" /> class.
/// </summary>
/// <param name="with">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="data">The pixel-data of the texture.</param>
/// <param name="sampler">The sampler used to get the color of a region.</param>
/// <param name="colorFormat">The color format the data is in. (default: RGB)</param>
public FloatPixelTexture(int with, int height, float[] data, ISampler<float> sampler, ColorFormat colorFormat = ColorFormat.RGB)
: base(with, height, 3, sampler)
{
#region Properties & Fields
this._data = data;
this.ColorFormat = colorFormat;
private readonly float[] _data;
/// <inheritdoc />
protected override ReadOnlySpan<float> Data => _data;
/// <summary>
/// Gets the color format the data is in.
/// </summary>
public ColorFormat ColorFormat { get; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="FloatPixelTexture" /> class.
/// A <see cref="AverageColorSampler"/> is used.
/// </summary>
/// <param name="with">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="data">The pixel-data of the texture.</param>
/// <param name="colorFormat">The color format the data is in. (default: RGB)</param>
public FloatPixelTexture(int with, int height, float[] data, ColorFormat colorFormat = ColorFormat.RGB)
: this(with, height, data, new AverageFloatSampler(), colorFormat)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="FloatPixelTexture" /> class.
/// </summary>
/// <param name="with">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="data">The pixel-data of the texture.</param>
/// <param name="sampler">The sampler used to get the color of a region.</param>
/// <param name="colorFormat">The color format the data is in. (default: RGB)</param>
public FloatPixelTexture(int with, int height, float[] data, ISampler<float> sampler, ColorFormat colorFormat = ColorFormat.RGB)
: base(with, height, 3, sampler)
{
this._data = data;
this.ColorFormat = colorFormat;
if (Data.Length != ((with * height) * 3)) throw new ArgumentException($"Data-Length {Data.Length} differs from the specified size {with}x{height} * 3 bytes ({with * height * 3}).");
}
#endregion
#region Methods
/// <inheritdoc />
protected override Color GetColor(in ReadOnlySpan<float> pixel)
{
return ColorFormat switch
{
ColorFormat.RGB => new Color(pixel[0], pixel[1], pixel[2]),
ColorFormat.BGR => new Color(pixel[2], pixel[1], pixel[0]),
_ => throw new ArgumentOutOfRangeException()
};
}
#endregion
if (Data.Length != ((with * height) * 3)) throw new ArgumentException($"Data-Length {Data.Length} differs from the specified size {with}x{height} * 3 bytes ({with * height * 3}).");
}
}
#endregion
#region Methods
/// <inheritdoc />
protected override Color GetColor(in ReadOnlySpan<float> pixel)
{
return ColorFormat switch
{
ColorFormat.RGB => new Color(pixel[0], pixel[1], pixel[2]),
ColorFormat.BGR => new Color(pixel[2], pixel[1], pixel[0]),
_ => throw new ArgumentOutOfRangeException()
};
}
#endregion
}
@@ -10,142 +10,141 @@ using System.Linq;
using RGB.NET.Core;
using RGB.NET.Presets.Decorators;
namespace RGB.NET.Presets.Textures.Gradients
namespace RGB.NET.Presets.Textures.Gradients;
/// <inheritdoc cref="AbstractDecoratable{T}" />
/// <inheritdoc cref="IGradient" />
/// <summary>
/// Represents a basic gradient.
/// </summary>
public abstract class AbstractGradient : AbstractDecoratable<IGradientDecorator>, IGradient
{
/// <inheritdoc cref="AbstractDecoratable{T}" />
/// <inheritdoc cref="IGradient" />
#region Properties & Fields
/// <summary>
/// Represents a basic gradient.
/// Gets a list of the stops used by this <see cref="AbstractGradient"/>.
/// </summary>
public abstract class AbstractGradient : AbstractDecoratable<IGradientDecorator>, IGradient
public ObservableCollection<GradientStop> GradientStops { get; } = new();
private bool _wrapGradient;
/// <summary>
/// Gets or sets if the Gradient wraps around if there isn't a second stop to take.
/// Example: There is a stop at offset 0.0, 0.5 and 0.75.
/// Without wrapping offset 1.0 will be calculated the same as 0.75; with wrapping it would be the same as 0.0.
/// </summary>
public bool WrapGradient
{
#region Properties & Fields
/// <summary>
/// Gets a list of the stops used by this <see cref="AbstractGradient"/>.
/// </summary>
public ObservableCollection<GradientStop> GradientStops { get; } = new();
private bool _wrapGradient;
/// <summary>
/// Gets or sets if the Gradient wraps around if there isn't a second stop to take.
/// Example: There is a stop at offset 0.0, 0.5 and 0.75.
/// Without wrapping offset 1.0 will be calculated the same as 0.75; with wrapping it would be the same as 0.0.
/// </summary>
public bool WrapGradient
{
get => _wrapGradient;
set => SetProperty(ref _wrapGradient, value);
}
#endregion
#region Events
/// <inheritdoc />
public event EventHandler? GradientChanged;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary>
protected AbstractGradient()
{
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (_, _) => OnGradientChanged();
}
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
protected AbstractGradient(params GradientStop[] gradientStops)
{
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (_, _) => OnGradientChanged();
foreach (GradientStop gradientStop in gradientStops)
GradientStops.Add(gradientStop);
}
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary>
/// <param name="wrapGradient">Specifies whether the gradient should wrapp or not (see <see cref="WrapGradient"/> for an example of what this means).</param>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
protected AbstractGradient(bool wrapGradient, params GradientStop[] gradientStops)
{
this.WrapGradient = wrapGradient;
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (_, _) => OnGradientChanged();
foreach (GradientStop gradientStop in gradientStops)
GradientStops.Add(gradientStop);
}
#endregion
#region Methods
/// <summary>
/// Clips the offset and ensures, that it is inside the bounds of the stop list.
/// </summary>
/// <param name="offset">The offset to clip.</param>
/// <returns>The clipped offset.</returns>
protected float ClipOffset(float offset)
{
float max = GradientStops.Max(stop => stop.Offset);
if (offset > max)
return max;
float min = GradientStops.Min(stop => stop.Offset);
return offset < min ? min : offset;
}
/// <inheritdoc />
public abstract Color GetColor(float offset);
/// <inheritdoc />
public virtual void Move(float offset)
{
offset /= 360.0f;
foreach (GradientStop gradientStop in GradientStops)
gradientStop.Offset += offset;
while (GradientStops.All(stop => stop.Offset > 1))
foreach (GradientStop gradientStop in GradientStops)
gradientStop.Offset -= 1;
while (GradientStops.All(stop => stop.Offset < 0))
foreach (GradientStop gradientStop in GradientStops)
gradientStop.Offset += 1;
}
/// <summary>
/// Should be called to indicate that the gradient was changed.
/// </summary>
protected void OnGradientChanged() => GradientChanged?.Invoke(this, EventArgs.Empty);
private void GradientCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
foreach (GradientStop gradientStop in e.OldItems)
gradientStop.PropertyChanged -= GradientStopChanged;
if (e.NewItems != null)
foreach (GradientStop gradientStop in e.NewItems)
gradientStop.PropertyChanged += GradientStopChanged;
OnGradientChanged();
}
private void GradientStopChanged(object? sender, PropertyChangedEventArgs propertyChangedEventArgs) => OnGradientChanged();
#endregion
get => _wrapGradient;
set => SetProperty(ref _wrapGradient, value);
}
}
#endregion
#region Events
/// <inheritdoc />
public event EventHandler? GradientChanged;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary>
protected AbstractGradient()
{
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (_, _) => OnGradientChanged();
}
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
protected AbstractGradient(params GradientStop[] gradientStops)
{
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (_, _) => OnGradientChanged();
foreach (GradientStop gradientStop in gradientStops)
GradientStops.Add(gradientStop);
}
/// <summary>
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
/// </summary>
/// <param name="wrapGradient">Specifies whether the gradient should wrapp or not (see <see cref="WrapGradient"/> for an example of what this means).</param>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
protected AbstractGradient(bool wrapGradient, params GradientStop[] gradientStops)
{
this.WrapGradient = wrapGradient;
GradientStops.CollectionChanged += GradientCollectionChanged;
PropertyChanged += (_, _) => OnGradientChanged();
foreach (GradientStop gradientStop in gradientStops)
GradientStops.Add(gradientStop);
}
#endregion
#region Methods
/// <summary>
/// Clips the offset and ensures, that it is inside the bounds of the stop list.
/// </summary>
/// <param name="offset">The offset to clip.</param>
/// <returns>The clipped offset.</returns>
protected float ClipOffset(float offset)
{
float max = GradientStops.Max(stop => stop.Offset);
if (offset > max)
return max;
float min = GradientStops.Min(stop => stop.Offset);
return offset < min ? min : offset;
}
/// <inheritdoc />
public abstract Color GetColor(float offset);
/// <inheritdoc />
public virtual void Move(float offset)
{
offset /= 360.0f;
foreach (GradientStop gradientStop in GradientStops)
gradientStop.Offset += offset;
while (GradientStops.All(stop => stop.Offset > 1))
foreach (GradientStop gradientStop in GradientStops)
gradientStop.Offset -= 1;
while (GradientStops.All(stop => stop.Offset < 0))
foreach (GradientStop gradientStop in GradientStops)
gradientStop.Offset += 1;
}
/// <summary>
/// Should be called to indicate that the gradient was changed.
/// </summary>
protected void OnGradientChanged() => GradientChanged?.Invoke(this, EventArgs.Empty);
private void GradientCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
foreach (GradientStop gradientStop in e.OldItems)
gradientStop.PropertyChanged -= GradientStopChanged;
if (e.NewItems != null)
foreach (GradientStop gradientStop in e.NewItems)
gradientStop.PropertyChanged += GradientStopChanged;
OnGradientChanged();
}
private void GradientStopChanged(object? sender, PropertyChangedEventArgs propertyChangedEventArgs) => OnGradientChanged();
#endregion
}
@@ -3,50 +3,49 @@
using RGB.NET.Core;
namespace RGB.NET.Presets.Textures.Gradients
namespace RGB.NET.Presets.Textures.Gradients;
/// <summary>
/// Represents a stop on a gradient.
/// </summary>
public class GradientStop : AbstractBindable
{
#region Properties & Fields
private float _offset;
/// <summary>
/// Represents a stop on a gradient.
/// Gets or sets the percentage offset to place this <see cref="GradientStop"/>. This should be inside the range of [0..1] but it's not necessary.
/// </summary>
public class GradientStop : AbstractBindable
public float Offset
{
#region Properties & Fields
private float _offset;
/// <summary>
/// Gets or sets the percentage offset to place this <see cref="GradientStop"/>. This should be inside the range of [0..1] but it's not necessary.
/// </summary>
public float Offset
{
get => _offset;
set => SetProperty(ref _offset, value);
}
private Color _color;
/// <summary>
/// Gets or sets the <see cref="Color"/> of this <see cref="GradientStop"/>.
/// </summary>
public Color Color
{
get => _color;
set => SetProperty(ref _color, value);
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GradientStop"/> class.
/// </summary>
/// <param name="offset">The percentage offset to place this <see cref="GradientStop"/>.</param>
/// <param name="color">The <see cref="Color"/> of the <see cref="GradientStop"/>.</param>
public GradientStop(float offset, Color color)
{
this.Offset = offset;
this.Color = color;
}
#endregion
get => _offset;
set => SetProperty(ref _offset, value);
}
}
private Color _color;
/// <summary>
/// Gets or sets the <see cref="Color"/> of this <see cref="GradientStop"/>.
/// </summary>
public Color Color
{
get => _color;
set => SetProperty(ref _color, value);
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GradientStop"/> class.
/// </summary>
/// <param name="offset">The percentage offset to place this <see cref="GradientStop"/>.</param>
/// <param name="color">The <see cref="Color"/> of the <see cref="GradientStop"/>.</param>
public GradientStop(float offset, Color color)
{
this.Offset = offset;
this.Color = color;
}
#endregion
}
+20 -21
View File
@@ -4,29 +4,28 @@ using System;
using RGB.NET.Core;
using RGB.NET.Presets.Decorators;
namespace RGB.NET.Presets.Textures.Gradients
namespace RGB.NET.Presets.Textures.Gradients;
/// <summary>
/// Represents a basic gradient.
/// </summary>
public interface IGradient : IDecoratable<IGradientDecorator>
{
/// <summary>
/// Represents a basic gradient.
/// Occurs if the <see cref="IGradient"/> is changed.
/// </summary>
public interface IGradient : IDecoratable<IGradientDecorator>
{
/// <summary>
/// Occurs if the <see cref="IGradient"/> is changed.
/// </summary>
event EventHandler GradientChanged;
event EventHandler GradientChanged;
/// <summary>
/// Gets the <see cref="Color"/> of the <see cref="IGradient"/> on the specified offset.
/// </summary>
/// <param name="offset">The percentage offset to take the <see cref="Color"/> from.</param>
/// <returns>The <see cref="Color"/> at the specific offset.</returns>
Color GetColor(float offset);
/// <summary>
/// Gets the <see cref="Color"/> of the <see cref="IGradient"/> on the specified offset.
/// </summary>
/// <param name="offset">The percentage offset to take the <see cref="Color"/> from.</param>
/// <returns>The <see cref="Color"/> at the specific offset.</returns>
Color GetColor(float offset);
/// <summary>
/// Moves the <see cref="IGradient"/> by the provided offset.
/// </summary>
/// <param name="offset">The offset the <see cref="IGradient"/> should be moved.</param>
void Move(float offset);
}
}
/// <summary>
/// Moves the <see cref="IGradient"/> by the provided offset.
/// </summary>
/// <param name="offset">The offset the <see cref="IGradient"/> should be moved.</param>
void Move(float offset);
}
@@ -5,148 +5,147 @@ using System.ComponentModel;
using System.Linq;
using RGB.NET.Core;
namespace RGB.NET.Presets.Textures.Gradients
namespace RGB.NET.Presets.Textures.Gradients;
/// <inheritdoc />
/// <summary>
/// Represents a linear interpolated gradient with n stops.
/// </summary>
public class LinearGradient : AbstractGradient
{
#region Properties & Fields
private bool _isOrderedGradientListDirty = true;
private LinkedList<GradientStop> _orderedGradientStops = new();
#endregion
#region Constructors
/// <inheritdoc />
/// <summary>
/// Represents a linear interpolated gradient with n stops.
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.Gradients.LinearGradient" /> class.
/// </summary>
public class LinearGradient : AbstractGradient
public LinearGradient()
{
#region Properties & Fields
private bool _isOrderedGradientListDirty = true;
private LinkedList<GradientStop> _orderedGradientStops = new();
#endregion
#region Constructors
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.Gradients.LinearGradient" /> class.
/// </summary>
public LinearGradient()
{
Initialize();
}
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.Gradients.LinearGradient" /> class.
/// </summary>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
public LinearGradient(params GradientStop[] gradientStops)
: base(gradientStops)
{
Initialize();
}
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.Gradients.AbstractGradient" /> class.
/// </summary>
/// <param name="wrapGradient">Specifies whether the gradient should wrapp or not (see <see cref="P:RGB.NET.Presets.Textures.Gradients.AbstractGradient.WrapGradient" /> for an example of what this means).</param>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
public LinearGradient(bool wrapGradient, params GradientStop[] gradientStops)
: base(wrapGradient, gradientStops)
{
Initialize();
}
#endregion
#region Methods
private void Initialize()
{
void OnGradientStopOnPropertyChanged(object? sender, PropertyChangedEventArgs args) => _isOrderedGradientListDirty = true;
foreach (GradientStop gradientStop in GradientStops)
gradientStop.PropertyChanged += OnGradientStopOnPropertyChanged;
GradientStops.CollectionChanged += (_, args) =>
{
if (args.OldItems != null)
foreach (GradientStop gradientStop in args.OldItems)
gradientStop.PropertyChanged -= OnGradientStopOnPropertyChanged;
if (args.NewItems != null)
foreach (GradientStop gradientStop in args.NewItems)
gradientStop.PropertyChanged += OnGradientStopOnPropertyChanged;
};
}
/// <inheritdoc />
/// <summary>
/// Gets the linear interpolated <see cref="T:RGB.NET.Core.Color" /> at the specified offset.
/// </summary>
/// <param name="offset">The percentage offset to take the color from.</param>
/// <returns>The <see cref="T:RGB.NET.Core.Color" /> at the specific offset.</returns>
public override Color GetColor(float offset)
{
if (GradientStops.Count == 0) return Color.Transparent;
if (GradientStops.Count == 1) return GradientStops[0].Color;
if (_isOrderedGradientListDirty)
_orderedGradientStops = new LinkedList<GradientStop>(GradientStops.OrderBy(x => x.Offset));
(GradientStop gsBefore, GradientStop gsAfter) = GetEnclosingGradientStops(offset, _orderedGradientStops, WrapGradient);
float blendFactor = 0;
if (!gsBefore.Offset.Equals(gsAfter.Offset))
blendFactor = ((offset - gsBefore.Offset) / (gsAfter.Offset - gsBefore.Offset));
float colA = ((gsAfter.Color.A - gsBefore.Color.A) * blendFactor) + gsBefore.Color.A;
float colR = ((gsAfter.Color.R - gsBefore.Color.R) * blendFactor) + gsBefore.Color.R;
float colG = ((gsAfter.Color.G - gsBefore.Color.G) * blendFactor) + gsBefore.Color.G;
float colB = ((gsAfter.Color.B - gsBefore.Color.B) * blendFactor) + gsBefore.Color.B;
return new Color(colA, colR, colG, colB);
}
/// <summary>
/// Get the two <see cref="GradientStop"/>s encapsulating the specified offset.
/// </summary>
/// <param name="offset">The reference offset.</param>
/// <param name="orderedStops">The ordered list of <see cref="GradientStop"/> to choose from.</param>
/// <param name="wrap">Bool indicating if the gradient should be wrapped or not.</param>
/// <returns>The two <see cref="GradientStop"/>s encapsulating the specified offset.</returns>
protected virtual (GradientStop gsBefore, GradientStop gsAfter) GetEnclosingGradientStops(float offset, LinkedList<GradientStop> orderedStops, bool wrap)
{
LinkedList<GradientStop> gradientStops = new(orderedStops);
if (wrap)
{
GradientStop? gsBefore, gsAfter;
do
{
gsBefore = gradientStops.LastOrDefault(n => n.Offset <= offset);
if (gsBefore == null)
{
GradientStop lastStop = gradientStops.Last!.Value;
gradientStops.AddFirst(new GradientStop(lastStop.Offset - 1, lastStop.Color));
gradientStops.RemoveLast();
}
gsAfter = gradientStops.FirstOrDefault(n => n.Offset >= offset);
if (gsAfter == null)
{
GradientStop firstStop = gradientStops.First!.Value;
gradientStops.AddLast(new GradientStop(firstStop.Offset + 1, firstStop.Color));
gradientStops.RemoveFirst();
}
} while ((gsBefore == null) || (gsAfter == null));
return (gsBefore, gsAfter);
}
offset = ClipOffset(offset);
return (gradientStops.Last(n => n.Offset <= offset), gradientStops.First(n => n.Offset >= offset));
}
#endregion
Initialize();
}
}
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.Gradients.LinearGradient" /> class.
/// </summary>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
public LinearGradient(params GradientStop[] gradientStops)
: base(gradientStops)
{
Initialize();
}
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.Gradients.AbstractGradient" /> class.
/// </summary>
/// <param name="wrapGradient">Specifies whether the gradient should wrapp or not (see <see cref="P:RGB.NET.Presets.Textures.Gradients.AbstractGradient.WrapGradient" /> for an example of what this means).</param>
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
public LinearGradient(bool wrapGradient, params GradientStop[] gradientStops)
: base(wrapGradient, gradientStops)
{
Initialize();
}
#endregion
#region Methods
private void Initialize()
{
void OnGradientStopOnPropertyChanged(object? sender, PropertyChangedEventArgs args) => _isOrderedGradientListDirty = true;
foreach (GradientStop gradientStop in GradientStops)
gradientStop.PropertyChanged += OnGradientStopOnPropertyChanged;
GradientStops.CollectionChanged += (_, args) =>
{
if (args.OldItems != null)
foreach (GradientStop gradientStop in args.OldItems)
gradientStop.PropertyChanged -= OnGradientStopOnPropertyChanged;
if (args.NewItems != null)
foreach (GradientStop gradientStop in args.NewItems)
gradientStop.PropertyChanged += OnGradientStopOnPropertyChanged;
};
}
/// <inheritdoc />
/// <summary>
/// Gets the linear interpolated <see cref="T:RGB.NET.Core.Color" /> at the specified offset.
/// </summary>
/// <param name="offset">The percentage offset to take the color from.</param>
/// <returns>The <see cref="T:RGB.NET.Core.Color" /> at the specific offset.</returns>
public override Color GetColor(float offset)
{
if (GradientStops.Count == 0) return Color.Transparent;
if (GradientStops.Count == 1) return GradientStops[0].Color;
if (_isOrderedGradientListDirty)
_orderedGradientStops = new LinkedList<GradientStop>(GradientStops.OrderBy(x => x.Offset));
(GradientStop gsBefore, GradientStop gsAfter) = GetEnclosingGradientStops(offset, _orderedGradientStops, WrapGradient);
float blendFactor = 0;
if (!gsBefore.Offset.Equals(gsAfter.Offset))
blendFactor = ((offset - gsBefore.Offset) / (gsAfter.Offset - gsBefore.Offset));
float colA = ((gsAfter.Color.A - gsBefore.Color.A) * blendFactor) + gsBefore.Color.A;
float colR = ((gsAfter.Color.R - gsBefore.Color.R) * blendFactor) + gsBefore.Color.R;
float colG = ((gsAfter.Color.G - gsBefore.Color.G) * blendFactor) + gsBefore.Color.G;
float colB = ((gsAfter.Color.B - gsBefore.Color.B) * blendFactor) + gsBefore.Color.B;
return new Color(colA, colR, colG, colB);
}
/// <summary>
/// Get the two <see cref="GradientStop"/>s encapsulating the specified offset.
/// </summary>
/// <param name="offset">The reference offset.</param>
/// <param name="orderedStops">The ordered list of <see cref="GradientStop"/> to choose from.</param>
/// <param name="wrap">Bool indicating if the gradient should be wrapped or not.</param>
/// <returns>The two <see cref="GradientStop"/>s encapsulating the specified offset.</returns>
protected virtual (GradientStop gsBefore, GradientStop gsAfter) GetEnclosingGradientStops(float offset, LinkedList<GradientStop> orderedStops, bool wrap)
{
LinkedList<GradientStop> gradientStops = new(orderedStops);
if (wrap)
{
GradientStop? gsBefore, gsAfter;
do
{
gsBefore = gradientStops.LastOrDefault(n => n.Offset <= offset);
if (gsBefore == null)
{
GradientStop lastStop = gradientStops.Last!.Value;
gradientStops.AddFirst(new GradientStop(lastStop.Offset - 1, lastStop.Color));
gradientStops.RemoveLast();
}
gsAfter = gradientStops.FirstOrDefault(n => n.Offset >= offset);
if (gsAfter == null)
{
GradientStop firstStop = gradientStops.First!.Value;
gradientStops.AddLast(new GradientStop(firstStop.Offset + 1, firstStop.Color));
gradientStops.RemoveFirst();
}
} while ((gsBefore == null) || (gsAfter == null));
return (gsBefore, gsAfter);
}
offset = ClipOffset(offset);
return (gradientStops.Last(n => n.Offset <= offset), gradientStops.First(n => n.Offset >= offset));
}
#endregion
}
@@ -5,105 +5,104 @@ using System;
using RGB.NET.Core;
using RGB.NET.Presets.Decorators;
namespace RGB.NET.Presets.Textures.Gradients
namespace RGB.NET.Presets.Textures.Gradients;
/// <inheritdoc cref="AbstractDecoratable{T}" />
/// <inheritdoc cref="IGradient" />
/// <summary>
/// Represents a rainbow gradient which circles through all colors of the HUE-color-space.<br />
/// See <see href="http://upload.wikimedia.org/wikipedia/commons/a/ad/HueScale.svg" /> as reference.
/// </summary>
public class RainbowGradient : AbstractDecoratable<IGradientDecorator>, IGradient
{
/// <inheritdoc cref="AbstractDecoratable{T}" />
/// <inheritdoc cref="IGradient" />
#region Properties & Fields
private float _startHue;
/// <summary>
/// Represents a rainbow gradient which circles through all colors of the HUE-color-space.<br />
/// See <see href="http://upload.wikimedia.org/wikipedia/commons/a/ad/HueScale.svg" /> as reference.
/// Gets or sets the hue (in degrees) to start from.
/// </summary>
public class RainbowGradient : AbstractDecoratable<IGradientDecorator>, IGradient
public float StartHue
{
#region Properties & Fields
private float _startHue;
/// <summary>
/// Gets or sets the hue (in degrees) to start from.
/// </summary>
public float StartHue
{
get => _startHue;
set => SetProperty(ref _startHue, value);
}
private float _endHue;
/// <summary>
/// Gets or sets the hue (in degrees) to end the with.
/// </summary>
public float EndHue
{
get => _endHue;
set => SetProperty(ref _endHue, value);
}
#endregion
#region Events
/// <inheritdoc />
public event EventHandler? GradientChanged;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RainbowGradient"/> class.
/// </summary>
/// <param name="startHue">The hue (in degrees) to start from (default: 0)</param>
/// <param name="endHue">The hue (in degrees) to end with (default: 360)</param>
public RainbowGradient(float startHue = 0, float endHue = 360)
{
this.StartHue = startHue;
this.EndHue = endHue;
PropertyChanged += (_, _) => OnGradientChanged();
}
#endregion
#region Methods
/// <inheritdoc />
/// <summary>
/// Gets the color on the rainbow at the specified offset.
/// </summary>
/// <param name="offset">The percentage offset to take the color from.</param>
/// <returns>The color at the specific offset.</returns>
public Color GetColor(float offset)
{
float range = EndHue - StartHue;
float hue = StartHue + (range * offset);
return HSVColor.Create(hue, 1, 1);
}
/// <inheritdoc />
public void Move(float offset)
{
// RainbowGradient is calculated inverse
offset *= -1;
StartHue += offset;
EndHue += offset;
while ((StartHue > 360) && (EndHue > 360))
{
StartHue -= 360;
EndHue -= 360;
}
while ((StartHue < -360) && (EndHue < -360))
{
StartHue += 360;
EndHue += 360;
}
}
/// <summary>
/// Should be called to indicate that the gradient was changed.
/// </summary>
protected void OnGradientChanged() => GradientChanged?.Invoke(this, EventArgs.Empty);
#endregion
get => _startHue;
set => SetProperty(ref _startHue, value);
}
}
private float _endHue;
/// <summary>
/// Gets or sets the hue (in degrees) to end the with.
/// </summary>
public float EndHue
{
get => _endHue;
set => SetProperty(ref _endHue, value);
}
#endregion
#region Events
/// <inheritdoc />
public event EventHandler? GradientChanged;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RainbowGradient"/> class.
/// </summary>
/// <param name="startHue">The hue (in degrees) to start from (default: 0)</param>
/// <param name="endHue">The hue (in degrees) to end with (default: 360)</param>
public RainbowGradient(float startHue = 0, float endHue = 360)
{
this.StartHue = startHue;
this.EndHue = endHue;
PropertyChanged += (_, _) => OnGradientChanged();
}
#endregion
#region Methods
/// <inheritdoc />
/// <summary>
/// Gets the color on the rainbow at the specified offset.
/// </summary>
/// <param name="offset">The percentage offset to take the color from.</param>
/// <returns>The color at the specific offset.</returns>
public Color GetColor(float offset)
{
float range = EndHue - StartHue;
float hue = StartHue + (range * offset);
return HSVColor.Create(hue, 1, 1);
}
/// <inheritdoc />
public void Move(float offset)
{
// RainbowGradient is calculated inverse
offset *= -1;
StartHue += offset;
EndHue += offset;
while ((StartHue > 360) && (EndHue > 360))
{
StartHue -= 360;
EndHue -= 360;
}
while ((StartHue < -360) && (EndHue < -360))
{
StartHue += 360;
EndHue += 360;
}
}
/// <summary>
/// Should be called to indicate that the gradient was changed.
/// </summary>
protected void OnGradientChanged() => GradientChanged?.Invoke(this, EventArgs.Empty);
#endregion
}
@@ -9,74 +9,73 @@ using RGB.NET.Core;
using RGB.NET.Presets.Helper;
using RGB.NET.Presets.Textures.Gradients;
namespace RGB.NET.Presets.Textures
namespace RGB.NET.Presets.Textures;
/// <inheritdoc />
/// <summary>
/// Represents a texture drawing a linear gradient.
/// </summary>
public sealed class LinearGradientTexture : AbstractGradientTexture
{
/// <inheritdoc />
#region Properties & Fields
private Point _startPoint = new(0, 0.5f);
/// <summary>
/// Represents a texture drawing a linear gradient.
/// Gets or sets the start <see cref="Point"/> (as percentage in the range [0..1]) of the <see cref="IGradient"/> drawn by this <see cref="LinearGradientTexture"/>. (default: 0.0, 0.5)
/// </summary>
public sealed class LinearGradientTexture : AbstractGradientTexture
public Point StartPoint
{
#region Properties & Fields
private Point _startPoint = new(0, 0.5f);
/// <summary>
/// Gets or sets the start <see cref="Point"/> (as percentage in the range [0..1]) of the <see cref="IGradient"/> drawn by this <see cref="LinearGradientTexture"/>. (default: 0.0, 0.5)
/// </summary>
public Point StartPoint
{
get => _startPoint;
set => SetProperty(ref _startPoint, value);
}
private Point _endPoint = new(1, 0.5f);
/// <summary>
/// Gets or sets the end <see cref="Point"/> (as percentage in the range [0..1]) of the <see cref="IGradient"/> drawn by this <see cref="LinearGradientTexture"/>. (default: 1.0, 0.5)
/// </summary>
public Point EndPoint
{
get => _endPoint;
set => SetProperty(ref _endPoint, value);
}
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.LinearGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.LinearGradientTexture" />.</param>
public LinearGradientTexture(Size size, IGradient gradient)
: base(size, gradient)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.LinearGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.LinearGradientTexture" />.</param>
/// <param name="startPoint">The start <see cref="T:RGB.NET.Core.Point" /> (as percentage in the range [0..1]).</param>
/// <param name="endPoint">The end <see cref="T:RGB.NET.Core.Point" /> (as percentage in the range [0..1]).</param>
public LinearGradientTexture(Size size, IGradient gradient, Point startPoint, Point endPoint)
: base(size, gradient)
{
this.StartPoint = startPoint;
this.EndPoint = endPoint;
}
#endregion
#region Methods
/// <inheritdoc />
protected override Color GetColor(in Point point)
{
float offset = GradientHelper.CalculateLinearGradientOffset(StartPoint, EndPoint, point);
return Gradient.GetColor(offset);
}
#endregion
get => _startPoint;
set => SetProperty(ref _startPoint, value);
}
}
private Point _endPoint = new(1, 0.5f);
/// <summary>
/// Gets or sets the end <see cref="Point"/> (as percentage in the range [0..1]) of the <see cref="IGradient"/> drawn by this <see cref="LinearGradientTexture"/>. (default: 1.0, 0.5)
/// </summary>
public Point EndPoint
{
get => _endPoint;
set => SetProperty(ref _endPoint, value);
}
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.LinearGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.LinearGradientTexture" />.</param>
public LinearGradientTexture(Size size, IGradient gradient)
: base(size, gradient)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.LinearGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The <see cref="T:RGB.NET.Presets.Gradients.IGradient" /> drawn by this <see cref="T:RGB.NET.Presets.Textures.LinearGradientTexture" />.</param>
/// <param name="startPoint">The start <see cref="T:RGB.NET.Core.Point" /> (as percentage in the range [0..1]).</param>
/// <param name="endPoint">The end <see cref="T:RGB.NET.Core.Point" /> (as percentage in the range [0..1]).</param>
public LinearGradientTexture(Size size, IGradient gradient, Point startPoint, Point endPoint)
: base(size, gradient)
{
this.StartPoint = startPoint;
this.EndPoint = endPoint;
}
#endregion
#region Methods
/// <inheritdoc />
protected override Color GetColor(in Point point)
{
float offset = GradientHelper.CalculateLinearGradientOffset(StartPoint, EndPoint, point);
return Gradient.GetColor(offset);
}
#endregion
}
@@ -6,76 +6,75 @@ using RGB.NET.Core;
using RGB.NET.Presets.Helper;
using RGB.NET.Presets.Textures.Gradients;
namespace RGB.NET.Presets.Textures
namespace RGB.NET.Presets.Textures;
/// <inheritdoc />
/// <summary>
/// Represents a texture drawing a radial gradient around a center point.
/// </summary>
public sealed class RadialGradientTexture : AbstractGradientTexture
{
/// <inheritdoc />
#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);
/// <summary>
/// Represents a texture drawing a radial gradient around a center point.
/// Gets or sets the center <see cref="Point"/> (as percentage in the range [0..1]) around which the <see cref="RadialGradientTexture"/> should be drawn. (default: 0.5, 0.5)
/// </summary>
public sealed class RadialGradientTexture : AbstractGradientTexture
public Point Center
{
#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);
/// <summary>
/// Gets or sets the center <see cref="Point"/> (as percentage in the range [0..1]) around which the <see cref="RadialGradientTexture"/> should be drawn. (default: 0.5, 0.5)
/// </summary>
public Point Center
get => _center;
set
{
get => _center;
set
{
if (SetProperty(ref _center, value))
CalculateReferenceDistance();
}
if (SetProperty(ref _center, value))
CalculateReferenceDistance();
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.RadialGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The gradient drawn by the brush.</param>
public RadialGradientTexture(Size size, IGradient gradient)
: base(size, gradient)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.RadialGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The gradient drawn by the brush.</param>
/// <param name="center">The center point (as percentage in the range [0..1]).</param>
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);
}
/// <inheritdoc />
protected override Color GetColor(in Point point)
{
float distance = GradientHelper.CalculateDistance(point, Center);
float offset = distance / _referenceDistance;
return Gradient.GetColor(offset);
}
#endregion
}
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.RadialGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The gradient drawn by the brush.</param>
public RadialGradientTexture(Size size, IGradient gradient)
: base(size, gradient)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Presets.Textures.RadialGradientTexture" /> class.
/// </summary>
/// <param name="size">The size of the texture.</param>
/// <param name="gradient">The gradient drawn by the brush.</param>
/// <param name="center">The center point (as percentage in the range [0..1]).</param>
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);
}
/// <inheritdoc />
protected override Color GetColor(in Point point)
{
float distance = GradientHelper.CalculateDistance(point, Center);
float offset = distance / _referenceDistance;
return Gradient.GetColor(offset);
}
#endregion
}
@@ -1,34 +1,33 @@
using System;
using RGB.NET.Core;
namespace RGB.NET.Presets.Textures.Sampler
namespace RGB.NET.Presets.Textures.Sampler;
/// <summary>
/// Represents a sampled that averages multiple byte-data entries.
/// </summary>
public class AverageByteSampler : ISampler<byte>
{
/// <summary>
/// Represents a sampled that averages multiple byte-data entries.
/// </summary>
public class AverageByteSampler : ISampler<byte>
#region Methods
/// <inheritdoc />
public void Sample(in SamplerInfo<byte> info, in Span<byte> pixelData)
{
#region Methods
int count = info.Width * info.Height;
if (count == 0) return;
/// <inheritdoc />
public void Sample(in SamplerInfo<byte> info, in Span<byte> pixelData)
{
int count = info.Width * info.Height;
if (count == 0) return;
ReadOnlySpan<byte> data = info.Data;
ReadOnlySpan<byte> data = info.Data;
int dataLength = pixelData.Length;
Span<uint> sums = stackalloc uint[dataLength];
for (int i = 0; i < data.Length; i += dataLength)
for (int j = 0; j < sums.Length; j++)
sums[j] += data[i + j];
int dataLength = pixelData.Length;
Span<uint> sums = stackalloc uint[dataLength];
for (int i = 0; i < data.Length; i += dataLength)
for (int j = 0; j < sums.Length; j++)
sums[j] += data[i + j];
float divisor = count * byte.MaxValue;
for (int i = 0; i < pixelData.Length; i++)
pixelData[i] = (sums[i] / divisor).GetByteValueFromPercentage();
}
#endregion
float divisor = count * byte.MaxValue;
for (int i = 0; i < pixelData.Length; i++)
pixelData[i] = (sums[i] / divisor).GetByteValueFromPercentage();
}
}
#endregion
}
@@ -1,33 +1,32 @@
using System;
using RGB.NET.Core;
namespace RGB.NET.Presets.Textures.Sampler
namespace RGB.NET.Presets.Textures.Sampler;
/// <summary>
/// Represents a sampled that averages multiple float-data entries.
/// </summary>
public class AverageFloatSampler : ISampler<float>
{
/// <summary>
/// Represents a sampled that averages multiple float-data entries.
/// </summary>
public class AverageFloatSampler : ISampler<float>
#region Methods
/// <inheritdoc />
public void Sample(in SamplerInfo<float> info, in Span<float> pixelData)
{
#region Methods
int count = info.Width * info.Height;
if (count == 0) return;
/// <inheritdoc />
public void Sample(in SamplerInfo<float> info, in Span<float> pixelData)
{
int count = info.Width * info.Height;
if (count == 0) return;
ReadOnlySpan<float> data = info.Data;
ReadOnlySpan<float> data = info.Data;
int dataLength = pixelData.Length;
Span<float> sums = stackalloc float[dataLength];
for (int i = 0; i < data.Length; i += dataLength)
for (int j = 0; j < sums.Length; j++)
sums[j] += data[i + j];
int dataLength = pixelData.Length;
Span<float> sums = stackalloc float[dataLength];
for (int i = 0; i < data.Length; i += dataLength)
for (int j = 0; j < sums.Length; j++)
sums[j] += data[i + j];
for (int i = 0; i < pixelData.Length; i++)
pixelData[i] = sums[i] / count;
}
#endregion
for (int i = 0; i < pixelData.Length; i++)
pixelData[i] = sums[i] / count;
}
}
#endregion
}