1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-12 16:58:29 +00:00

Added own color-type to have more control over and easier manipulation of collors

This commit is contained in:
Darth Affe 2016-09-11 10:40:01 +02:00
parent 5c6dba769b
commit 381ec55349
29 changed files with 271 additions and 93 deletions

View File

@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
using CUE.NET.Helper;
@ -41,7 +42,7 @@ namespace CUE.NET.Brushes
/// <summary>
/// Gets a dictionary containing all colors for points calculated in the last render pass.
/// </summary>
public Dictionary<BrushRenderTarget, Color> RenderedTargets { get; } = new Dictionary<BrushRenderTarget, Color>();
public Dictionary<BrushRenderTarget, CorsairColor> RenderedTargets { get; } = new Dictionary<BrushRenderTarget, CorsairColor>();
/// <summary>
/// Gets the strongly-typed target used for the effect.
@ -97,7 +98,7 @@ namespace CUE.NET.Brushes
/// <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 abstract Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget);
protected abstract CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget);
/// <summary>
/// Finalizes the color by appliing the overall brightness and opacity.<br/>
@ -105,14 +106,14 @@ namespace CUE.NET.Brushes
/// </summary>
/// <param name="color">The color to finalize.</param>
/// <returns>The finalized color.</returns>
protected virtual Color FinalizeColor(Color color)
protected virtual CorsairColor FinalizeColor(CorsairColor 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);
return ColorHelper.ColorFromHSV(color.GetHSVHue(), color.GetHSVSaturation(), finalBrightness, finalAlpha);
}
#endregion

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Drawing;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
@ -36,7 +37,7 @@ namespace CUE.NET.Brushes
/// <summary>
/// Gets a dictionary containing all colors for points calculated in the last render pass.
/// </summary>
Dictionary<BrushRenderTarget, Color> RenderedTargets { get; }
Dictionary<BrushRenderTarget, CorsairColor> RenderedTargets { get; }
/// <summary>
/// Performas the render pass of the brush and calculates the raw colors for all requested points.

View File

@ -0,0 +1,9 @@
using CUE.NET.Gradients;
namespace CUE.NET.Brushes
{
public interface IGradientBrush : IBrush
{
IGradient Gradient { get; }
}
}

View File

@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Brushes
{
@ -66,10 +67,10 @@ namespace CUE.NET.Brushes
/// <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 Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
if (Image == null || Image.Width == 0 || Image.Height == 0)
return Color.Transparent;
return CorsairColor.Transparent;
//TODO DarthAffe 16.03.2016: Refactor to allow more scale-/interpolation-modes
float scaleX = Image.Width / rectangle.Width;

View File

@ -6,6 +6,7 @@
// ReSharper disable UnusedMember.Global
using System.Drawing;
using CUE.NET.Devices.Generic;
using CUE.NET.Gradients;
using CUE.NET.Helper;
@ -14,10 +15,10 @@ namespace CUE.NET.Brushes
/// <summary>
/// Represents a brush drawing a linear gradient.
/// </summary>
public class LinearGradientBrush : AbstractBrush
public class LinearGradientBrush : AbstractBrush, IGradientBrush
{
#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>
@ -74,9 +75,9 @@ namespace CUE.NET.Brushes
/// <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 Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
if (Gradient == null) return Color.Transparent;
if (Gradient == null) return CorsairColor.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);

View File

@ -12,7 +12,7 @@ namespace CUE.NET.Brushes
{
#region Properties & Fields
private Dictionary<CorsairLedId, Color> _colors;
private Dictionary<CorsairLedId, CorsairColor> _colors;
#endregion
@ -22,7 +22,7 @@ namespace CUE.NET.Brushes
/// Initializes a new instance of the <see cref="ProfileBrush"/> class.
/// </summary>
/// <param name="keyLights">The light settings of the CUE profile.</param>
internal ProfileBrush(Dictionary<CorsairLedId, Color> keyLights)
internal ProfileBrush(Dictionary<CorsairLedId, CorsairColor> keyLights)
{
this._colors = keyLights;
}
@ -37,13 +37,13 @@ namespace CUE.NET.Brushes
/// <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 Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
CorsairLed led = CueSDK.KeyboardSDK[renderTarget.LedId];
if (led == null) return Color.Transparent;
if (led == null) return CorsairColor.Transparent;
Color color;
return !_colors.TryGetValue(led.Id, out color) ? Color.Transparent : color;
CorsairColor color;
return !_colors.TryGetValue(led.Id, out color) ? CorsairColor.Transparent : color;
}
#endregion

View File

@ -4,6 +4,7 @@
using System;
using System.Drawing;
using CUE.NET.Devices.Generic;
using CUE.NET.Gradients;
using CUE.NET.Helper;
@ -12,7 +13,7 @@ namespace CUE.NET.Brushes
/// <summary>
/// Represents a brush drawing a radial gradient around a center point.
/// </summary>
public class RadialGradientBrush : AbstractBrush
public class RadialGradientBrush : AbstractBrush, IGradientBrush
{
#region Properties & Fields
@ -66,9 +67,9 @@ namespace CUE.NET.Brushes
/// <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 Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
if(Gradient == null) return Color.Transparent;
if(Gradient == null) return CorsairColor.Transparent;
PointF centerPoint = new PointF(rectangle.X + rectangle.Width * Center.X, rectangle.Y + rectangle.Height * Center.Y);

View File

@ -2,6 +2,7 @@
using System;
using System.Drawing;
using CUE.NET.Devices.Generic;
using CUE.NET.Helper;
namespace CUE.NET.Brushes
@ -27,7 +28,7 @@ namespace CUE.NET.Brushes
/// <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 Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
return ColorHelper.ColorFromHSV((float)_random.NextDouble() * 360f, 1, 1);
}

View File

@ -2,6 +2,7 @@
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System.Drawing;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Brushes
{
@ -15,7 +16,7 @@ namespace CUE.NET.Brushes
/// <summary>
/// Gets or sets the color drawn by the brush.
/// </summary>
public Color Color { get; set; }
public CorsairColor Color { get; set; }
#endregion
@ -25,7 +26,7 @@ namespace CUE.NET.Brushes
/// 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)
public SolidColorBrush(CorsairColor color)
{
this.Color = color;
}
@ -40,7 +41,7 @@ namespace CUE.NET.Brushes
/// <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 Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
return Color;
}
@ -59,6 +60,16 @@ namespace CUE.NET.Brushes
return brush.Color;
}
public static explicit operator SolidColorBrush(CorsairColor color)
{
return new SolidColorBrush(color);
}
public static implicit operator CorsairColor(SolidColorBrush brush)
{
return brush.Color;
}
#endregion
}
}

View File

@ -45,10 +45,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Brushes\IGradientBrush.cs" />
<Compile Include="Brushes\ImageBrush.cs" />
<Compile Include="Brushes\ProfileBrush.cs" />
<Compile Include="Brushes\BrushRenderTarget.cs" />
<Compile Include="CueSDKAutoUpdate.cs" />
<Compile Include="Devices\Generic\CorsairColor.cs" />
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" />

View File

@ -254,7 +254,7 @@ namespace CUE.NET.Devices.Generic
brush.UpdateEffects();
brush.PerformFinalize();
foreach (KeyValuePair<BrushRenderTarget, Color> renders in brush.RenderedTargets)
foreach (KeyValuePair<BrushRenderTarget, CorsairColor> renders in brush.RenderedTargets)
this[renders.Key.LedId].Color = renders.Value;
}
// ReSharper disable once CatchAllClause
@ -263,7 +263,7 @@ namespace CUE.NET.Devices.Generic
private void UpdateLeds(ICollection<LedUpateRequest> updateRequests)
{
updateRequests = updateRequests.Where(x => x.Color != Color.Transparent).ToList();
updateRequests = updateRequests.Where(x => x.Color != CorsairColor.Transparent).ToList();
OnLedsUpdating(updateRequests);

View File

@ -0,0 +1,54 @@
using System.Drawing;
namespace CUE.NET.Devices.Generic
{
public class CorsairColor
{
#region Constants
public static CorsairColor Transparent => Color.Transparent;
#endregion
#region Properties & Fields
public byte A { get; set; }
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
#endregion
#region Constructors
public CorsairColor()
{ }
public CorsairColor(byte r, byte g, byte b) : this(255, r, g, b)
{ }
public CorsairColor(byte a, byte r, byte g, byte b)
{
this.A = a;
this.R = r;
this.G = g;
this.B = b;
}
#endregion
#region Operators
public static implicit operator CorsairColor(Color color)
{
return new CorsairColor(color.A, color.R, color.G, color.B);
}
public static implicit operator Color(CorsairColor color)
{
return Color.FromArgb(color.A, color.R, color.G, color.B);
}
#endregion
}
}

View File

@ -33,13 +33,13 @@ namespace CUE.NET.Devices.Generic
/// <summary>
/// Gets the Color the LED should be set to on the next update.
/// </summary>
public Color RequestedColor { get; private set; } = Color.Transparent;
public CorsairColor RequestedColor { get; private set; } = CorsairColor.Transparent;
private Color _color = Color.Transparent;
private CorsairColor _color = CorsairColor.Transparent;
/// <summary>
/// Gets the current color of the LED. Sets the <see cref="RequestedColor" /> for the next update. />.
/// </summary>
public Color Color
public CorsairColor Color
{
get { return _color; }
set
@ -86,8 +86,8 @@ namespace CUE.NET.Devices.Generic
/// </summary>
internal void Reset()
{
_color = Color.Transparent;
RequestedColor = Color.Transparent;
_color = CorsairColor.Transparent;
RequestedColor = CorsairColor.Transparent;
IsLocked = false;
}

View File

@ -1,7 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System.Drawing;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Devices.Generic
@ -21,7 +20,7 @@ namespace CUE.NET.Devices.Generic
/// <summary>
/// Gets the requested color of the led.
/// </summary>
public Color Color { get; set; }
public CorsairColor Color { get; set; }
#endregion
@ -32,7 +31,7 @@ namespace CUE.NET.Devices.Generic
/// </summary>
/// <param name="ledId">The id of the led to update.</param>
/// <param name="color">The requested color of the led.</param>
public LedUpateRequest(CorsairLedId ledId, Color color)
public LedUpateRequest(CorsairLedId ledId, CorsairColor color)
{
this.LedId = ledId;
this.Color = color;

View File

@ -7,7 +7,8 @@ namespace CUE.NET.Effects
/// <summary>
/// Represents a basic effect targeting an <see cref="IBrush"/>.
/// </summary>
public abstract class AbstractBrushEffect : IEffect<IBrush>
public abstract class AbstractBrushEffect<T> : IEffect<IBrush>
where T : IBrush
{
#region Properties & Fields
@ -19,7 +20,7 @@ namespace CUE.NET.Effects
/// <summary>
/// Gets the <see cref="IBrush"/> this effect is targeting.
/// </summary>
protected IBrush Brush { get; set; }
protected T Brush { get; set; }
#endregion
@ -38,7 +39,7 @@ namespace CUE.NET.Effects
/// <returns><c>true</c> if the effect can be attached; otherwise, <c>false</c>.</returns>
public virtual bool CanBeAppliedTo(IBrush target)
{
return true;
return target is T;
}
/// <summary>
@ -47,7 +48,7 @@ namespace CUE.NET.Effects
/// <param name="target">The <see cref="IBrush"/> this effect is attached to.</param>
public virtual void OnAttach(IBrush target)
{
Brush = target;
Brush = (T)target;
}
/// <summary>
@ -56,9 +57,15 @@ namespace CUE.NET.Effects
/// <param name="target">The <see cref="IBrush"/> this effect is detached from.</param>
public virtual void OnDetach(IBrush target)
{
Brush = null;
Brush = default(T);
}
#endregion
}
/// <summary>
/// Represents a basic effect targeting an <see cref="IBrush"/>.
/// </summary>
public abstract class AbstractBrushEffect : AbstractBrushEffect<IBrush>
{ }
}

View File

@ -9,7 +9,8 @@ namespace CUE.NET.Effects
/// <summary>
/// Represents a basic effect targeting an <see cref="ILedGroup"/>.
/// </summary>
public abstract class AbstractLedGroupEffect : IEffect<ILedGroup>
public abstract class AbstractLedGroupEffect<T> : IEffect<ILedGroup>
where T : ILedGroup
{
#region Properties & Fields
@ -21,7 +22,7 @@ namespace CUE.NET.Effects
/// <summary>
/// Gets the <see cref="ILedGroup"/> this effect is targeting.
/// </summary>
protected ILedGroup LedGroup { get; set; }
protected T LedGroup { get; set; }
#endregion
@ -40,7 +41,7 @@ namespace CUE.NET.Effects
/// <returns><c>true</c> if the effect can be attached; otherwise, <c>false</c>.</returns>
public virtual bool CanBeAppliedTo(ILedGroup target)
{
return true;
return target is T;
}
/// <summary>
@ -49,7 +50,7 @@ namespace CUE.NET.Effects
/// <param name="target">The <see cref="ILedGroup"/> this effect is attached to.</param>
public virtual void OnAttach(ILedGroup target)
{
LedGroup = target;
LedGroup = (T)target;
}
/// <summary>
@ -58,9 +59,15 @@ namespace CUE.NET.Effects
/// <param name="target">The <see cref="ILedGroup"/> this effect is detached from.</param>
public virtual void OnDetach(ILedGroup target)
{
LedGroup = null;
LedGroup = default(T);
}
#endregion
}
/// <summary>
/// Represents a basic effect targeting an <see cref="ILedGroup"/>.
/// </summary>
public abstract class AbstractLedGroupEffect : AbstractLedGroupEffect<ILedGroup>
{ }
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Drawing;
using CUE.NET;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Keyboard;
using CUE.NET.Exceptions;
@ -65,10 +66,10 @@ namespace Example_AudioAnalyzer_full
CueSDK.UpdateMode = UpdateMode.Continuous;
// Add a black background. We want this to be semi-transparent to add some sort of fade-effect - this will smooth everything out a bit
// Note that this isn't a 'real effect' since it's update-rate dependent. A real effect would do always the same thing not mather how fast the keyboard updates.
_keyboard.Brush = new SolidColorBrush(Color.FromArgb(96, 0, 0, 0));
_keyboard.Brush = new SolidColorBrush(new CorsairColor(96, 0, 0, 0));
// Add our song-beat-effect. Remember to uncomment the update in the spectrum effect if you want to remove this.
ListLedGroup songBeatGroup = new ListLedGroup(_keyboard, _keyboard);
songBeatGroup.Brush = new SolidColorBrush(Color.FromArgb(127, 164, 164, 164));
songBeatGroup.Brush = new SolidColorBrush(new CorsairColor(127, 164, 164, 164));
songBeatGroup.Brush.AddEffect(new SongBeatEffect(_soundDataProcessor));
// Add our spectrum-effect using the soundDataProcessor and a rainbow from purple to red as gradient

View File

@ -1,6 +1,7 @@
using System;
using System.Drawing;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;
using CUE.NET.Gradients;
using Example_AudioAnalyzer_full.TakeAsIs;
@ -28,9 +29,9 @@ namespace Example_AudioAnalyzer_full
#region Methods
protected override Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
protected override CorsairColor GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
if (_soundDataProcessor?.BarValues == null) return Color.Transparent;
if (_soundDataProcessor?.BarValues == null) return CorsairColor.Transparent;
// This logic is also stolen from AterialDawn
int barSampleIndex = (int)Math.Floor(_soundDataProcessor.BarValues.Length * (renderTarget.Point.X / (rectangle.X + rectangle.Width))); // Calculate bar sampling index
@ -38,7 +39,7 @@ namespace Example_AudioAnalyzer_full
float verticalPos = (renderTarget.Point.Y / rectangle.Height);
// If the barHeight is lower than the vertical pos currently calculated return the brush value. Otherwise do nothing by returning transparent.
return curBarHeight <= verticalPos ? base.GetColorAtPoint(rectangle, renderTarget) : Color.Transparent;
return curBarHeight <= verticalPos ? base.GetColorAtPoint(rectangle, renderTarget) : CorsairColor.Transparent;
}
#endregion

View File

@ -0,0 +1,37 @@
using CUE.NET.Brushes;
using CUE.NET.Effects;
using CUE.NET.Gradients;
namespace SimpleDevTest
{
public class MoveRainbowEffect : AbstractBrushEffect<IGradientBrush>
{
#region Properties & Fields
public float DegreePerSecond { get; set; } = 30f;
#endregion
#region Constructors
#endregion
#region Methods
public override void Update(float deltaTime)
{
float value = DegreePerSecond * deltaTime;
//DarthAffe 11.09.2016: Only to test! This will overflow if run for a longer time!!!
((RainbowGradient)Brush.Gradient).StartHue += value;
((RainbowGradient)Brush.Gradient).EndHue += value;
}
public override bool CanBeAppliedTo(IBrush target)
{
return (target as IGradientBrush)?.Gradient is RainbowGradient;
}
#endregion
}
}

View File

@ -39,17 +39,15 @@ namespace SimpleDevTest
CueSDK.UpdateMode = UpdateMode.Continuous;
IBrush rainbowBrush = new LinearGradientBrush(new RainbowGradient());
rainbowBrush.AddEffect(new FlashEffect());
rainbowBrush.AddEffect(new FlashEffect { Attack = 5f, Sustain = 1f, Decay = 0, Release = 5f, Interval = 1f });
rainbowBrush.AddEffect(new MoveRainbowEffect());
rainbowBrush.AddEffect(new RemoveRedEffect());
AddTestBrush(CueSDK.KeyboardSDK, rainbowBrush);
AddTestBrush(CueSDK.KeyboardSDK, rainbowBrush);
AddTestBrush(CueSDK.KeyboardSDK, rainbowBrush);
AddTestBrush(CueSDK.MouseSDK, rainbowBrush);
AddTestBrush(CueSDK.HeadsetSDK, rainbowBrush);
AddTestBrush(CueSDK.MousematSDK, rainbowBrush);
Wait(10);
//// Get connected keyboard or throw exception if there is no light controllable keyboard connected
//CorsairKeyboard keyboard = CueSDK.KeyboardSDK;
//if (keyboard == null)

View File

@ -0,0 +1,14 @@
using CUE.NET.Devices.Generic;
using CUE.NET.Effects;
namespace SimpleDevTest
{
public class RemoveRedEffect : AbstractBrushEffect
{
public override void Update(float deltaTime)
{
foreach (CorsairColor colors in Brush.RenderedTargets.Values)
colors.R = 0;
}
}
}

View File

@ -47,8 +47,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MoveRainbowEffect.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RemoveRedEffect.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config">

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Gradients
{
@ -65,7 +66,7 @@ namespace CUE.NET.Gradients
/// </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);
public abstract CorsairColor GetColor(float offset);
#endregion
}

View File

@ -2,6 +2,7 @@
// ReSharper disable MemberCanBePrivate.Global
using System.Drawing;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Gradients
{
@ -20,7 +21,7 @@ namespace CUE.NET.Gradients
/// <summary>
/// Gets or sets the color of the stop.
/// </summary>
public Color Color { get; set; }
public CorsairColor Color { get; set; }
#endregion
@ -31,7 +32,7 @@ namespace CUE.NET.Gradients
/// </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)
public GradientStop(float offset, CorsairColor color)
{
this.Offset = offset;
this.Color = color;

View File

@ -1,4 +1,4 @@
using System.Drawing;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Gradients
{
@ -12,6 +12,6 @@ namespace CUE.NET.Gradients
/// </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);
CorsairColor GetColor(float offset);
}
}

View File

@ -2,6 +2,7 @@
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Gradients
{
@ -35,9 +36,9 @@ namespace CUE.NET.Gradients
/// </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)
public override CorsairColor GetColor(float offset)
{
if (!GradientStops.Any()) return Color.Transparent;
if (!GradientStops.Any()) return CorsairColor.Transparent;
if (GradientStops.Count == 1) return GradientStops.First().Color;
offset = ClipOffset(offset);
@ -54,7 +55,7 @@ namespace CUE.NET.Gradients
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);
return new CorsairColor(colA, colR, colG, colB);
}
#endregion

View File

@ -1,7 +1,7 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System.Drawing;
using CUE.NET.Devices.Generic;
using CUE.NET.Helper;
namespace CUE.NET.Gradients
@ -50,7 +50,7 @@ namespace CUE.NET.Gradients
/// </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)
public CorsairColor GetColor(float offset)
{
float range = EndHue - StartHue;
float hue = (StartHue + (range * offset)) % 360f;

View File

@ -1,7 +1,7 @@
// ReSharper disable MemberCanBePrivate.Global
using System;
using System.Drawing;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Helper
{
@ -13,57 +13,56 @@ namespace CUE.NET.Helper
#region byte/float conversion
/// <summary>
/// Converts the alpha-value of the <see cref="Color"/> to an float value int the range [0..1].
/// Converts the alpha-value of the <see cref="CorsairColor"/> to an float value int the range [0..1].
/// </summary>
/// <param name="color">The color to take the alpha-value from.</param>
/// <returns>The float-value in the range of [0..1]</returns>
public static float GetFloatA(this Color color)
public static float GetFloatA(this CorsairColor color)
{
return color.A / 255f;
}
/// <summary>
/// Converts the red-value of the <see cref="Color"/> to an float value int the range [0..1].
/// Converts the red-value of the <see cref="CorsairColor"/> to an float value int the range [0..1].
/// </summary>
/// <param name="color">The color to take the red-value from.</param>
/// <returns>The float-value in the range of [0..1]</returns>
public static float GetFloatR(this Color color)
public static float GetFloatR(this CorsairColor color)
{
return color.R / 255f;
}
/// <summary>
/// Converts the green-value of the <see cref="Color"/> to an float value int the range [0..1].
/// Converts the green-value of the <see cref="CorsairColor"/> to an float value int the range [0..1].
/// </summary>
/// <param name="color">The color to take the green-value from.</param>
/// <returns>The float-value in the range of [0..1]</returns>
public static float GetFloatG(this Color color)
public static float GetFloatG(this CorsairColor color)
{
return color.G / 255f;
}
/// <summary>
/// Converts the blue-value of the <see cref="Color"/> to an float value int the range [0..1].
/// Converts the blue-value of the <see cref="CorsairColor"/> to an float value int the range [0..1].
/// </summary>
/// <param name="color">The color to take the blue-value from.</param>
/// <returns>The float-value in the range of [0..1]</returns>
public static float GetFloatB(this Color color)
public static float GetFloatB(this CorsairColor color)
{
return color.B / 255f;
}
/// <summary>
/// Creates a <see cref="Color"/> object from the respective rgb-float-values in the range [0..1].
/// Creates a <see cref="CorsairColor"/> object from the respective rgb-float-values in the range [0..1].
/// </summary>
/// <param name="a">The alpha-value in the range [0..1].</param>
/// <param name="r">The red-value in the range [0..1].</param>
/// <param name="g">The green-value in the range [0..1].</param>
/// <param name="b">The blue-value in the range [0..1].</param>
/// <returns>The color-object created representing the given values.</returns>
public static Color CreateColorFromFloat(float a, float r, float g, float b)
public static CorsairColor CreateColorFromFloat(float a, float r, float g, float b)
{
// ReSharper disable once ExceptionNotDocumentedOptional
return Color.FromArgb(GetIntColorFromFloat(a), GetIntColorFromFloat(r), GetIntColorFromFloat(g), GetIntColorFromFloat(b));
return new CorsairColor(GetIntColorFromFloat(a), GetIntColorFromFloat(r), GetIntColorFromFloat(g), GetIntColorFromFloat(b));
}
private static byte GetIntColorFromFloat(float f)
@ -83,7 +82,7 @@ namespace CUE.NET.Helper
/// <param name="bg">The background-color.</param>
/// <param name="fg">The foreground-color</param>
/// <returns>The resulting color.</returns>
public static Color Blend(this Color bg, Color fg)
public static CorsairColor Blend(this CorsairColor bg, CorsairColor fg)
{
if (fg.A == 255)
return fg;
@ -103,12 +102,39 @@ namespace CUE.NET.Helper
#region RGB/HSV conversion
// https://en.wikipedia.org/wiki/HSL_and_HSV
/// <summary>
/// Gets the hue-value (HSV-color space) of the color.
/// </summary>
/// <param name="color">The color to take the hue from.</param>
/// <returns>The hue-value (HSV-color space) of the color.</returns>
public static float GetHSVHue(this CorsairColor color)
{
if (color.R == color.G && color.G == color.B) return 0.0f;
float min = Math.Min(Math.Min(color.R, color.G), color.B);
float max = Math.Max(Math.Max(color.R, color.G), color.B);
float hue = 0f;
if (Math.Abs(max - color.R) < float.Epsilon) // r is max
hue = (color.G - color.B) / (max - min);
else if (Math.Abs(max - color.G) < float.Epsilon) // g is max
hue = 2f + (color.B - color.R) / (max - min);
else // b is max
hue = 4f + (color.R - color.G) / (max - min);
hue = hue * 60f;
if (hue < 0f)
hue += 360f;
return hue;
}
/// <summary>
/// Gets the saturation-value (HSV-color space) of the color.
/// </summary>
/// <param name="color">The color to take the saturation from.</param>
/// <returns>The saturation-value (HSV-color space) of the color.</returns>
public static float GetHSVSaturation(this Color color)
public static float GetHSVSaturation(this CorsairColor color)
{
int max = Math.Max(color.R, Math.Max(color.G, color.B));
int min = Math.Min(color.R, Math.Min(color.G, color.B));
@ -123,26 +149,26 @@ namespace CUE.NET.Helper
/// </summary>
/// <param name="color">The color to take the value from.</param>
/// <returns>The value-value (HSV-color space) of the color.</returns>
public static float GetHSVValue(this Color color)
public static float GetHSVValue(this CorsairColor color)
{
return Math.Max(color.R, Math.Max(color.G, color.B)) / 255f;
}
// Based on http://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both/6930407#6930407 as of 27.09.2015
/// <summary>
/// Creates a <see cref="Color"/> object from the respective hsv-float-values in the range [0..1].
/// Creates a <see cref="CorsairColor"/> object from the respective hsv-float-values in the range [0..1].
/// </summary>
/// <param name="hue">The hue of the color.</param>
/// <param name="saturation">The saturation of the color.</param>
/// <param name="value">The value of the color.</param>
/// <param name="alpha">The alpha of the color.</param>
/// <returns>The color-object created representing the given values.</returns>
public static Color ColorFromHSV(float hue, float saturation, float value, byte alpha = 255)
public static CorsairColor ColorFromHSV(float hue, float saturation, float value, byte alpha = 255)
{
if (saturation <= 0.0)
{
int val = GetIntColorFromFloat(value);
return Color.FromArgb(alpha, val, val, val);
byte val = GetIntColorFromFloat(value);
return new CorsairColor(alpha, val, val, val);
}
float hh = (hue % 360f) / 60f;
@ -155,17 +181,17 @@ namespace CUE.NET.Helper
switch (i)
{
case 0:
return Color.FromArgb(alpha, GetIntColorFromFloat(value), GetIntColorFromFloat(t), GetIntColorFromFloat(p));
return new CorsairColor(alpha, GetIntColorFromFloat(value), GetIntColorFromFloat(t), GetIntColorFromFloat(p));
case 1:
return Color.FromArgb(alpha, GetIntColorFromFloat(q), GetIntColorFromFloat(value), GetIntColorFromFloat(p));
return new CorsairColor(alpha, GetIntColorFromFloat(q), GetIntColorFromFloat(value), GetIntColorFromFloat(p));
case 2:
return Color.FromArgb(alpha, GetIntColorFromFloat(p), GetIntColorFromFloat(value), GetIntColorFromFloat(t));
return new CorsairColor(alpha, GetIntColorFromFloat(p), GetIntColorFromFloat(value), GetIntColorFromFloat(t));
case 3:
return Color.FromArgb(alpha, GetIntColorFromFloat(p), GetIntColorFromFloat(q), GetIntColorFromFloat(value));
return new CorsairColor(alpha, GetIntColorFromFloat(p), GetIntColorFromFloat(q), GetIntColorFromFloat(value));
case 4:
return Color.FromArgb(alpha, GetIntColorFromFloat(t), GetIntColorFromFloat(p), GetIntColorFromFloat(value));
return new CorsairColor(alpha, GetIntColorFromFloat(t), GetIntColorFromFloat(p), GetIntColorFromFloat(value));
default:
return Color.FromArgb(alpha, GetIntColorFromFloat(value), GetIntColorFromFloat(p), GetIntColorFromFloat(q));
return new CorsairColor(alpha, GetIntColorFromFloat(value), GetIntColorFromFloat(p), GetIntColorFromFloat(q));
}
}

View File

@ -4,6 +4,7 @@ using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Profiles
@ -20,7 +21,7 @@ namespace CUE.NET.Profiles
/// </summary>
internal string Name { get; }
private Dictionary<CorsairLedId, Color> _colors;
private Dictionary<CorsairLedId, CorsairColor> _colors;
#endregion
@ -72,7 +73,7 @@ namespace CUE.NET.Profiles
return new
{
key = (CorsairLedId)Enum.Parse(typeof(CorsairLedId), name),
color = ColorTranslator.FromHtml(x.Attribute("color").Value)
color = (CorsairColor)ColorTranslator.FromHtml(x.Attribute("color").Value)
};
})
.ToDictionary(x => x.key, x => x.color)