mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Cleanup
This commit is contained in:
71
app/Display/DisplayGammaRamp.cs
Normal file
71
app/Display/DisplayGammaRamp.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace GHelper.Display
|
||||
{
|
||||
|
||||
public class DisplayGammaRamp
|
||||
{
|
||||
|
||||
public DisplayGammaRamp(ushort[] red, ushort[] green, ushort[] blue)
|
||||
{
|
||||
if (red?.Length != GammaRamp.DataPoints)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(red));
|
||||
}
|
||||
|
||||
if (green?.Length != GammaRamp.DataPoints)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(green));
|
||||
}
|
||||
|
||||
if (blue?.Length != GammaRamp.DataPoints)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(blue));
|
||||
}
|
||||
|
||||
Red = red;
|
||||
Green = green;
|
||||
Blue = blue;
|
||||
}
|
||||
|
||||
public DisplayGammaRamp(double brightness = 0.5, double contrast = 0.5, double gamma = 1)
|
||||
: this(
|
||||
CalculateLUT(brightness, contrast, gamma),
|
||||
CalculateLUT(brightness, contrast, gamma),
|
||||
CalculateLUT(brightness, contrast, gamma)
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
internal GammaRamp AsRamp()
|
||||
{
|
||||
return new GammaRamp(Red, Green, Blue);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
56
app/Display/GammaRamp.cs
Normal file
56
app/Display/GammaRamp.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace GHelper.Display
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct GammaRamp
|
||||
{
|
||||
public const int DataPoints = 256;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DataPoints)]
|
||||
public readonly ushort[] Red;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DataPoints)]
|
||||
public readonly ushort[] Green;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DataPoints)]
|
||||
public readonly ushort[] Blue;
|
||||
|
||||
public GammaRamp(ushort[] red, ushort[] green, ushort[] blue)
|
||||
{
|
||||
if (red == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(red));
|
||||
}
|
||||
|
||||
if (green == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(green));
|
||||
}
|
||||
|
||||
if (blue == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(blue));
|
||||
}
|
||||
|
||||
if (red.Length != DataPoints)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(red));
|
||||
}
|
||||
|
||||
if (green.Length != DataPoints)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(green));
|
||||
}
|
||||
|
||||
if (blue.Length != DataPoints)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(blue));
|
||||
}
|
||||
|
||||
Red = red;
|
||||
Green = green;
|
||||
Blue = blue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using WindowsDisplayAPI;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace GHelper.Display
|
||||
{
|
||||
@@ -24,32 +24,24 @@ namespace GHelper.Display
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveGamma()
|
||||
{
|
||||
var display = WindowsDisplayAPI.Display.GetDisplays().Where(x => x.DisplayScreen.IsPrimary).FirstOrDefault();
|
||||
if (display is null) return;
|
||||
|
||||
gamma = display.GammaRamp;
|
||||
|
||||
Logger.WriteLine("R:" + string.Join("-", display.GammaRamp.Red));
|
||||
Logger.WriteLine("G:" + string.Join("-", display.GammaRamp.Green));
|
||||
Logger.WriteLine("B:" + string.Join("-", display.GammaRamp.Blue));
|
||||
}
|
||||
|
||||
public void RestoreGamma()
|
||||
{
|
||||
var display = WindowsDisplayAPI.Display.GetDisplays().Where(x => x.DisplayScreen.IsPrimary).FirstOrDefault();
|
||||
if (gamma is not null) display!.GammaRamp = gamma;
|
||||
}
|
||||
|
||||
public void SetGamma(int brightness = 100, int contrast = 100)
|
||||
{
|
||||
var display = WindowsDisplayAPI.Display.GetDisplays().Where(x => x.DisplayScreen.IsPrimary).FirstOrDefault();
|
||||
var bright = (float)(brightness) / 100;
|
||||
|
||||
Logger.WriteLine("Brightness: " + bright.ToString());
|
||||
|
||||
display!.GammaRamp = new DisplayGammaRamp(bright, bright, 1);
|
||||
var screenName = ScreenNative.FindLaptopScreen();
|
||||
if (screenName is null) return;
|
||||
|
||||
try
|
||||
{
|
||||
var handle = ScreenNative.CreateDC(screenName, screenName, null, IntPtr.Zero);
|
||||
var gammaRamp = new DisplayGammaRamp(bright, bright, 1).AsRamp();
|
||||
ScreenNative.SetDeviceGammaRamp(handle, ref gammaRamp);
|
||||
} catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine(ex.ToString());
|
||||
}
|
||||
|
||||
//ScreenBrightness.Set(60 + (int)(40 * bright));
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,15 @@ namespace GHelper.Display
|
||||
}
|
||||
internal class ScreenNative
|
||||
{
|
||||
|
||||
[DllImport("gdi32", CharSet = CharSet.Unicode)]
|
||||
internal static extern IntPtr CreateDC(string driver, string device, string port, IntPtr deviceMode);
|
||||
|
||||
[DllImport("gdi32")]
|
||||
internal static extern bool SetDeviceGammaRamp(IntPtr dcHandle, ref GammaRamp ramp);
|
||||
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
||||
public struct DEVMODE
|
||||
{
|
||||
@@ -146,7 +155,7 @@ namespace GHelper.Display
|
||||
public const string defaultDevice = @"\\.\DISPLAY1";
|
||||
|
||||
|
||||
private static string? FindInternalName(bool log = false)
|
||||
public static string? FindInternalName(bool log = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user