Add gamma correction option to midnight mode (and fix a few bugs).#329
Merged
vedang merged 1 commit intovedang:masterfrom Dec 29, 2025
Merged
Add gamma correction option to midnight mode (and fix a few bugs).#329vedang merged 1 commit intovedang:masterfrom
vedang merged 1 commit intovedang:masterfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.