mirror of
https://github.com/DarthAffe/CUE.NET.git
synced 2026-11-11 13:06:24 +00:00
Refactored brushes and gradients
This commit is contained in:
1 parent
3c449b272d
commit
f7b8fe130c
10 files changed
+167
-103
No files matched your search
@@ -0,0 +1,47 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Brushes.Gradient
|
||||
{
|
||||
public abstract class AbstractGradient : IGradient
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
public IList<GradientStop> GradientStops { get; } = new List<GradientStop>();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public AbstractGradient()
|
||||
{ }
|
||||
|
||||
public AbstractGradient(params GradientStop[] gradientStops)
|
||||
{
|
||||
foreach (GradientStop gradientStop in gradientStops)
|
||||
GradientStops.Add(gradientStop);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
protected float ClipOffset(float offset)
|
||||
{
|
||||
float max = GradientStops.Max(n => n.Offset);
|
||||
if (offset > max)
|
||||
return max;
|
||||
|
||||
float min = GradientStops.Min(n => n.Offset);
|
||||
if (offset < min)
|
||||
return min;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
public abstract Color GetColor(float offset);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
using System.Drawing;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Brushes
|
||||
namespace CUE.NET.Devices.Keyboard.Brushes.Gradient
|
||||
{
|
||||
public class GradientStop
|
||||
{
|
||||
@@ -0,0 +1,9 @@
|
||||
using System.Drawing;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Brushes.Gradient
|
||||
{
|
||||
public interface IGradient
|
||||
{
|
||||
Color GetColor(float offset);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Brushes.Gradient
|
||||
{
|
||||
public class LinearGradient : AbstractGradient
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public LinearGradient()
|
||||
{ }
|
||||
|
||||
public LinearGradient(params GradientStop[] gradientStops)
|
||||
: base(gradientStops)
|
||||
{ }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public override Color GetColor(float offset)
|
||||
{
|
||||
if (!GradientStops.Any()) return Color.Transparent;
|
||||
if (GradientStops.Count == 1) return GradientStops.First().Color;
|
||||
|
||||
offset = ClipOffset(offset);
|
||||
|
||||
GradientStop gsBefore = GradientStops.Where(n => n.Offset <= offset).OrderBy(n => n.Offset).Last();
|
||||
GradientStop gsAfter = GradientStops.Where(n => n.Offset >= offset).OrderBy(n => n.Offset).First();
|
||||
|
||||
float blendFactor = 0f;
|
||||
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 Color.FromArgb(colA, colR, colG, colB);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
|
||||
using System.Drawing;
|
||||
using CUE.NET.Helper;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Brushes.Gradient
|
||||
{
|
||||
public class RainbowGradient : IGradient
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
public float StartHue { get; set; }
|
||||
public float EndHue { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public RainbowGradient(float startHue = 0f, float endHue = 360f)
|
||||
{
|
||||
this.StartHue = startHue;
|
||||
this.EndHue = endHue;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
#endregion
|
||||
|
||||
public Color GetColor(float offset)
|
||||
{
|
||||
float range = EndHue - StartHue;
|
||||
float hue = (StartHue + (range * offset)) % 360f;
|
||||
if (hue < 0)
|
||||
hue += 360;
|
||||
return ColorHelper.ColorFromHSV(hue, 1f, 1f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,8 @@
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// ReSharper disable ReturnTypeCanBeEnumerable.Global
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using CUE.NET.Devices.Keyboard.Brushes.Gradient;
|
||||
using CUE.NET.Helper;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Brushes
|
||||
@@ -15,7 +14,7 @@ namespace CUE.NET.Devices.Keyboard.Brushes
|
||||
|
||||
public PointF StartPoint { get; set; } = new PointF(0f, 0.5f);
|
||||
public PointF EndPoint { get; set; } = new PointF(1f, 0.5f);
|
||||
public IList<GradientStop> GradientStops { get; } = new List<GradientStop>();
|
||||
public IGradient Gradient { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -24,19 +23,16 @@ namespace CUE.NET.Devices.Keyboard.Brushes
|
||||
public LinearGradientBrush()
|
||||
{ }
|
||||
|
||||
public LinearGradientBrush(PointF startPoint, PointF endPoint, params GradientStop[] gradientStops)
|
||||
public LinearGradientBrush(IGradient gradient)
|
||||
{
|
||||
this.Gradient = gradient;
|
||||
}
|
||||
|
||||
public LinearGradientBrush(PointF startPoint, PointF endPoint, IGradient gradient)
|
||||
{
|
||||
this.StartPoint = startPoint;
|
||||
this.EndPoint = endPoint;
|
||||
|
||||
foreach (GradientStop gradientStop in gradientStops)
|
||||
GradientStops.Add(gradientStop);
|
||||
}
|
||||
|
||||
public LinearGradientBrush(params GradientStop[] gradientStops)
|
||||
{
|
||||
foreach (GradientStop gradientStop in gradientStops)
|
||||
GradientStops.Add(gradientStop);
|
||||
this.Gradient = gradient;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -45,37 +41,13 @@ namespace CUE.NET.Devices.Keyboard.Brushes
|
||||
|
||||
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
|
||||
{
|
||||
if (!GradientStops.Any()) return Color.Transparent;
|
||||
if (GradientStops.Count == 1) return GradientStops.First().Color;
|
||||
if (Gradient == null) return Color.Transparent;
|
||||
|
||||
PointF startPoint = new PointF(StartPoint.X * rectangle.Width, StartPoint.Y * rectangle.Height);
|
||||
PointF endPoint = new PointF(EndPoint.X * rectangle.Width, EndPoint.Y * rectangle.Height);
|
||||
|
||||
float offset = GradientHelper.CalculateGradientOffset(startPoint, endPoint, point);
|
||||
|
||||
// Clip the input if before or after the max/min offset values
|
||||
float max = GradientStops.Max(n => n.Offset);
|
||||
if (offset > max)
|
||||
offset = max;
|
||||
|
||||
float min = GradientStops.Min(n => n.Offset);
|
||||
if (offset < min)
|
||||
offset = min;
|
||||
|
||||
// Find gradient stops that surround the input value
|
||||
GradientStop gsBefore = GradientStops.Where(n => n.Offset <= offset).OrderBy(n => n.Offset).Last();
|
||||
GradientStop gsAfter = GradientStops.Where(n => n.Offset >= offset).OrderBy(n => n.Offset).First();
|
||||
|
||||
float blendFactor = 0f;
|
||||
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 FinalizeColor(Color.FromArgb(colA, colR, colG, colB));
|
||||
float offset = GradientHelper.CalculateLinearGradientOffset(startPoint, endPoint, point);
|
||||
return FinalizeColor(Gradient.GetColor(offset));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
|
||||
using System.Drawing;
|
||||
using CUE.NET.Helper;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Brushes
|
||||
{
|
||||
public class RainbowBrush : AbstractBrush
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
public PointF StartPoint { get; set; } = new PointF(0f, 0.5f);
|
||||
public PointF EndPoint { get; set; } = new PointF(1f, 0.5f);
|
||||
public float StartHue { get; set; }
|
||||
public float EndHue { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public RainbowBrush(float startHue = 0f, float endHue = 360f)
|
||||
{
|
||||
this.StartHue = startHue;
|
||||
this.EndHue = endHue;
|
||||
}
|
||||
|
||||
public RainbowBrush(PointF startPoint, PointF endPoint, float startHue = 0f, float endHue = 360f)
|
||||
{
|
||||
this.StartPoint = startPoint;
|
||||
this.EndPoint = endPoint;
|
||||
this.StartHue = startHue;
|
||||
this.EndHue = endHue;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
|
||||
{
|
||||
PointF startPoint = new PointF(StartPoint.X * rectangle.Width, StartPoint.Y * rectangle.Height);
|
||||
PointF endPoint = new PointF(EndPoint.X * rectangle.Width, EndPoint.Y * rectangle.Height);
|
||||
|
||||
float offset = GradientHelper.CalculateGradientOffset(startPoint, endPoint, point);
|
||||
float range = EndHue - StartHue;
|
||||
|
||||
float hue = (StartHue + (range * offset)) % 360f;
|
||||
return FinalizeColor(ColorHelper.ColorFromHSV(hue, 1f, 1f));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user