tsct: set color temperature - scripts - various script and utils
 (HTM) git clone git://z3bra.org/scripts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
 (DIR) commit 6078f8fdacf1fe2350048d557a21f549fd156885
 (DIR) parent b8339904da2b7f4c4be81dc822d8ee879c66ab4f
 (HTM) Author: z3bra <willyatmailoodotorg>
       Date:   Tue, 17 Nov 2015 01:20:27 +0100
       
       sct: set color temperature
       
       Diffstat:
         A sct                                 |      93 +++++++++++++++++++++++++++++++
       
       1 file changed, 93 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/sct b/sct
       t@@ -0,0 +1,93 @@
       +#!/usr/bin/tcc -run -lm -lX11 -lXrandr
       +
       +/*
       + * A nifty little tool to set the color temp of the screen
       + * credits to http://www.tedunangst.com/flak/post/sct-set-color-temperature
       + *
       + * > Campfire style: sct 4500
       + * > Dust storm on Mars style: sct 2000
       + * > Coffee free all nighter style: sct 8000
       + */
       +
       +#include <stdio.h>
       +#include <X11/Xlib.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);
       +        }
       +}
       +