Let Linux Be Colorless

2023-07-05

The reason for this matter stems from an article I read: Reduce Your Screen Time With This Simple Trick.

A new study shows that, compared to using applications to limit usage time, changing the phone screen to a grayscale is more effective.

Both iOS and Android come with features to switch the screen display to grayscale, primarily aimed at the colorblind people. Windows 10 also has a similar feature. I don't have a macOS device at hand right now, but since iOS has it, there's no reason why macOS shouldn't.

The problem arises with Linux. There is no native tool provided on Linux for this purpose, and the only one that comes close is RedShift.

However, the function of this tool is to adjust the gamma value, so it can only adjust the proportion of the three primary colors (red, green, blue), and cannot turn the screen into grayscale.

The solution I found in the end is to use picom.

Picom is an X compositor that supports OpenGL rendering, so you can write your own OpenGL shader to control the display effect:

#version 330

in vec2 texcoord;
uniform sampler2D tex;

vec4 window_shader() {
    vec4 c = texelFetch(tex, ivec2(texcoord), 0);

    float grayscale = c.x * 0.299 + c.y * 0.587 + c.z * 0.114;
    c.x = grayscale;
    c.y = grayscale;
    c.z = grayscale;
    return c;
}

Because the human eye is more sensitive to different colors, here we use the formula for converting color to grayscale same as the NTSC standard, with a ratio of 0.299:0.587:0.114 for red, green, and blue respectively, to calculate the grayscale value. Then, set red, green, and blue to this grayscale value, which turns the colorful image into grayscale. In fact, using the average intensity of the three colors would yield similar results.

Save the shader as ~/.picom.glsl, and then run picom:

picom --backend glx --window-shader-fg '/home/mistivia/.picom.glsl' &

You'll see the screen turn grayscale. If you want it to run automatically on startup, you can add this command line to .xinitrc. To revert to colorful display, simply kill the picom process.



Email: i (at) mistivia (dot) com