mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Gamma ramp tweaks
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace GHelper.Display
|
||||
@@ -29,7 +30,7 @@ namespace GHelper.Display
|
||||
Blue = blue;
|
||||
}
|
||||
|
||||
public DisplayGammaRamp(double brightness = 0.5, double contrast = 0.5, double gamma = 1)
|
||||
public DisplayGammaRamp(double brightness = 1, double contrast = 1, double gamma = 1)
|
||||
: this(
|
||||
CalculateLUT(brightness, contrast, gamma),
|
||||
CalculateLUT(brightness, contrast, gamma),
|
||||
@@ -38,28 +39,60 @@ namespace GHelper.Display
|
||||
{
|
||||
}
|
||||
|
||||
public DisplayGammaRamp(
|
||||
double redBrightness,
|
||||
double redContrast,
|
||||
double redGamma,
|
||||
double greenBrightness,
|
||||
double greenContrast,
|
||||
double greenGamma,
|
||||
double blueBrightness,
|
||||
double blueContrast,
|
||||
double blueGamma
|
||||
)
|
||||
: this(
|
||||
CalculateLUT(redBrightness, redContrast, redGamma),
|
||||
CalculateLUT(greenBrightness, greenContrast, greenGamma),
|
||||
CalculateLUT(blueBrightness, blueContrast, blueGamma)
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
internal DisplayGammaRamp(GammaRamp ramp) :
|
||||
this(ramp.Red, ramp.Green, ramp.Blue)
|
||||
{
|
||||
}
|
||||
public ushort[] Blue { get; }
|
||||
public ushort[] Green { get; }
|
||||
public ushort[] Red { get; }
|
||||
private static ushort[] CalculateLUT(double brightness, double contrast, double gamma)
|
||||
{
|
||||
|
||||
// Fill the gamma curve
|
||||
var result = new ushort[GammaRamp.DataPoints];
|
||||
string bits = "";
|
||||
|
||||
for (var i = 0; i < result.Length; i++)
|
||||
{
|
||||
result[i] = (ushort)((0.5 + brightness / 2) * ushort.MaxValue * i / (float)(result.Length - 1));
|
||||
bits += result[i].ToString() + " ";
|
||||
}
|
||||
|
||||
//Debug.WriteLine(bits);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ushort[] Brightness(ushort[] data, double brightness)
|
||||
{
|
||||
var result = new ushort[GammaRamp.DataPoints];
|
||||
for (var i = 0; i < result.Length; i++)
|
||||
{
|
||||
result[i] = (ushort)(data[i]*brightness);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal GammaRamp AsBrightnessRamp(double brightness)
|
||||
{
|
||||
return new GammaRamp(
|
||||
Brightness(Red, brightness),
|
||||
Brightness(Green, brightness),
|
||||
Brightness(Blue, brightness)
|
||||
);
|
||||
}
|
||||
|
||||
internal GammaRamp AsRamp()
|
||||
{
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace GHelper.Display
|
||||
|
||||
public const int MAX_REFRESH = 1000;
|
||||
|
||||
public static DisplayGammaRamp? gamma;
|
||||
public static DisplayGammaRamp? gammaRamp;
|
||||
|
||||
public void AutoScreen(bool force = false)
|
||||
{
|
||||
@@ -24,6 +24,29 @@ namespace GHelper.Display
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveGamma()
|
||||
{
|
||||
var screenName = ScreenNative.FindLaptopScreen();
|
||||
if (screenName is null) return;
|
||||
|
||||
try
|
||||
{
|
||||
var handle = ScreenNative.CreateDC(screenName, screenName, null, IntPtr.Zero);
|
||||
var gammaRamp = new GammaRamp();
|
||||
if (ScreenNative.GetDeviceGammaRamp(handle, ref gammaRamp))
|
||||
{
|
||||
var gamma = new DisplayGammaRamp(gammaRamp);
|
||||
Logger.WriteLine("Gamma R: " + string.Join("-", gamma.Red));
|
||||
Logger.WriteLine("Gamma G: " + string.Join("-", gamma.Green));
|
||||
Logger.WriteLine("Gamma B: " + string.Join("-", gamma.Blue));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void SetGamma(int brightness = 100, int contrast = 100)
|
||||
{
|
||||
var bright = (float)(brightness) / 100;
|
||||
@@ -35,8 +58,23 @@ namespace GHelper.Display
|
||||
try
|
||||
{
|
||||
var handle = ScreenNative.CreateDC(screenName, screenName, null, IntPtr.Zero);
|
||||
var gammaRamp = new DisplayGammaRamp(bright, bright, 1).AsRamp();
|
||||
ScreenNative.SetDeviceGammaRamp(handle, ref gammaRamp);
|
||||
if (gammaRamp is null)
|
||||
{
|
||||
var gammaDump = new GammaRamp();
|
||||
if (ScreenNative.GetDeviceGammaRamp(handle, ref gammaDump))
|
||||
{
|
||||
gammaRamp = new DisplayGammaRamp(gammaDump);
|
||||
Logger.WriteLine("Gamma R: " + string.Join("-", gammaRamp.Red));
|
||||
Logger.WriteLine("Gamma G: " + string.Join("-", gammaRamp.Green));
|
||||
Logger.WriteLine("Gamma B: " + string.Join("-", gammaRamp.Blue));
|
||||
}
|
||||
}
|
||||
|
||||
if (gammaRamp is null) gammaRamp = new DisplayGammaRamp();
|
||||
|
||||
var ramp = gammaRamp.AsBrightnessRamp(bright);
|
||||
ScreenNative.SetDeviceGammaRamp(handle, ref ramp);
|
||||
|
||||
} catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine(ex.ToString());
|
||||
|
||||
@@ -39,7 +39,8 @@ namespace GHelper.Display
|
||||
[DllImport("gdi32")]
|
||||
internal static extern bool SetDeviceGammaRamp(IntPtr dcHandle, ref GammaRamp ramp);
|
||||
|
||||
|
||||
[DllImport("gdi32")]
|
||||
internal static extern bool GetDeviceGammaRamp(IntPtr dcHandle, ref GammaRamp ramp);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
||||
public struct DEVMODE
|
||||
|
||||
@@ -260,7 +260,6 @@ namespace GHelper
|
||||
panelPerformance.Focus();
|
||||
}
|
||||
|
||||
|
||||
private void SliderGamma_ValueChanged(object? sender, EventArgs e)
|
||||
{
|
||||
screenControl.SetGamma(sliderGamma.Value);
|
||||
|
||||
Reference in New Issue
Block a user