From 7ef07ed99d9c30da8206c47a3f54e56c75efcde7 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 15 Jan 2017 13:57:44 +0100 Subject: [PATCH] Added ConicalGradientBrush --- Brushes/ConicalGradientBrush.cs | 103 ++++++++++++++++++++++++++++++ CUE.NET.csproj | 1 + Examples/SimpleDevTest/Program.cs | 11 +++- 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 Brushes/ConicalGradientBrush.cs diff --git a/Brushes/ConicalGradientBrush.cs b/Brushes/ConicalGradientBrush.cs new file mode 100644 index 0000000..bcd9cec --- /dev/null +++ b/Brushes/ConicalGradientBrush.cs @@ -0,0 +1,103 @@ +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable MemberCanBeProtected.Global +// ReSharper disable ReturnTypeCanBeEnumerable.Global +// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global +// ReSharper disable UnusedMember.Global + +using System; +using System.Drawing; +using CUE.NET.Devices.Generic; +using CUE.NET.Gradients; + +namespace CUE.NET.Brushes +{ + /// + /// Represents a brush drawing a conical gradient. + /// + public class ConicalGradientBrush : AbstractBrush, IGradientBrush + { + #region Properties & Fields + + /// + /// Gets or sets the origin (radian-angle) the brush is drawn to. (default: -π/2) + /// + public float Origin { get; set; } = (float)Math.Atan2(-1, 0); + + /// + /// Gets or sets the center point (as percentage in the range [0..1]) of the gradient drawn by the brush. (default: 0.5f, 0.5f) + /// + public PointF Center { get; set; } = new PointF(0.5f, 0.5f); + + /// + /// Gets or sets the gradient drawn by the brush. If null it will default to full transparent. + /// + public IGradient Gradient { get; set; } + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// + public ConicalGradientBrush() + { } + + /// + /// Initializes a new instance of the class. + /// + /// The gradient drawn by the brush. + public ConicalGradientBrush(IGradient gradient) + { + this.Gradient = gradient; + } + + /// + /// Initializes a new instance of the class. + /// + /// The center point (as percentage in the range [0..1]). + /// The gradient drawn by the brush. + public ConicalGradientBrush(PointF center, IGradient gradient) + { + this.Center = center; + this.Gradient = gradient; + } + + /// + /// Initializes a new instance of the class. + /// + /// The center point (as percentage in the range [0..1]). + /// The origin (radian-angle) the brush is drawn to. + /// The gradient drawn by the brush. + public ConicalGradientBrush(PointF center, float origin, IGradient gradient) + { + this.Center = center; + this.Origin = origin; + this.Gradient = gradient; + } + + #endregion + + #region Methods + + /// + /// Gets the color at an specific point assuming the brush is drawn into the given rectangle. + /// + /// The rectangle in which the brush should be drawn. + /// The target (key/point) from which the color should be taken. + /// The color at the specified point. + protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget) + { + float centerX = rectangle.Width * Center.X; + float centerY = rectangle.Height * Center.Y; + + double angle = Math.Atan2(renderTarget.Point.Y - centerY, renderTarget.Point.X - centerX) - Origin; + if (angle < 0) angle += Math.PI * 2; + float offset = (float)(angle / (Math.PI * 2)); + + return Gradient.GetColor(offset); + } + + #endregion + } +} diff --git a/CUE.NET.csproj b/CUE.NET.csproj index 59c5f6e..72493ca 100644 --- a/CUE.NET.csproj +++ b/CUE.NET.csproj @@ -47,6 +47,7 @@ + diff --git a/Examples/SimpleDevTest/Program.cs b/Examples/SimpleDevTest/Program.cs index 51ccd07..c9cc1c5 100644 --- a/Examples/SimpleDevTest/Program.cs +++ b/Examples/SimpleDevTest/Program.cs @@ -36,9 +36,16 @@ namespace SimpleDevTest CueSDK.Initialize(); Console.WriteLine("Initialized with " + CueSDK.LoadedArchitecture + "-SDK"); + float halfKeyboardWidth = CueSDK.KeyboardSDK.DeviceRectangle.Width / 2f; + ILedGroup left = new RectangleLedGroup(CueSDK.KeyboardSDK, new RectangleF(0, 0, halfKeyboardWidth, CueSDK.KeyboardSDK.DeviceRectangle.Height)); + ILedGroup right = new RectangleLedGroup(CueSDK.KeyboardSDK, new RectangleF(halfKeyboardWidth, 0, halfKeyboardWidth, CueSDK.KeyboardSDK.DeviceRectangle.Height)); + //CueSDK.KeyboardSDK.Brush = new LinearGradientBrush(new LinearGradient(true, new GradientStop(0, Color.Blue), new GradientStop(0.5f, Color.Red))); - CueSDK.KeyboardSDK.Brush = new LinearGradientBrush(new RainbowGradient()); - CueSDK.KeyboardSDK.Brush.AddEffect(new MoveGradientEffect()); + left.Brush = new ConicalGradientBrush(new PointF(0.6f, 0.6f), new RainbowGradient(360, 0)); + left.Brush.AddEffect(new MoveGradientEffect()); + + right.Brush = new ConicalGradientBrush(new PointF(0.4f, 0.6f), new RainbowGradient()); + right.Brush.AddEffect(new MoveGradientEffect()); CueSDK.UpdateMode = UpdateMode.Continuous;