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

Added brightness and opacity to brushes

This resolves #10
This commit is contained in:
Darth Affe 2015-09-27 17:25:06 +02:00
parent 19c64fedb7
commit 3c449b272d
6 changed files with 62 additions and 36 deletions

View File

@ -45,6 +45,7 @@
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" />
<Compile Include="Devices\Keyboard\Brushes\AbstractBrush.cs" />
<Compile Include="Devices\Keyboard\Brushes\GradientStop.cs" />
<Compile Include="Devices\Keyboard\Brushes\IBrush.cs" />
<Compile Include="Devices\Keyboard\Brushes\LinearGradientBrush.cs" />

View File

@ -0,0 +1,41 @@
using System.Drawing;
using CUE.NET.Helper;
namespace CUE.NET.Devices.Keyboard.Brushes
{
public abstract class AbstractBrush : IBrush
{
#region Properties & Fields
public float Brightness { get; set; }
public float Opacity { get; set; }
#endregion
#region Constructors
protected AbstractBrush(float brightness = 1f, float opacity = 1f)
{
this.Brightness = brightness;
this.Opacity = opacity;
}
#endregion
#region Methods
public abstract Color GetColorAtPoint(RectangleF rectangle, PointF point);
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
}
}

View File

@ -4,6 +4,9 @@ namespace CUE.NET.Devices.Keyboard.Brushes
{
public interface IBrush
{
float Brightness { get; set; }
float Opacity { get; set; }
Color GetColorAtPoint(RectangleF rectangle, PointF point);
}
}

View File

@ -2,7 +2,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable ReturnTypeCanBeEnumerable.Global
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
@ -10,7 +9,7 @@ using CUE.NET.Helper;
namespace CUE.NET.Devices.Keyboard.Brushes
{
public class LinearGradientBrush : IBrush
public class LinearGradientBrush : AbstractBrush
{
#region Properties & Fields
@ -44,7 +43,7 @@ namespace CUE.NET.Devices.Keyboard.Brushes
#region Methods
public Color GetColorAtPoint(RectangleF rectangle, PointF point)
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
if (!GradientStops.Any()) return Color.Transparent;
if (GradientStops.Count == 1) return GradientStops.First().Color;
@ -75,7 +74,8 @@ namespace CUE.NET.Devices.Keyboard.Brushes
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);
return FinalizeColor(Color.FromArgb(colA, colR, colG, colB));
}
#endregion

View File

@ -1,10 +1,11 @@
using System;
// ReSharper disable MemberCanBePrivate.Global
using System.Drawing;
using CUE.NET.Helper;
namespace CUE.NET.Devices.Keyboard.Brushes
{
public class RainbowBrush : IBrush
public class RainbowBrush : AbstractBrush
{
#region Properties & Fields
@ -12,61 +13,39 @@ namespace CUE.NET.Devices.Keyboard.Brushes
public PointF EndPoint { get; set; } = new PointF(1f, 0.5f);
public float StartHue { get; set; }
public float EndHue { get; set; }
public int Alpha { get; set; } = 255;
#endregion
#region Constructors
public RainbowBrush(float startHue = 0f, float endHue = 360f, int alpha = 255)
public RainbowBrush(float startHue = 0f, float endHue = 360f)
{
this.StartHue = startHue;
this.EndHue = endHue;
this.Alpha = alpha;
}
public RainbowBrush(PointF startPoint, PointF endPoint, float startHue = 0f, float endHue = 360f, int alpha = 255)
public RainbowBrush(PointF startPoint, PointF endPoint, float startHue = 0f, float endHue = 360f)
{
this.StartPoint = startPoint;
this.EndPoint = endPoint;
this.StartHue = startHue;
this.EndHue = endHue;
this.Alpha = alpha;
}
#endregion
#region Methods
public Color GetColorAtPoint(RectangleF rectangle, PointF point)
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
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.CalculateGradientOffset(startPoint, endPoint, point);
float range = EndHue - StartHue;
float progress = (StartHue + (range * offset)) / 360f;
float div = (Math.Abs(progress % 1) * 6);
int value = (int)((div % 1) * 255);
switch ((int)div)
{
case 0:
return Color.FromArgb(Alpha, 255, value, 0);
case 1:
return Color.FromArgb(Alpha, 255 - value, 255, 0);
case 2:
return Color.FromArgb(Alpha, 0, 255, value);
case 3:
return Color.FromArgb(Alpha, 0, 255 - value, 255);
case 4:
return Color.FromArgb(Alpha, value, 0, 255);
case 5:
return Color.FromArgb(Alpha, 255, 0, 255 - value);
default:
return Color.Transparent;
}
float hue = (StartHue + (range * offset)) % 360f;
return FinalizeColor(ColorHelper.ColorFromHSV(hue, 1f, 1f));
}
#endregion

View File

@ -1,8 +1,10 @@
// ReSharper disable MemberCanBePrivate.Global
using System.Drawing;
namespace CUE.NET.Devices.Keyboard.Brushes
{
public class SolidColorBrush : IBrush
public class SolidColorBrush : AbstractBrush
{
#region Properties & Fields
@ -21,9 +23,9 @@ namespace CUE.NET.Devices.Keyboard.Brushes
#region Methods
public Color GetColorAtPoint(RectangleF rectangle, PointF point)
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
return Color; // A solid color brush returns the same color no matter the point
return FinalizeColor(Color);
}
#endregion