From 70f8a54a386c72cdf5e2d0dd3304b4e8ec0f68e2 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Tue, 29 Sep 2015 20:06:34 +0200 Subject: [PATCH] Added RadialGradientBrush --- CUE.NET.csproj | 1 + .../Keyboard/Brushes/RadialGradientBrush.cs | 54 +++++++++++++++++++ Helper/GradientHelper.cs | 11 +++- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 Devices/Keyboard/Brushes/RadialGradientBrush.cs diff --git a/CUE.NET.csproj b/CUE.NET.csproj index 1b45987..61a8821 100644 --- a/CUE.NET.csproj +++ b/CUE.NET.csproj @@ -52,6 +52,7 @@ + diff --git a/Devices/Keyboard/Brushes/RadialGradientBrush.cs b/Devices/Keyboard/Brushes/RadialGradientBrush.cs new file mode 100644 index 0000000..57cca44 --- /dev/null +++ b/Devices/Keyboard/Brushes/RadialGradientBrush.cs @@ -0,0 +1,54 @@ +using System; +using System.Drawing; +using CUE.NET.Devices.Keyboard.Brushes.Gradient; +using CUE.NET.Helper; + +namespace CUE.NET.Devices.Keyboard.Brushes +{ + public class RadialGradientBrush : AbstractBrush + { + #region Properties & Fields + + public PointF Center { get; set; } = new PointF(0.5f, 0.5f); + public IGradient Gradient { get; set; } + + #endregion + + #region Constructors + + public RadialGradientBrush() + { } + + public RadialGradientBrush(IGradient gradient) + { + this.Gradient = gradient; + } + + public RadialGradientBrush(PointF center, IGradient gradient) + { + this.Center = center; + this.Gradient = gradient; + } + + #endregion + + #region Methods + + public override Color GetColorAtPoint(RectangleF rectangle, PointF point) + { + PointF centerPoint = new PointF(rectangle.X + rectangle.Width * Center.X, rectangle.Y + rectangle.Height * Center.Y); + + // Calculate the distance to the farthest point from the center as reference (this has to be a corner) + float refDistance = (float)Math.Max(Math.Max(Math.Max(GradientHelper.CalculateDistance(rectangle.Location, centerPoint), + GradientHelper.CalculateDistance(new PointF(rectangle.X + rectangle.Width, rectangle.Y), centerPoint)), + GradientHelper.CalculateDistance(new PointF(rectangle.X, rectangle.Y + rectangle.Height), centerPoint)), + GradientHelper.CalculateDistance(new PointF(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), centerPoint)); + + float distance = GradientHelper.CalculateDistance(point, centerPoint); + float offset = distance / refDistance; + return FinalizeColor(Gradient.GetColor(offset)); + } + + #endregion + } +} diff --git a/Helper/GradientHelper.cs b/Helper/GradientHelper.cs index 4a01073..54dac67 100644 --- a/Helper/GradientHelper.cs +++ b/Helper/GradientHelper.cs @@ -1,4 +1,6 @@ -using System; +// ReSharper disable MemberCanBePrivate.Global + +using System; using System.Drawing; namespace CUE.NET.Helper @@ -41,7 +43,7 @@ namespace CUE.NET.Helper /// public static float CalculateDistance(PointF point, PointF origin, PointF direction) { - float distance = (float)Math.Sqrt((point.Y - origin.Y) * (point.Y - origin.Y) + (point.X - origin.X) * (point.X - origin.X)); + float distance = CalculateDistance(point, origin); return (((point.Y < origin.Y) && (direction.Y > origin.Y)) || ((point.Y > origin.Y) && (direction.Y < origin.Y)) || @@ -49,5 +51,10 @@ namespace CUE.NET.Helper ((point.Y.Equals(origin.Y)) && (point.X > origin.X) && (direction.X < origin.X))) ? -distance : distance; } + + public static float CalculateDistance(PointF point1, PointF point2) + { + return (float)Math.Sqrt((point1.Y - point2.Y) * (point1.Y - point2.Y) + (point1.X - point2.X) * (point1.X - point2.X)); + } } }