summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorZach Kost-Smith <zachkostsmith@gmail.com>2022-01-10 02:41:00 -0600
committerVedang Manerikar <ved.manerikar@gmail.com>2023-01-17 10:36:51 +0530
commitfdb187493fe6f10fea31a76daa98c07db591cd90 (patch)
tree41cf753afc31f161d6b03853b9b8c7bb2e71bbdb /lisp
parent2037c52f127f3971e8f8bcd1a8874bcaa4c8f5ba (diff)
Add support for midnight mode with color
This inversion method attempts to maintain the color hue and saturation but inverts the lightness using the OKLab color space[^1]. [^1]: https://bottosson.github.io/posts/oklab/ * server/epdfinfo.c (image-recolor): Add feature to support the OKLab inversion method functionality * lisp/pdf-view.el (pdf-view-midnight-invert): Add new variable to invert the image color lightness while maintaining hue. (pdf-view-midnight-minor-mode): Account for above. * lisp/pdf-info.el (pdf-info-query--parse-response): Handle changes to :render/usecolors command Closes: #69 Closes: #169 Closes: politza/pdf-tools#698 Closes: politza/pdf-tools#608
Diffstat (limited to 'lisp')
-rw-r--r--lisp/pdf-info.el23
-rw-r--r--lisp/pdf-view.el29
2 files changed, 47 insertions, 5 deletions
diff --git a/lisp/pdf-info.el b/lisp/pdf-info.el
index d3e17bf..7cc4388 100644
--- a/lisp/pdf-info.el
+++ b/lisp/pdf-info.el
@@ -570,8 +570,14 @@ interrupted."
(let ((key (intern (car key-value)))
(value (cadr key-value)))
(cl-case key
- ((:render/printed :render/usecolors)
- (setq value (equal value "1"))))
+ ((:render/printed)
+ (setq value (equal value "1")))
+ ((:render/usecolors)
+ (setq value (ignore-errors
+ (let ((int-val (cl-parse-integer value)))
+ (if (> int-val 0)
+ int-val
+ nil))))))
(push value options)
(push key options)))
options))
@@ -1726,8 +1732,19 @@ Returns a list \(LEFT TOP RIGHT BOT\)."
((:render/foreground :render/background)
(push (pdf-util-hexcolor value)
soptions))
- ((:render/usecolors :render/printed)
+ ((:render/printed)
(push (if value 1 0) soptions))
+ ((:render/usecolors)
+ ;; 0 -> original color
+ ;; 1 -> recolor document to grayscale mapping black to
+ ;; :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))
+ soptions))
(t (push value soptions)))
(push key soptions)))
soptions)))
diff --git a/lisp/pdf-view.el b/lisp/pdf-view.el
index afca46f..6197029 100644
--- a/lisp/pdf-view.el
+++ b/lisp/pdf-view.el
@@ -118,6 +118,20 @@ This should be a cons \(FOREGROUND . BACKGROUND\) of colors."
:type '(cons (color :tag "Foreground")
(color :tag "Background")))
+(defcustom pdf-view-midnight-invert nil
+ "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
+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.
+Nevertheless, this seems to work well in most cases."
+ :group 'pdf-view
+ :type 'boolean)
+
(defcustom pdf-view-change-page-hook nil
"Hook run after changing to another page, but before displaying it.
@@ -1243,7 +1257,16 @@ The colors are determined by the variable
(pdf-info-setoptions
:render/foreground (or (car pdf-view-midnight-colors) "black")
:render/background (or (cdr pdf-view-midnight-colors) "white")
- :render/usecolors t))))
+ :render/usecolors
+ (if pdf-view-midnight-invert
+ ;; If midnight invert is enabled, pass "2" indicating
+ ;; that :render/foreground and :render/background should
+ ;; be ignored and to instead invert the PDF (preserving
+ ;; hue)
+ 2
+ ;; If invert is not enabled, pass "1" indictating that
+ ;; :render/foreground and :render/background should be used
+ 1)))))
(cond
(pdf-view-midnight-minor-mode
(add-hook 'after-save-hook enable nil t)
@@ -1252,7 +1275,9 @@ The colors are determined by the variable
(t
(remove-hook 'after-save-hook enable t)
(remove-hook 'after-revert-hook enable t)
- (pdf-info-setoptions :render/usecolors nil))))
+ (pdf-info-setoptions
+ ;; Value "0" indicates that colors should remain unchanged
+ :render/usecolors 0))))
(pdf-cache-clear-images)
(pdf-view-redisplay t))