Refactored brushes, gradients, effects and the keyboard to uncouple them

This commit is contained in:
DarthAffe committed 2015-10-18 17:00:27 +02:00
1 parent 5fc7abc9db
commit 630090ac6f
20 files changed
+79 -62

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
// ReSharper disable MemberCanBeProtected.Global
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace CUE.NET.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 gradient.
/// </summary>
public IList<GradientStop> GradientStops { get; } = new List<GradientStop>();
#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);
}
#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 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;
}
/// <summary>
/// Gets the color of the gradient on 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 abstract Color GetColor(float offset);
#endregion
}
}
+42
View File
@@ -0,0 +1,42 @@
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable MemberCanBePrivate.Global
using System.Drawing;
namespace CUE.NET.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 stop. This should be inside the range of [0..1] but it's not necessary.
/// </summary>
public float Offset { get; set; }
/// <summary>
/// Gets or sets the color of the stop.
/// </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 stop.</param>
/// <param name="color">The color of the stop.</param>
public GradientStop(float offset, Color color)
{
this.Offset = offset;
this.Color = color;
}
#endregion
}
}
+17
View File
@@ -0,0 +1,17 @@
using System.Drawing;
namespace CUE.NET.Gradients
{
/// <summary>
/// Represents a basic gradient.
/// </summary>
public interface IGradient
{
/// <summary>
/// Gets the color of the gradient on the specified offset.
/// </summary>
/// <param name="offset">The percentage offset to take the color from.</param>
/// <returns>The color at the specific offset.</returns>
Color GetColor(float offset);
}
}
+60
View File
@@ -0,0 +1,60 @@
using System.Drawing;
using System.Linq;
namespace CUE.NET.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)
{ }
#endregion
#region Methods
/// <summary>
/// Gets the linear interpolated color 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 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
}
}
+61
View File
@@ -0,0 +1,61 @@
// ReSharper disable MemberCanBePrivate.Global
using System.Drawing;
using CUE.NET.Helper;
namespace CUE.NET.Gradients
{
/// <summary>
/// Represents a rainbow gradient which circles through all color colors of the HUE-color-space.<br />
/// See <see cref="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 float StartHue { get; set; }
/// <summary>
/// Gets or sets the hue (in degrees) to end the with.
/// </summary>
public float 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(float startHue = 0f, float endHue = 360f)
{
this.StartHue = startHue;
this.EndHue = endHue;
}
#endregion
#region Methods
#endregion
/// <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(float offset)
{
float range = EndHue - StartHue;
float hue = (StartHue + (range * offset)) % 360f;
if (hue < 0)
hue += 360;
return ColorHelper.ColorFromHSV(hue, 1f, 1f);
}
}
}