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

+68
View File
@@ -0,0 +1,68 @@
using System.Drawing;
using CUE.NET.Helper;
namespace CUE.NET.Brushes
{
/// <summary>
/// Represents a basic brush.
/// </summary>
public abstract class AbstractBrush : IBrush
{
#region Properties & Fields
/// <summary>
/// Gets or sets the overall percentage brightness of the brush.
/// </summary>
public float Brightness { get; set; }
/// <summary>
/// Gets or sets the overall percentage opacity of the brush.
/// </summary>
public float Opacity { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AbstractBrush"/> class.
/// </summary>
/// <param name="brightness">The overall percentage brightness of the brush. (default: 1f)</param>
/// <param name="opacity">The overall percentage opacity of the brush. (default: 1f)</param>
protected AbstractBrush(float brightness = 1f, float opacity = 1f)
{
this.Brightness = brightness;
this.Opacity = opacity;
}
#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="point">The point from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
public abstract Color GetColorAtPoint(RectangleF rectangle, PointF point);
/// <summary>
/// Finalizes the color by appliing the overall brightness and opacity.<br/>
/// This method should always be the last call of a <see cref="GetColorAtPoint" /> implementation.
/// </summary>
/// <param name="color">The color to finalize.</param>
/// <returns>The finalized color.</returns>
protected virtual Color FinalizeColor(Color color)
{
// Since we use HSV to calculate there is no way to make a color 'brighter' than 100%
// Be carefull with the naming: Since we use HSV the correct term is 'value' but outside we call it 'brightness'
// THIS IS NOT A HSB CALCULATION!!!
float finalBrightness = color.GetHSVValue() * (Brightness < 0 ? 0 : (Brightness > 1f ? 1f : Brightness));
byte finalAlpha = (byte)(color.A * (Opacity < 0 ? 0 : (Opacity > 1f ? 1f : Opacity)));
return ColorHelper.ColorFromHSV(color.GetHue(), color.GetHSVSaturation(), finalBrightness, finalAlpha);
}
#endregion
}
}
+28
View File
@@ -0,0 +1,28 @@
using System.Drawing;
namespace CUE.NET.Brushes
{
/// <summary>
/// Represents a basic brush.
/// </summary>
public interface IBrush
{
/// <summary>
/// Gets or sets the overall percentage brightness of the brush.
/// </summary>
float Brightness { get; set; }
/// <summary>
/// Gets or sets the overall percentage opacity of the brush.
/// </summary>
float Opacity { get; set; }
/// <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="point">The point from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
Color GetColorAtPoint(RectangleF rectangle, PointF point);
}
}
+88
View File
@@ -0,0 +1,88 @@
// ReSharper disable CollectionNeverUpdated.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable ReturnTypeCanBeEnumerable.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System.Drawing;
using CUE.NET.Gradients;
using CUE.NET.Helper;
namespace CUE.NET.Brushes
{
/// <summary>
/// Represents a brush drawing a linear gradient.
/// </summary>
public class LinearGradientBrush : AbstractBrush
{
#region Properties & Fields
/// <summary>
/// Gets or sets the start point (as percentage in the range [0..1]) of the gradient drawn by the brush. (default: 0f, 0.5f)
/// </summary>
public PointF StartPoint { get; set; } = new PointF(0f, 0.5f);
/// <summary>
/// Gets or sets the end point (as percentage in the range [0..1]) of the gradient drawn by the brush. (default: 1f, 0.5f)
/// </summary>
public PointF EndPoint { get; set; } = new PointF(1f, 0.5f);
/// <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 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 gradient drawn by the brush.</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 point (as percentage in the range [0..1]).</param>
/// <param name="endPoint">The end point (as percentage in the range [0..1]).</param>
/// <param name="gradient">The gradient drawn by the brush.</param>
public LinearGradientBrush(PointF startPoint, PointF 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="point">The point from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
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.CalculateLinearGradientOffset(startPoint, endPoint, point);
return FinalizeColor(Gradient.GetColor(offset));
}
#endregion
}
}
+86
View File
@@ -0,0 +1,86 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System;
using System.Drawing;
using CUE.NET.Gradients;
using CUE.NET.Helper;
namespace CUE.NET.Brushes
{
/// <summary>
/// Represents a brush drawing a radial gradient around a center point.
/// </summary>
public class RadialGradientBrush : AbstractBrush
{
#region Properties & Fields
/// <summary>
/// Gets or sets the center point (as percentage in the range [0..1]) around which the brush should be drawn.
/// </summary>
public PointF Center { get; set; } = new PointF(0.5f, 0.5f);
/// <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="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(PointF center, IGradient gradient)
{
this.Center = center;
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="point">The point from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
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)
// ReSharper disable once RedundantCast - never trust this ...
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
}
}
+35
View File
@@ -0,0 +1,35 @@
using System;
using System.Drawing;
using CUE.NET.Helper;
namespace CUE.NET.Brushes
{
//TODO DarthAffe 30.09.2015: Like this the brush seems kinda useless. Think about making it cool.
/// <summary>
/// Represents a brush drawing random colors.
/// </summary>
public class RandomColorBrush : AbstractBrush
{
#region Properties & Fields
private Random _random = new Random();
#endregion
#region Methods
/// <summary>
/// Gets a random color.
/// </summary>
/// <param name="rectangle">This value isn't used.</param>
/// <param name="point">This value isn't used.</param>
/// <returns>A random color.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
return FinalizeColor(ColorHelper.ColorFromHSV((float)_random.NextDouble() * 360f, 1, 1));
}
#endregion
}
}
+50
View File
@@ -0,0 +1,50 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System.Drawing;
namespace CUE.NET.Brushes
{
/// <summary>
/// Represents a brush drawing only a single color.
/// </summary>
public class SolidColorBrush : AbstractBrush
{
#region Properties & Fields
/// <summary>
/// Gets or sets the color drawn by the brush.
/// </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 color drawn by the brush.</param>
public SolidColorBrush(Color color)
{
this.Color = color;
}
#endregion
#region Methods
/// <summary>
/// Returns the <see cref="Color" /> of the brush.
/// </summary>
/// <param name="rectangle">This value isn't used.</param>
/// <param name="point">This value isn't used.</param>
/// <returns>The <see cref="Color" /> of the brush.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
return FinalizeColor(Color);
}
#endregion
}
}