Skip to content

Linux color inversion (rgb, hue rotation, color matrices)

Notifications You must be signed in to change notification settings

vn971/linux-color-inversion

Repository files navigation

Linux color inversion

This repository shows how to do "smart" color inversion on Linux.

The general idea is to transform black-on-white texts to white-on-black, which may reduce the stress on your eyes, especially in dark environments.

There are many ways to do "color inversion" though:

Name First image
(editor)
Second image
(color chooser)
Third image
(gimp chooser)
Original Image
RGB
negation
Matrix
(halfs)
Matrix
(thirds)
RGB shift
RGB shift lowcontrast

RGB negation

The simplest, but probably also the worst.

Black becomes white and vice versa. Color (r,g,b) transforms to (1-r, 1-g, 1-b). It can be applied to your system by installing xcalib, and running xcalib -i -a. To restore, type the same command again.

Color matrix multiplication

This is a relatively good approach, although it does lose some quality (see below).

The idea is to make each (r,g,b) component to be a linear function from original r,g,b values.

The "Matrix (halfs)" picture was generated by the formula: (new_red = 1 - (1/2)*green - (1/2)*blue)

The "Matrix (thirds)" picture was generated by the formula: (new_red = 1 + (1/3)*red - (2/3)*green - (2/3)*blue)

See below on how to enable this color transformation on Linux (compton).

References: these color transformations are also implemented in the Windows application NegativeScreen. I studied those repo matrices while doing this comparison.

RGB shift

This approach preserves the "color" (hue) and maintains color richness (the transformation is a bijection from RGB color space to itself).

Technically, this transformation is the same as doing 180-degree HUE rotation combined with RGB negation.

See below on how to enable this color transformation on Linux (compton). You can test it on images with GIMP:

  1. Menu > Colors > HueSaturation > Hue > 180 > OK.
  2. Menu > Colors > Invert

RGB shift low-contrast (recommended)

Same as RGB shift, but lightens darker colors, making black only a dark gray. This is because it is actually hard for the eye to read absolutely white letter near absolutely black background. The contrast (black against white) blinds you a bit. Having at least some light coming from background helps a lot (in my opinion).

Making it work with "compton"

In order to bring these niceties to your system do:

  • Install compton compositor
  • Clone this repo (or download the *.glsl files)
  • Go into the directory containing the *.glsl files
  • Launch the compositor with: compton --backend glx --glx-fshader-win "$(cat shift_whiteish.glsl)" --invert-color-include id!=0
  • If you use subpixel order for your fonts, consider inverting them simultaneously, too (RGB should become BGR and vice-versa).

If you want to try out other transformations (like matrices, or maybe some of your own glsl), just insert a different file into the command above.

Warning: This will replace your current (xfce/gnome/kde/i3/whatever) compositor. Non-permanently, so if you're not satisfied, kill the compositor and/or start your old one back. Alternatively, log out and log in back.

Alternatives, future work

To the best of my knowledge, these approaches do NOT work:

  • XRenderer
  • gamma ramps modifications (because they are color-independent)
  • xcalib modifications, because it uses gamma ramps
  • and actually everything non-OpenGL-based.

Some discussion on Wayland support can be found here.

MacOS users had an option for RGB shift, but seemingly lost it in 2014: lswank/Tranquility#9

Reported to be working:

A future work might be to find some "colorfullness"-preserving algorithm, in addition to preserving hue and doing some form of brightness inversion. If interested, see wikipedia for "colorfullness" and prepare yourself to experiment with glsl code.

Credits

  • The compiz plugin and the ubuntu forum for showing that the task is possible, and showing it should be OpenGL-based
  • Compton developers
  • NegativeScreen(Windows) developers for providing analysis, matrix examples
  • Kurnevsky Evgeny for suggesting and explaining on how to use compton for such a task.

About

Linux color inversion (rgb, hue rotation, color matrices)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages