1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-12 16:58:29 +00:00

Added RadialGradientBrush

This commit is contained in:
Darth Affe 2015-09-29 20:06:34 +02:00
parent f7b8fe130c
commit 70f8a54a38
3 changed files with 64 additions and 2 deletions

View File

@ -52,6 +52,7 @@
<Compile Include="Devices\Keyboard\Brushes\Gradient\RainbowGradient.cs" />
<Compile Include="Devices\Keyboard\Brushes\IBrush.cs" />
<Compile Include="Devices\Keyboard\Brushes\LinearGradientBrush.cs" />
<Compile Include="Devices\Keyboard\Brushes\RadialGradientBrush.cs" />
<Compile Include="Devices\Keyboard\Brushes\SolidColorBrush.cs" />
<Compile Include="Devices\Keyboard\Enums\CorsairLogicalKeyboardLayout.cs" />
<Compile Include="Devices\Headset\Enums\CorsairHeadsetLedId.cs" />

View File

@ -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
}
}

View File

@ -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
/// </summary>
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));
}
}
}