From 1b4af74b14d2d3752ecd1bf833f72f0f9ab57ba7 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 11 Dec 2016 15:41:22 +0100 Subject: [PATCH] Added gamma-value to brushes to allow a simple color correction --- Brushes/AbstractBrush.cs | 15 ++++++++++++++- Brushes/IBrush.cs | 6 ++++++ Helper/ColorHelper.cs | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Brushes/AbstractBrush.cs b/Brushes/AbstractBrush.cs index 3e9626d..c77f646 100644 --- a/Brushes/AbstractBrush.cs +++ b/Brushes/AbstractBrush.cs @@ -2,6 +2,7 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable VirtualMemberNeverOverridden.Global +using System; using System.Collections.Generic; using System.Drawing; using System.Linq; @@ -34,6 +35,12 @@ namespace CUE.NET.Brushes /// public float Opacity { get; set; } + /// + /// 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. + /// + public float Gamma { get; set; } + /// /// Gets the Rectangle used in the last render pass. /// @@ -58,10 +65,12 @@ namespace CUE.NET.Brushes /// /// The overall percentage brightness of the brush. (default: 1f) /// The overall percentage opacity of the brush. (default: 1f) - protected AbstractBrush(float brightness = 1f, float opacity = 1f) + /// The gamma-value used to correct the colors calculated by the brush. (default: 1f) + protected AbstractBrush(float brightness = 1f, float opacity = 1f, float gamma = 1f) { this.Brightness = brightness; this.Opacity = opacity; + this.Gamma = gamma; } #endregion @@ -113,6 +122,10 @@ namespace CUE.NET.Brushes // 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))); + + if (Math.Abs(Gamma - 1f) > float.Epsilon) + ColorHelper.CorrectGamma(color, Gamma); + return ColorHelper.ColorFromHSV(color.GetHSVHue(), color.GetHSVSaturation(), finalBrightness, finalAlpha); } diff --git a/Brushes/IBrush.cs b/Brushes/IBrush.cs index b831614..1be96ab 100644 --- a/Brushes/IBrush.cs +++ b/Brushes/IBrush.cs @@ -29,6 +29,12 @@ namespace CUE.NET.Brushes /// float Opacity { get; set; } + /// + /// 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. + /// + float Gamma { get; set; } + /// /// Gets the Rectangle used in the last render pass. /// diff --git a/Helper/ColorHelper.cs b/Helper/ColorHelper.cs index d03e20b..8a6e842 100644 --- a/Helper/ColorHelper.cs +++ b/Helper/ColorHelper.cs @@ -99,6 +99,23 @@ namespace CUE.NET.Helper #endregion + #region Color-Correction + + /// + /// Corrects the color using the specified gamma. + /// + /// The color to correct. + /// The gamma value to apply. + 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