Skip to content

Instantly share code, notes, and snippets.

@flarn2006
Created February 4, 2016 04:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flarn2006/aa434c8329327d6c8e20 to your computer and use it in GitHub Desktop.
Save flarn2006/aa434c8329327d6c8e20 to your computer and use it in GitHub Desktop.
Screen tinting code for C#
// 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