sct.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* copied from https://https.www.google.com.tedunangst.com/flak/post/sct-set-color-temperature */
  2. /* public domain, do as you wish */
  3. #include <X11/Xlib.h>
  4. #include <X11/Xproto.h>
  5. #include <X11/Xatom.h>
  6. #include <X11/extensions/Xrandr.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9. /* cribbed from redshift, but truncated with 500K steps */
  10. static const struct { float r; float g; float b; } whitepoints[] = {
  11. { 1.00000000, 0.18172716, 0.00000000, }, /* 1000K */
  12. { 1.00000000, 0.42322816, 0.00000000, },
  13. { 1.00000000, 0.54360078, 0.08679949, },
  14. { 1.00000000, 0.64373109, 0.28819679, },
  15. { 1.00000000, 0.71976951, 0.42860152, },
  16. { 1.00000000, 0.77987699, 0.54642268, },
  17. { 1.00000000, 0.82854786, 0.64816570, },
  18. { 1.00000000, 0.86860704, 0.73688797, },
  19. { 1.00000000, 0.90198230, 0.81465502, },
  20. { 1.00000000, 0.93853986, 0.88130458, },
  21. { 1.00000000, 0.97107439, 0.94305985, },
  22. { 1.00000000, 1.00000000, 1.00000000, }, /* 6500K */
  23. { 0.95160805, 0.96983355, 1.00000000, },
  24. { 0.91194747, 0.94470005, 1.00000000, },
  25. { 0.87906581, 0.92357340, 1.00000000, },
  26. { 0.85139976, 0.90559011, 1.00000000, },
  27. { 0.82782969, 0.89011714, 1.00000000, },
  28. { 0.80753191, 0.87667891, 1.00000000, },
  29. { 0.78988728, 0.86491137, 1.00000000, }, /* 10000K */
  30. { 0.77442176, 0.85453121, 1.00000000, },
  31. };
  32. int
  33. main(int argc, char **argv)
  34. {
  35. Display *dpy = XOpenDisplay(NULL);
  36. int screen = DefaultScreen(dpy);
  37. Window root = RootWindow(dpy, screen);
  38. XRRScreenResources *res = XRRGetScreenResourcesCurrent(dpy, root);
  39. int temp = 6500;
  40. if (argc > 1)
  41. temp = atoi(argv[1]);
  42. if (temp < 1000 || temp > 10000)
  43. temp = 6500;
  44. temp -= 1000;
  45. double ratio = temp % 500 / 500.0;
  46. #define AVG(c) whitepoints[temp / 500].c * (1 - ratio) + whitepoints[temp / 500 + 1].c * ratio
  47. double gammar = AVG(r);
  48. double gammag = AVG(g);
  49. double gammab = AVG(b);
  50. int num_crtcs = res->ncrtc;
  51. for (int c = 0; c < res->ncrtc; c++) {
  52. int crtcxid = res->crtcs[c];
  53. XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(dpy, res, crtcxid);
  54. int size = XRRGetCrtcGammaSize(dpy, crtcxid);
  55. XRRCrtcGamma *crtc_gamma = XRRAllocGamma(size);
  56. for (int i = 0; i < size; i++) {
  57. double g = 65535.0 * i / size;
  58. crtc_gamma->red[i] = g * gammar;
  59. crtc_gamma->green[i] = g * gammag;
  60. crtc_gamma->blue[i] = g * gammab;
  61. }
  62. XRRSetCrtcGamma(dpy, crtcxid, crtc_gamma);
  63. XFree(crtc_gamma);
  64. }
  65. }