Skip to content

Commit

Permalink
Change the default color inversion to maintain hue
Browse files Browse the repository at this point in the history
This commit enables @smithzvk's work in vedang#69 as the default in
`pdf-tools`. It works well for all the PDFs I have tested with.

The previous behaviour can be enabled by setting
`pdf-view-midnight-invert` to `nil`, if required.
  • Loading branch information
vedang committed Jan 17, 2023
1 parent fdb1874 commit bc2ba11
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- Track rotation of pages and add a command to make rotation of pages easy @vizs #165
+ The new command is bound to =R= in `pdf-view-mode-map`.

- Add support for maintaining hue in `pdf-view-midnight-minor-mode` using the OKLab color space @smithzvk #69
+ This inversion method is now enabled by default and can be controlled by the `pdf-view-midnight-invert` variable. Check out the images in the PR for how beautiful the colors look in midnight mode now.

** Functionality fixes and improvements
- Fix: Saving a PDF when ~buffer-file-name~ is missing will now prompt for a filename (eg: in EWW) @akater #178

Expand Down
9 changes: 3 additions & 6 deletions lisp/pdf-info.el
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,9 @@ interrupted."
((:render/usecolors)
(setq value (ignore-errors
(let ((int-val (cl-parse-integer value)))
(if (> int-val 0)
(if (> 3 int-val 0)
int-val
nil))))))
0))))))
(push value options)
(push key options)))
options))
Expand Down Expand Up @@ -1740,10 +1740,7 @@ Returns a list \(LEFT TOP RIGHT BOT\)."
;; :render/foreground and white to :render/background
;; 2 -> recolor document by inverting the perceived lightness
;; preserving hue
(push (cond ((integerp value) value)
;; Map nil -> 0 and t -> 1
(value 1)
(t 0))
(push (if (and (integerp value) (> 3 value 0)) value 0)
soptions))
(t (push value soptions)))
(push key soptions)))
Expand Down
24 changes: 12 additions & 12 deletions lisp/pdf-view.el
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ This should be a cons \(FOREGROUND . BACKGROUND\) of colors."
:type '(cons (color :tag "Foreground")
(color :tag "Background")))

(defcustom pdf-view-midnight-invert nil
(defcustom pdf-view-midnight-invert t
"In midnight mode invert the image color lightness maintaining hue.
This is particularly useful if you are viewing documents with
color coded data in plots. This will maintain the colors such
that 'blue' and 'red' will remain these colors in the inverted
rendering. This inversion is non-trivial. This makes use of the
that blue and red will remain these colors in the inverted
rendering. This inversion is non-trivial. This makes use of the
OKLab color space which is well calibrated to have equal
perceptual brightness across hue, but not all colors are within
the RGB gamut after inversion, causing some colors to saturate.
Expand Down Expand Up @@ -1281,6 +1281,14 @@ The colors are determined by the variable
(pdf-cache-clear-images)
(pdf-view-redisplay t))

(defun pdf-view-set-theme-background ()
"Set the buffer's color filter to correspond to the current Emacs theme."
(pdf-util-assert-pdf-buffer)
(pdf-info-setoptions
:render/foreground (face-foreground 'default nil)
:render/background (face-background 'default nil)
:render/usecolors 1))

(defun pdf-view-refresh-themed-buffer (&optional get-theme)
"Refresh the current buffer to activate applied colors.
Expand All @@ -1292,14 +1300,6 @@ current theme's colors."
(pdf-view-set-theme-background))
(pdf-view-redisplay t))

(defun pdf-view-set-theme-background ()
"Set the buffer's color filter to correspond to the current Emacs theme."
(pdf-util-assert-pdf-buffer)
(pdf-info-setoptions
:render/foreground (face-foreground 'default nil)
:render/background (face-background 'default nil)
:render/usecolors t))

(define-minor-mode pdf-view-themed-minor-mode
"Synchronize color filter with the present Emacs theme.
Expand All @@ -1315,7 +1315,7 @@ The colors are determined by the `face-foreground' and
(t
(remove-hook 'after-save-hook #'pdf-view-set-theme-background t)
(remove-hook 'after-revert-hook #'pdf-view-set-theme-background t)
(pdf-info-setoptions :render/usecolors nil)))
(pdf-info-setoptions :render/usecolors 0)))
(pdf-view-refresh-themed-buffer pdf-view-themed-minor-mode))

(when pdf-view-use-unicode-ligther
Expand Down

0 comments on commit bc2ba11

Please sign in to comment.