Skip to content

Add gamma correction option to midnight mode (and fix a few bugs).#329

Merged
vedang merged 1 commit intovedang:masterfrom
vizziv:master
Dec 29, 2025
Merged

Add gamma correction option to midnight mode (and fix a few bugs).#329
vedang merged 1 commit intovedang:masterfrom
vizziv:master

Conversation

@vizziv
Copy link
Contributor

@vizziv vizziv commented Oct 10, 2025

I use pdf-tools's midnight mode for viewing PDFs produced by LaTeX, but I found that colorful text would often have much lower contrast with the background in midnight mode than the original. As a quick hack to fix this, I added an option that allows gamma correcting the PDF in midnight mode. There are two natural ways to do this (before or after inversion), so there's an option to select the preferred way. Along the way, I found and fixed a few bugs in midnight mode.


Gamma correction means rasing the lightness (scaled to range from 0 to 1) of all colors in an image to some exponent; the chosen exponent is called "gamma". Choosing gamma more than 1 darkens the image, and choosing gamma less than 1 lightens it. The default is 1, which has no effect.

There's another option that determines whether we apply gamma correction before or after inverting lightness. The default is to apply gamma correction after inversion.

I found that I preferred the way things looked when applying the gamma correction before inversion, which is why the option is here. One caveat is that doing the correction before inversion reverses the effect direction, i.e., choosing gamma more than 1 lightens the image, and choosing gamma less than 1 darkens it.

This commit also fixes a midnight mode bug that was present when converting from integer RGB to double RGB: we were dividing by 256 instead of 255. This had a few bad effects. First, it meant that the background was slightly the wrong color. This was often not visible due to rounding, but it became visible when using gamma correction. Second, it meant that the fast path mapping white to the background color never fired.

Finally, just as there's a fast path mapping white to the background color, there was a comment mentioning a fast path mapping black to the foreground color, but it wasn't in the code. So this commit adds that fast path, too.

Gamma correction means rasing the lightness (scaled to range from 0 to
1) of all colors in an image to some exponent; the chosen exponent is
called "gamma". Choosing gamma more than 1 darkens the image, and
choosing gamma less than 1 lightens it. The default is 1, which has no
effect.

There's another option that determines whether we apply gamma
correction before or after inverting lightness. The default is to
apply gamma correction after inversion.

I found that I preferred the way things looked when applying the gamma
correction before inversion, which is why the option is here. One
caveat is that doing the correction before inversion reverses the
effect direction, i.e., choosing gamma more than 1 lightens the image,
and choosing gamma less than 1 darkens it.

This commit also fixes a midnight mode bug that was present when
converting from integer RGB to double RGB: we were dividing by 256
instead of 255. This had a few bad effects. First, it meant that the
background was slightly the wrong color. This was often not visible
due to rounding, but it became visible when using gamma
correction. Second, it meant that the fast path mapping white to the
background color never fired.

Finally, just as there's a fast path mapping white to the background
color, there was a comment mentioning a fast path mapping black to the
foreground color, but it wasn't in the code. So this commit adds that
fast path, too.
@vedang vedang merged commit 8623dc3 into vedang:master Dec 29, 2025
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants