mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-12 17:48:31 +00:00
Added some basic brushes
This commit is contained in:
parent
05c0633348
commit
3331496d59
97
RGB.NET.Brushes/Brushes/ConicalGradientBrush.cs
Normal file
97
RGB.NET.Brushes/Brushes/ConicalGradientBrush.cs
Normal file
@ -0,0 +1,97 @@
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// ReSharper disable MemberCanBeProtected.Global
|
||||
// ReSharper disable ReturnTypeCanBeEnumerable.Global
|
||||
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
|
||||
// ReSharper disable UnusedMember.Global
|
||||
|
||||
using System;
|
||||
using RGB.NET.Brushes.Gradients;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a brush drawing a conical gradient.
|
||||
/// </summary>
|
||||
public class ConicalGradientBrush : AbstractBrush, IGradientBrush
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the origin (radian-angle) this <see cref="ConicalGradientBrush"/> is drawn to. (default: -π/2)
|
||||
/// </summary>
|
||||
public float Origin { get; set; } = (float)Math.Atan2(-1, 0);
|
||||
|
||||
/// <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="ConicalGradientBrush"/>. (default: 0.5, 0.5)
|
||||
/// </summary>
|
||||
public Point Center { get; set; } = new Point(0.5, 0.5);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the gradient drawn by the brush. If null it will default to full transparent.
|
||||
/// </summary>
|
||||
public IGradient Gradient { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConicalGradientBrush"/> class.
|
||||
/// </summary>
|
||||
public ConicalGradientBrush()
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConicalGradientBrush"/> class.
|
||||
/// </summary>
|
||||
/// <param name="gradient">The <see cref="IGradient"/> drawn by this <see cref="ConicalGradientBrush"/>.</param>
|
||||
public ConicalGradientBrush(IGradient gradient)
|
||||
{
|
||||
this.Gradient = gradient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConicalGradientBrush"/> class.
|
||||
/// </summary>
|
||||
/// <param name="center">The center <see cref="Point"/> (as percentage in the range [0..1]).</param>
|
||||
/// <param name="gradient">The <see cref="IGradient"/> drawn by this <see cref="ConicalGradientBrush"/>.</param>
|
||||
public ConicalGradientBrush(Point center, IGradient gradient)
|
||||
{
|
||||
this.Center = center;
|
||||
this.Gradient = gradient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConicalGradientBrush"/> class.
|
||||
/// </summary>
|
||||
/// <param name="center">The center <see cref="Point"/> (as percentage in the range [0..1]).</param>
|
||||
/// <param name="origin">The origin (radian-angle) the <see cref="IBrush"/> is drawn to.</param>
|
||||
/// <param name="gradient">The <see cref="IGradient"/> drawn by this <see cref="ConicalGradientBrush"/>.</param>
|
||||
public ConicalGradientBrush(Point center, float origin, IGradient gradient)
|
||||
{
|
||||
this.Center = center;
|
||||
this.Origin = origin;
|
||||
this.Gradient = gradient;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Color GetColorAtPoint(Rectangle rectangle, BrushRenderTarget renderTarget)
|
||||
{
|
||||
double centerX = rectangle.Size.Width * Center.X;
|
||||
double centerY = rectangle.Size.Height * Center.Y;
|
||||
|
||||
double angle = Math.Atan2(renderTarget.Point.Y - centerY, renderTarget.Point.X - centerX) - Origin;
|
||||
if (angle < 0) angle += Math.PI * 2;
|
||||
double offset = angle / (Math.PI * 2);
|
||||
|
||||
return Gradient.GetColor(offset);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
16
RGB.NET.Brushes/Brushes/IGradientBrush.cs
Normal file
16
RGB.NET.Brushes/Brushes/IGradientBrush.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using RGB.NET.Brushes.Gradients;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a basic gradient-brush.
|
||||
/// </summary>
|
||||
public interface IGradientBrush : IBrush
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IGradient"/> used by this <see cref="IGradientBrush"/>.
|
||||
/// </summary>
|
||||
IGradient Gradient { get; }
|
||||
}
|
||||
}
|
||||
88
RGB.NET.Brushes/Brushes/LinearGradientBrush.cs
Normal file
88
RGB.NET.Brushes/Brushes/LinearGradientBrush.cs
Normal file
@ -0,0 +1,88 @@
|
||||
// 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 RGB.NET.Brushes.Gradients;
|
||||
using RGB.NET.Brushes.Helper;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a brush drawing a linear gradient.
|
||||
/// </summary>
|
||||
public class LinearGradientBrush : AbstractBrush, IGradientBrush
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <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="LinearGradientBrush"/>. (default: 0.0, 0.5)
|
||||
/// </summary>
|
||||
public Point StartPoint { get; set; } = new Point(0, 0.5);
|
||||
|
||||
/// <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="LinearGradientBrush"/>. (default: 1.0, 0.5)
|
||||
/// </summary>
|
||||
public Point EndPoint { get; set; } = new Point(1, 0.5);
|
||||
|
||||
/// <inheritdoc />
|
||||
public IGradient Gradient { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LinearGradientBrush"/> class.
|
||||
/// </summary>
|
||||
public LinearGradientBrush()
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LinearGradientBrush"/> class.
|
||||
/// </summary>
|
||||
/// <param name="gradient">The <see cref="IGradient"/> drawn by this <see cref="LinearGradientBrush"/>.</param>
|
||||
public LinearGradientBrush(IGradient gradient)
|
||||
{
|
||||
this.Gradient = gradient;
|
||||
}
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LinearGradientBrush"/> class.
|
||||
/// </summary>
|
||||
/// <param name="startPoint">The start <see cref="Point"/> (as percentage in the range [0..1]).</param>
|
||||
/// <param name="endPoint">The end <see cref="Point"/> (as percentage in the range [0..1]).</param>
|
||||
/// <param name="gradient">The <see cref="IGradient"/> drawn by this <see cref="LinearGradientBrush"/>.</param>
|
||||
public LinearGradientBrush(Point startPoint, Point endPoint, IGradient gradient)
|
||||
{
|
||||
this.StartPoint = startPoint;
|
||||
this.EndPoint = endPoint;
|
||||
this.Gradient = gradient;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets the color at an specific point assuming the brush is drawn into the given rectangle.
|
||||
/// </summary>
|
||||
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
|
||||
/// <param name="renderTarget">The target (key/point) from which the color should be taken.</param>
|
||||
/// <returns>The color at the specified point.</returns>
|
||||
protected override Color GetColorAtPoint(Rectangle rectangle, BrushRenderTarget renderTarget)
|
||||
{
|
||||
if (Gradient == null) return Color.Transparent;
|
||||
|
||||
Point startPoint = new Point(StartPoint.X * rectangle.Size.Width, StartPoint.Y * rectangle.Size.Height);
|
||||
Point endPoint = new Point(EndPoint.X * rectangle.Size.Width, EndPoint.Y * rectangle.Size.Height);
|
||||
|
||||
double offset = GradientHelper.CalculateLinearGradientOffset(startPoint, endPoint, renderTarget.Point);
|
||||
return Gradient.GetColor(offset);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
82
RGB.NET.Brushes/Brushes/RadialGradientBrush.cs
Normal file
82
RGB.NET.Brushes/Brushes/RadialGradientBrush.cs
Normal file
@ -0,0 +1,82 @@
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
|
||||
// ReSharper disable UnusedMember.Global
|
||||
|
||||
using System;
|
||||
using RGB.NET.Brushes.Gradients;
|
||||
using RGB.NET.Brushes.Helper;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a brush drawing a radial gradient around a center point.
|
||||
/// </summary>
|
||||
public class RadialGradientBrush : AbstractBrush, IGradientBrush
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the center <see cref="Point"/> (as percentage in the range [0..1]) around which the <see cref="RadialGradientBrush"/> should be drawn. (default: 0.5, 0.5)
|
||||
/// </summary>
|
||||
public Point Center { get; set; } = new Point(0.5, 0.5);
|
||||
|
||||
/// <inheritdoc />
|
||||
public IGradient Gradient { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RadialGradientBrush"/> class.
|
||||
/// </summary>
|
||||
public RadialGradientBrush()
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RadialGradientBrush"/> class.
|
||||
/// </summary>
|
||||
/// <param name="gradient">The gradient drawn by the brush.</param>
|
||||
public RadialGradientBrush(IGradient gradient)
|
||||
{
|
||||
this.Gradient = gradient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RadialGradientBrush"/> class.
|
||||
/// </summary>
|
||||
/// <param name="center">The center point (as percentage in the range [0..1]).</param>
|
||||
/// <param name="gradient">The gradient drawn by the brush.</param>
|
||||
public RadialGradientBrush(Point center, IGradient gradient)
|
||||
{
|
||||
this.Center = center;
|
||||
this.Gradient = gradient;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Color GetColorAtPoint(Rectangle rectangle, BrushRenderTarget renderTarget)
|
||||
{
|
||||
if (Gradient == null) return Color.Transparent;
|
||||
|
||||
Point centerPoint = new Point(rectangle.Location.X + (rectangle.Size.Width * Center.X), rectangle.Location.Y + (rectangle.Size.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 ...
|
||||
double refDistance = Math.Max(Math.Max(Math.Max(GradientHelper.CalculateDistance(rectangle.Location, centerPoint),
|
||||
GradientHelper.CalculateDistance(new Point(rectangle.Location.X + rectangle.Size.Width, rectangle.Location.Y), centerPoint)),
|
||||
GradientHelper.CalculateDistance(new Point(rectangle.Location.X, rectangle.Location.Y + rectangle.Size.Height), centerPoint)),
|
||||
GradientHelper.CalculateDistance(new Point(rectangle.Location.X + rectangle.Size.Width, rectangle.Location.Y + rectangle.Size.Height), centerPoint));
|
||||
|
||||
double distance = GradientHelper.CalculateDistance(renderTarget.Point, centerPoint);
|
||||
double offset = distance / refDistance;
|
||||
return Gradient.GetColor(offset);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
67
RGB.NET.Brushes/Brushes/SolidColorBrush.cs
Normal file
67
RGB.NET.Brushes/Brushes/SolidColorBrush.cs
Normal file
@ -0,0 +1,67 @@
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
|
||||
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a brush drawing only a single color.
|
||||
/// </summary>
|
||||
public class SolidColorBrush : AbstractBrush
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Color"/> drawn by this <see cref="SolidColorBrush"/>.
|
||||
/// </summary>
|
||||
public Color Color { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SolidColorBrush"/> class.
|
||||
/// </summary>
|
||||
/// <param name="color">The <see cref="Color"/> drawn by this <see cref="SolidColorBrush"/>.</param>
|
||||
public SolidColorBrush(Color color)
|
||||
{
|
||||
this.Color = color;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Color GetColorAtPoint(Rectangle rectangle, BrushRenderTarget renderTarget)
|
||||
{
|
||||
return Color;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operators
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="Color" /> to a <see cref="SolidColorBrush" />.
|
||||
/// </summary>
|
||||
/// <param name="color">The <see cref="Color"/> to convert.</param>
|
||||
public static explicit operator SolidColorBrush(Color color)
|
||||
{
|
||||
return new SolidColorBrush(color);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="SolidColorBrush" /> to a <see cref="Color" />.
|
||||
/// </summary>
|
||||
/// <param name="brush">The <see cref="Color"/> to convert.</param>
|
||||
public static implicit operator Color(SolidColorBrush brush)
|
||||
{
|
||||
return brush.Color;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
87
RGB.NET.Brushes/Gradients/AbstractGradient.cs
Normal file
87
RGB.NET.Brushes/Gradients/AbstractGradient.cs
Normal file
@ -0,0 +1,87 @@
|
||||
// ReSharper disable MemberCanBeProtected.Global
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes.Gradients
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a basic gradient.
|
||||
/// </summary>
|
||||
public abstract class AbstractGradient : IGradient
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of the stops used by this <see cref="AbstractGradient"/>.
|
||||
/// </summary>
|
||||
public IList<GradientStop> GradientStops { get; } = new List<GradientStop>();
|
||||
|
||||
/// <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; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AbstractGradient"/> class.
|
||||
/// </summary>
|
||||
protected AbstractGradient()
|
||||
{ }
|
||||
|
||||
/// <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)
|
||||
{
|
||||
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;
|
||||
|
||||
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"></param>
|
||||
/// <returns></returns>
|
||||
protected double ClipOffset(double offset)
|
||||
{
|
||||
double max = GradientStops.Max(n => n.Offset);
|
||||
if (offset > max)
|
||||
return max;
|
||||
|
||||
double min = GradientStops.Min(n => n.Offset);
|
||||
return offset < min ? min : offset;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public abstract Color GetColor(double offset);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
42
RGB.NET.Brushes/Gradients/GradientStop.cs
Normal file
42
RGB.NET.Brushes/Gradients/GradientStop.cs
Normal file
@ -0,0 +1,42 @@
|
||||
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes.Gradients
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a stop on a gradient.
|
||||
/// </summary>
|
||||
public class GradientStop
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <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 double Offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Color"/> of this <see cref="GradientStop"/>.
|
||||
/// </summary>
|
||||
public Color Color { get; set; }
|
||||
|
||||
#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(double offset, Color color)
|
||||
{
|
||||
this.Offset = offset;
|
||||
this.Color = color;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
17
RGB.NET.Brushes/Gradients/IGradient.cs
Normal file
17
RGB.NET.Brushes/Gradients/IGradient.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes.Gradients
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a basic gradient.
|
||||
/// </summary>
|
||||
public interface IGradient
|
||||
{
|
||||
/// <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(double offset);
|
||||
}
|
||||
}
|
||||
95
RGB.NET.Brushes/Gradients/LinearGradient.cs
Normal file
95
RGB.NET.Brushes/Gradients/LinearGradient.cs
Normal file
@ -0,0 +1,95 @@
|
||||
// ReSharper disable UnusedMember.Global
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes.Gradients
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a linear interpolated gradient with n stops.
|
||||
/// </summary>
|
||||
public class LinearGradient : AbstractGradient
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LinearGradient"/> class.
|
||||
/// </summary>
|
||||
public LinearGradient()
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LinearGradient"/> class.
|
||||
/// </summary>
|
||||
/// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
|
||||
public LinearGradient(params GradientStop[] gradientStops)
|
||||
: base(gradientStops)
|
||||
{ }
|
||||
|
||||
/// <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="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)
|
||||
{ }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets the linear interpolated <see cref="Color"/> at the given offset.
|
||||
/// </summary>
|
||||
/// <param name="offset">The percentage offset to take the color from.</param>
|
||||
/// <returns>The <see cref="Color"/> at the specific offset.</returns>
|
||||
public override Color GetColor(double offset)
|
||||
{
|
||||
if (GradientStops.Count == 0) return Color.Transparent;
|
||||
if (GradientStops.Count == 1) return new Color(GradientStops.First().Color);
|
||||
|
||||
GradientStop gsBefore;
|
||||
GradientStop gsAfter;
|
||||
|
||||
IList<GradientStop> orderedStops = GradientStops.OrderBy(x => x.Offset).ToList();
|
||||
if (WrapGradient)
|
||||
{
|
||||
gsBefore = orderedStops.LastOrDefault(n => n.Offset <= offset);
|
||||
if (gsBefore == null)
|
||||
{
|
||||
GradientStop lastStop = orderedStops[orderedStops.Count - 1];
|
||||
gsBefore = new GradientStop(lastStop.Offset - 1, lastStop.Color);
|
||||
}
|
||||
|
||||
gsAfter = orderedStops.FirstOrDefault(n => n.Offset >= offset);
|
||||
if (gsAfter == null)
|
||||
{
|
||||
GradientStop firstStop = orderedStops[0];
|
||||
gsAfter = new GradientStop(firstStop.Offset + 1, firstStop.Color);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = ClipOffset(offset);
|
||||
|
||||
gsBefore = orderedStops.Last(n => n.Offset <= offset);
|
||||
gsAfter = orderedStops.First(n => n.Offset >= offset);
|
||||
}
|
||||
|
||||
double blendFactor = 0;
|
||||
if (!gsBefore.Offset.Equals(gsAfter.Offset))
|
||||
blendFactor = ((offset - gsBefore.Offset) / (gsAfter.Offset - gsBefore.Offset));
|
||||
|
||||
byte colA = (byte)(((gsAfter.Color.A - gsBefore.Color.A) * blendFactor) + gsBefore.Color.A);
|
||||
byte colR = (byte)(((gsAfter.Color.R - gsBefore.Color.R) * blendFactor) + gsBefore.Color.R);
|
||||
byte colG = (byte)(((gsAfter.Color.G - gsBefore.Color.G) * blendFactor) + gsBefore.Color.G);
|
||||
byte colB = (byte)(((gsAfter.Color.B - gsBefore.Color.B) * blendFactor) + gsBefore.Color.B);
|
||||
|
||||
return new Color(colA, colR, colG, colB);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
61
RGB.NET.Brushes/Gradients/RainbowGradient.cs
Normal file
61
RGB.NET.Brushes/Gradients/RainbowGradient.cs
Normal file
@ -0,0 +1,61 @@
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
|
||||
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes.Gradients
|
||||
{
|
||||
/// <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 : IGradient
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the hue (in degrees) to start from.
|
||||
/// </summary>
|
||||
public double StartHue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the hue (in degrees) to end the with.
|
||||
/// </summary>
|
||||
public double EndHue { get; set; }
|
||||
|
||||
#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(double startHue = 0, double endHue = 360)
|
||||
{
|
||||
this.StartHue = startHue;
|
||||
this.EndHue = endHue;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets the color on the rainbow at the given 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(double offset)
|
||||
{
|
||||
double range = EndHue - StartHue;
|
||||
double hue = (StartHue + (range * offset)) % 360f;
|
||||
if (hue < 0)
|
||||
hue += 360;
|
||||
return new Color(hue, 1f, 1f);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
80
RGB.NET.Brushes/Helper/GradientHelper.cs
Normal file
80
RGB.NET.Brushes/Helper/GradientHelper.cs
Normal file
@ -0,0 +1,80 @@
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
|
||||
using System;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Brushes.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Offers some extensions and helper-methods for gradient related things.
|
||||
/// </summary>
|
||||
public static class GradientHelper
|
||||
{
|
||||
// Based on https://web.archive.org/web/20170125201230/https://dotupdate.wordpress.com/2008/01/28/find-the-color-of-a-point-in-a-lineargradientbrush/
|
||||
/// <summary>
|
||||
/// Calculates the offset of an given <see cref="Point"/> on an gradient.
|
||||
/// </summary>
|
||||
/// <param name="startPoint">The start <see cref="Point"/> of the gradient.</param>
|
||||
/// <param name="endPoint">The end <see cref="Point"/> of the gradient.</param>
|
||||
/// <param name="point">The <see cref="Point"/> on the gradient to which the offset is calculated.</param>
|
||||
/// <returns>The offset of the <see cref="Point"/> on the gradient.</returns>
|
||||
public static double CalculateLinearGradientOffset(Point startPoint, Point endPoint, Point point)
|
||||
{
|
||||
Point intersectingPoint;
|
||||
if (startPoint.Y.Equals(endPoint.Y)) // Horizontal case
|
||||
intersectingPoint = new Point(point.X, startPoint.Y);
|
||||
|
||||
else if (startPoint.X.Equals(endPoint.X)) // Vertical case
|
||||
intersectingPoint = new Point(startPoint.X, point.Y);
|
||||
|
||||
else // Diagonal case
|
||||
{
|
||||
double slope = (endPoint.Y - startPoint.Y) / (endPoint.X - startPoint.X);
|
||||
double orthogonalSlope = -1 / slope;
|
||||
|
||||
double startYIntercept = startPoint.Y - (slope * startPoint.X);
|
||||
double pointYIntercept = point.Y - (orthogonalSlope * point.X);
|
||||
|
||||
double intersectingPointX = (pointYIntercept - startYIntercept) / (slope - orthogonalSlope);
|
||||
double intersectingPointY = (slope * intersectingPointX) + startYIntercept;
|
||||
intersectingPoint = new Point(intersectingPointX, intersectingPointY);
|
||||
}
|
||||
|
||||
// Calculate distances relative to the vector start
|
||||
double intersectDistance = CalculateDistance(intersectingPoint, startPoint, endPoint);
|
||||
double gradientLength = CalculateDistance(endPoint, startPoint, endPoint);
|
||||
|
||||
return intersectDistance / gradientLength;
|
||||
}
|
||||
|
||||
// Based on https://web.archive.org/web/20170125201230/https://dotupdate.wordpress.com/2008/01/28/find-the-color-of-a-point-in-a-lineargradientbrush/
|
||||
/// <summary>
|
||||
/// Returns the signed magnitude of a <see cref="Point"/> on a vector.
|
||||
/// </summary>
|
||||
/// <param name="point">The <see cref="Point"/> on the vector of which the magnitude should be calculated.</param>
|
||||
/// <param name="origin">The origin of the vector.</param>
|
||||
/// <param name="direction">The direction of the vector.</param>
|
||||
/// <returns>The signed magnitude of a <see cref="Point"/> on a vector.</returns>
|
||||
public static double CalculateDistance(Point point, Point origin, Point direction)
|
||||
{
|
||||
double distance = CalculateDistance(point, origin);
|
||||
|
||||
return (((point.Y < origin.Y) && (direction.Y > origin.Y)) ||
|
||||
((point.Y > origin.Y) && (direction.Y < origin.Y)) ||
|
||||
((point.Y.Equals(origin.Y)) && (point.X < origin.X) && (direction.X > origin.X)) ||
|
||||
((point.Y.Equals(origin.Y)) && (point.X > origin.X) && (direction.X < origin.X)))
|
||||
? -distance : distance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculated the distance between two <see cref="Point"/>.
|
||||
/// </summary>
|
||||
/// <param name="point1">The first <see cref="Point"/>.</param>
|
||||
/// <param name="point2">The second <see cref="Point"/>.</param>
|
||||
/// <returns>The distance between the two <see cref="Point"/>.</returns>
|
||||
public static double CalculateDistance(Point point1, Point point2)
|
||||
{
|
||||
return Math.Sqrt(((point1.Y - point2.Y) * (point1.Y - point2.Y)) + ((point1.X - point2.X) * (point1.X - point2.X)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -31,6 +31,10 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="RGB.NET.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\RGB.NET.Core.1.0.0\lib\net45\RGB.NET.Core.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
@ -41,8 +45,23 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Brushes\ConicalGradientBrush.cs" />
|
||||
<Compile Include="Brushes\IGradientBrush.cs" />
|
||||
<Compile Include="Brushes\LinearGradientBrush.cs" />
|
||||
<Compile Include="Brushes\RadialGradientBrush.cs" />
|
||||
<Compile Include="Brushes\SolidColorBrush.cs" />
|
||||
<Compile Include="Gradients\AbstractGradient.cs" />
|
||||
<Compile Include="Gradients\GradientStop.cs" />
|
||||
<Compile Include="Gradients\IGradient.cs" />
|
||||
<Compile Include="Gradients\LinearGradient.cs" />
|
||||
<Compile Include="Gradients\RainbowGradient.cs" />
|
||||
<Compile Include="Helper\GradientHelper.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
2
RGB.NET.Brushes/RGB.NET.Brushes.csproj.DotSettings
Normal file
2
RGB.NET.Brushes/RGB.NET.Brushes.csproj.DotSettings
Normal file
@ -0,0 +1,2 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=brushes/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
4
RGB.NET.Brushes/packages.config
Normal file
4
RGB.NET.Brushes/packages.config
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="RGB.NET.Core" version="1.0.0" targetFramework="net45" />
|
||||
</packages>
|
||||
Loading…
x
Reference in New Issue
Block a user