commit 387c6cfcd97b2faeaa1e28096e0698bbc857293b
parent 2781ab32ce207523441cbe4ecce967f408c86217
Author: sin <sin@2f30.org>
Date:   Wed, 18 Nov 2015 08:46:49 +0000
add sct - set color temperature by tedu
Diffstat:
| A | sct.c |  |  | 84 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | 
1 file changed, 84 insertions(+), 0 deletions(-)
diff --git a/sct.c b/sct.c
@@ -0,0 +1,84 @@
+/* cc -std=c99 -O2 -I /usr/X11R6/include -o sct sct.c -L /usr/X11R6/lib -lm -lX11 -lXrandr */
+#include <stdio.h>
+#include <X11/Xlib.h>
+#include <X11/Xlibint.h>
+#include <X11/Xproto.h>
+#include <X11/Xatom.h>
+#include <X11/extensions/Xrandr.h>
+#include <X11/extensions/Xrender.h>
+#include <strings.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <math.h>
+
+
+/* cribbed from redshift, but truncated with 500K steps */
+static const struct { float r; float g; float b; } whitepoints[] = {
+	{ 1.00000000,  0.18172716,  0.00000000, }, /* 1000K */
+	{ 1.00000000,  0.42322816,  0.00000000, },
+	{ 1.00000000,  0.54360078,  0.08679949, },
+	{ 1.00000000,  0.64373109,  0.28819679, },
+	{ 1.00000000,  0.71976951,  0.42860152, },
+	{ 1.00000000,  0.77987699,  0.54642268, },
+	{ 1.00000000,  0.82854786,  0.64816570, },
+	{ 1.00000000,  0.86860704,  0.73688797, },
+	{ 1.00000000,  0.90198230,  0.81465502, },
+	{ 1.00000000,  0.93853986,  0.88130458, },
+	{ 1.00000000,  0.97107439,  0.94305985, },
+	{ 1.00000000,  1.00000000,  1.00000000, }, /* 6500K */
+	{ 0.95160805,  0.96983355,  1.00000000, },
+	{ 0.91194747,  0.94470005,  1.00000000, },
+	{ 0.87906581,  0.92357340,  1.00000000, },
+	{ 0.85139976,  0.90559011,  1.00000000, },
+	{ 0.82782969,  0.89011714,  1.00000000, },
+	{ 0.80753191,  0.87667891,  1.00000000, },
+	{ 0.78988728,  0.86491137,  1.00000000, }, /* 10000K */
+	{ 0.77442176,  0.85453121,  1.00000000, },
+};
+
+int
+main(int argc, char **argv)
+{
+	Display *dpy = XOpenDisplay(NULL);
+	int screen = DefaultScreen(dpy);
+	Window root = RootWindow(dpy, screen);
+
+	XRRScreenResources *res = XRRGetScreenResourcesCurrent(dpy, root);
+
+	int temp = 6500;
+	if (argc > 1)
+		temp = atoi(argv[1]);
+	if (temp < 1000 || temp > 10000)
+		temp = 6500;
+
+	temp -= 1000;
+	double ratio = temp % 500 / 500.0;
+#define AVG(c) whitepoints[temp / 500].c * (1 - ratio) + whitepoints[temp / 500 + 1].c * ratio
+	double gammar = AVG(r);
+	double gammag = AVG(g);
+	double gammab = AVG(b);
+
+	int num_crtcs = res->ncrtc;
+	for (int c = 0; c < res->ncrtc; c++) {
+		int crtcxid = res->crtcs[c];
+		XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(dpy, res, crtcxid);
+
+		int size = XRRGetCrtcGammaSize(dpy, crtcxid);
+
+		XRRCrtcGamma *crtc_gamma = XRRAllocGamma(size);
+
+		for (int i = 0; i < size; i++) {
+			double g = 65535.0 * i / size;
+			crtc_gamma->red[i] = g * gammar;
+			crtc_gamma->green[i] = g * gammag;
+			crtc_gamma->blue[i] = g * gammab;
+		}
+		XRRSetCrtcGamma(dpy, crtcxid, crtc_gamma);
+
+		XFree(crtc_gamma);
+	}
+}
+