Created
February 4, 2016 04:12
-
-
Save flarn2006/aa434c8329327d6c8e20 to your computer and use it in GitHub Desktop.
Screen tinting code for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by flarn2006 <flarn2006@gmail.com> | |
// Licensed under MIT License | |
using System; | |
using System.Runtime.InteropServices; | |
namespace YourNamespaceHere //TODO: change me | |
{ | |
static class ScreenTint | |
{ | |
[DllImport("user32")] | |
private static extern IntPtr GetDC(IntPtr hWnd); | |
[DllImport("gdi32")] | |
private static extern bool GetDeviceGammaRamp(IntPtr hDC, [Out] short[] lpRamp); | |
[DllImport("gdi32")] | |
private static extern bool SetDeviceGammaRamp(IntPtr hDC, short[] lpRamp); | |
private static IntPtr hDC; | |
private static short[] originalRamps; | |
static ScreenTint() | |
{ | |
hDC = GetDC(IntPtr.Zero); | |
originalRamps = new short[768]; | |
GetDeviceGammaRamp(hDC, originalRamps); | |
} | |
public static void Tint(byte red, byte green, byte blue) | |
{ | |
short[] ramps = new short[768]; | |
for (int i=0; i<768; i++) { | |
byte value; | |
if (i < 256) | |
value = red; | |
else if (i < 512) | |
value = green; | |
else | |
value = blue; | |
byte orig = (byte)((originalRamps[i] & 0xFF00) >> 8); | |
ramps[i] = (short)(value * orig); | |
} | |
SetDeviceGammaRamp(hDC, ramps); | |
} | |
public static void Restore() | |
{ | |
SetDeviceGammaRamp(hDC, originalRamps); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment