1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-12 08:48:30 +00:00

Added ConicalGradientBrush

This commit is contained in:
Darth Affe 2017-01-15 13:57:44 +01:00
parent 92fcbcff2a
commit 7ef07ed99d
3 changed files with 113 additions and 2 deletions

View File

@ -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
{
/// <summary>
/// Represents a brush drawing a conical gradient.
/// </summary>
public class ConicalGradientBrush : AbstractBrush, IGradientBrush
{
#region Properties & Fields
/// <summary>
/// Gets or sets the origin (radian-angle) the brush is drawn to. (default: -π/2)
/// </summary>
public float Origin { get; set; } = (float)Math.Atan2(-1, 0);
/// <summary>
/// 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)
/// </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="ConicalGradientBrush"/> class.
/// </summary>
public ConicalGradientBrush()
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ConicalGradientBrush"/> class.
/// </summary>
/// <param name="gradient">The gradient drawn by the brush.</param>
public ConicalGradientBrush(IGradient gradient)
{
this.Gradient = gradient;
}
/// <summary>
/// Initializes a new instance of the <see cref="ConicalGradientBrush"/> 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 ConicalGradientBrush(PointF center, IGradient gradient)
{
this.Center = center;
this.Gradient = gradient;
}
/// <summary>
/// Initializes a new instance of the <see cref="ConicalGradientBrush"/> class.
/// </summary>
/// <param name="center">The center point (as percentage in the range [0..1]).</param>
/// <param name="origin">The origin (radian-angle) the brush is drawn to.</param>
/// <param name="gradient">The gradient drawn by the brush.</param>
public ConicalGradientBrush(PointF center, float origin, IGradient gradient)
{
this.Center = center;
this.Origin = origin;
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="renderTarget">The target (key/point) from which the color should be taken.</param>
/// <returns>The color at the specified point.</returns>
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
}
}

View File

@ -47,6 +47,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Brushes\ConicalGradientBrush.cs" />
<Compile Include="Brushes\IGradientBrush.cs" /> <Compile Include="Brushes\IGradientBrush.cs" />
<Compile Include="Brushes\ImageBrush.cs" /> <Compile Include="Brushes\ImageBrush.cs" />
<Compile Include="Brushes\ProfileBrush.cs" /> <Compile Include="Brushes\ProfileBrush.cs" />

View File

@ -36,9 +36,16 @@ namespace SimpleDevTest
CueSDK.Initialize(); CueSDK.Initialize();
Console.WriteLine("Initialized with " + CueSDK.LoadedArchitecture + "-SDK"); 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 LinearGradient(true, new GradientStop(0, Color.Blue), new GradientStop(0.5f, Color.Red)));
CueSDK.KeyboardSDK.Brush = new LinearGradientBrush(new RainbowGradient()); left.Brush = new ConicalGradientBrush(new PointF(0.6f, 0.6f), new RainbowGradient(360, 0));
CueSDK.KeyboardSDK.Brush.AddEffect(new MoveGradientEffect()); 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; CueSDK.UpdateMode = UpdateMode.Continuous;