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

Added color-corrections for brushes

This commit is contained in:
Darth Affe 2016-12-17 14:16:57 +01:00
parent c13d103566
commit da15638817
6 changed files with 142 additions and 34 deletions

View File

@ -2,10 +2,10 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable VirtualMemberNeverOverridden.Global
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CUE.NET.ColorCorrection;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
@ -36,10 +36,9 @@ namespace CUE.NET.Brushes
public float Opacity { get; set; }
/// <summary>
/// Gets or sets the gamma-value used to correct the colors calculated by the brush.
/// Values greater than one will make colors brighter, values less than one will make colors darker.
/// Gets a list of color-corrections used to correct the colors of the brush.
/// </summary>
public float Gamma { get; set; }
public List<IColorCorrection> ColorCorrections { get; } = new List<IColorCorrection>();
/// <summary>
/// Gets the Rectangle used in the last render pass.
@ -65,12 +64,10 @@ namespace CUE.NET.Brushes
/// </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>
/// <param name="gamma">The gamma-value used to correct the colors calculated by the brush. (default: 1f)</param>
protected AbstractBrush(float brightness = 1f, float opacity = 1f, float gamma = 1f)
protected AbstractBrush(float brightness = 1f, float opacity = 1f)
{
this.Brightness = brightness;
this.Opacity = opacity;
this.Gamma = gamma;
}
#endregion
@ -117,8 +114,8 @@ namespace CUE.NET.Brushes
/// <returns>The finalized color.</returns>
protected virtual CorsairColor FinalizeColor(CorsairColor color)
{
if (Math.Abs(Gamma - 1f) > float.Epsilon)
ColorHelper.CorrectGamma(color, Gamma);
foreach (IColorCorrection colorCorrection in ColorCorrections)
colorCorrection.ApplyTo(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'

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Drawing;
using CUE.NET.ColorCorrection;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
@ -30,10 +31,9 @@ namespace CUE.NET.Brushes
float Opacity { get; set; }
/// <summary>
/// Gets or sets the gamma-value used to correct the colors calculated by the brush.
/// Values greater than one will make colors brighter, values less than one will make colors darker.
/// Gets a list of color-corrections used to correct the colors of the brush.
/// </summary>
float Gamma { get; set; }
List<IColorCorrection> ColorCorrections { get; }
/// <summary>
/// Gets the Rectangle used in the last render pass.

View File

@ -51,6 +51,8 @@
<Compile Include="Brushes\ImageBrush.cs" />
<Compile Include="Brushes\ProfileBrush.cs" />
<Compile Include="Brushes\BrushRenderTarget.cs" />
<Compile Include="ColorCorrection\GammaCorrection.cs" />
<Compile Include="ColorCorrection\IColorCorrection.cs" />
<Compile Include="CueSDKAutoUpdate.cs" />
<Compile Include="Devices\Generic\CorsairColor.cs" />
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />

View File

@ -0,0 +1,104 @@
using System;
using CUE.NET.Devices.Generic;
using CUE.NET.Helper;
namespace CUE.NET.ColorCorrection
{
/// <summary>
/// Represents a gamma-color-correction.
/// </summary>
public class GammaCorrection : IColorCorrection
{
#region Properties & Fields
/// <summary>
/// Gets or sets the gamma-value of the color 'red' used for color-correction.
/// Values greater than one will make colors brighter, values less than one will make colors darker.
/// </summary>
public float R { get; set; }
/// <summary>
/// Gets or sets the gamma-value of the color 'green' used for color-correction.
/// Values greater than one will make colors brighter, values less than one will make colors darker.
/// </summary>
public float G { get; set; }
/// <summary>
/// Gets or sets the gamma-value of the color 'blue' used for color-correction.
/// Values greater than one will make colors brighter, values less than one will make colors darker.
/// </summary>
public float B { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GammaCorrection"/> class using the default-value 1f (no correction) for all colors.
/// </summary>
public GammaCorrection() : this(1f) { }
/// <summary>
/// Initializes a new instance of the <see cref="GammaCorrection"/> class.
/// </summary>
/// <param name="gamma">The gamma-value for all colors used for color-correction.
/// Values greater than one will make colors brighter, values less than one will make colors darker.</param>
public GammaCorrection(float gamma)
{
this.R = gamma;
this.G = gamma;
this.B = gamma;
}
/// <summary>
/// Initializes a new instance of the <see cref="GammaCorrection"/> class.
/// </summary>
/// <param name="r">The gamma-value for the color 'red' used for color-correction.
/// Values greater than one will make colors brighter, values less than one will make colors darker.</param>
/// <param name="g">The gamma-value for the color 'green' used for color-correction. Values
/// greater than one will make colors brighter, values less than one will make colors darker.</param>
/// <param name="b">The gamma-value for the color 'blue' used for color-correction. Values
/// greater than one will make colors brighter, values less than one will make colors darker.</param>
public GammaCorrection(float r, float g, float b)
{
this.R = r;
this.G = g;
this.B = b;
}
#endregion
#region Methods
/// <summary>
/// Applies the gamma-correction to the given color.
/// </summary>
/// <param name="color">The color to correct.</param>
public void ApplyTo(CorsairColor color)
{
if (Math.Abs(R - 1f) > float.Epsilon)
color.R = ColorHelper.GetIntColorFromFloat((float)Math.Pow(color.GetFloatR(), 1.0 / R));
if (Math.Abs(G - 1f) > float.Epsilon)
color.G = ColorHelper.GetIntColorFromFloat((float)Math.Pow(color.GetFloatG(), 1.0 / G));
if (Math.Abs(B - 1f) > float.Epsilon)
color.B = ColorHelper.GetIntColorFromFloat((float)Math.Pow(color.GetFloatB(), 1.0 / B));
}
#endregion
#region Operators
/// <summary>
/// Converts a <see cref="float" /> to a <see cref="GammaCorrection" /> using the same value for all colors.
/// </summary>
/// <param name="gamma">The float-value to convert.</param>
public static implicit operator GammaCorrection(float gamma)
{
return new GammaCorrection(gamma);
}
#endregion
}
}

View File

@ -0,0 +1,16 @@
using CUE.NET.Devices.Generic;
namespace CUE.NET.ColorCorrection
{
/// <summary>
/// Represents generic color-correction.
/// </summary>
public interface IColorCorrection
{
/// <summary>
/// Applies the color-correction to the given color.
/// </summary>
/// <param name="color">The color to correct.</param>
void ApplyTo(CorsairColor color);
}
}

View File

@ -1,6 +1,7 @@
// ReSharper disable MemberCanBePrivate.Global
using System;
using CUE.NET.ColorCorrection;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Helper
@ -13,7 +14,7 @@ namespace CUE.NET.Helper
#region byte/float conversion
/// <summary>
/// Converts the alpha-value of the <see cref="CorsairColor"/> to an float value int the range [0..1].
/// Converts the alpha-value of the <see cref="CorsairColor"/> to a float value in 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>
@ -23,7 +24,7 @@ namespace CUE.NET.Helper
}
/// <summary>
/// Converts the red-value of the <see cref="CorsairColor"/> to an float value int the range [0..1].
/// Converts the red-value of the <see cref="CorsairColor"/> to a float value in 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>
@ -33,7 +34,7 @@ namespace CUE.NET.Helper
}
/// <summary>
/// Converts the green-value of the <see cref="CorsairColor"/> to an float value int the range [0..1].
/// Converts the green-value of the <see cref="CorsairColor"/> to a float value in 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>
@ -43,7 +44,7 @@ namespace CUE.NET.Helper
}
/// <summary>
/// Converts the blue-value of the <see cref="CorsairColor"/> to an float value int the range [0..1].
/// Converts the blue-value of the <see cref="CorsairColor"/> to a float value in 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>
@ -65,7 +66,12 @@ namespace CUE.NET.Helper
return new CorsairColor(GetIntColorFromFloat(a), GetIntColorFromFloat(r), GetIntColorFromFloat(g), GetIntColorFromFloat(b));
}
private static byte GetIntColorFromFloat(float f)
/// <summary>
/// Converts the given float-value to a integer-color in the range [0..255].
/// </summary>
/// <param name="f">The float color-value</param>
/// <returns>The integer-value int the range [0..255].</returns>
public static byte GetIntColorFromFloat(float f)
{
// ReSharper disable once RedundantCast - never trust this ...
float calcF = (float)Math.Max(0f, Math.Min(1f, f));
@ -99,23 +105,6 @@ namespace CUE.NET.Helper
#endregion
#region Color-Correction
/// <summary>
/// Corrects the color using the specified gamma.
/// </summary>
/// <param name="color">The color to correct.</param>
/// <param name="gamma">The gamma value to apply.</param>
public static void CorrectGamma(CorsairColor color, float gamma)
{
double value = 1.0 / gamma;
color.R = GetIntColorFromFloat((float)Math.Pow(color.GetFloatR(), value));
color.G = GetIntColorFromFloat((float)Math.Pow(color.GetFloatG(), value));
color.B = GetIntColorFromFloat((float)Math.Pow(color.GetFloatB(), value));
}
#endregion
#region RGB/HSV conversion
// https://en.wikipedia.org/wiki/HSL_and_HSV