7

How can we invert the colors of the image when opening a pdf file in doc-view?

0

2 Answers 2

10

pdf-tools

If you decide to install pdf-tools package (also available on Melpa) to view PDF files (instead of doc-view-mode), you can view the PDF in "midnight mode" by using the default binding C-c C-r m or by doing M-x pdf-view-midnight-minor-mode.

The default colors of this minor mode are,

But you can customize those by customizing the variable pdf-view-midnight-colors.

From C-h v pdf-view-midnight-colors,

Colors used when `pdf-view-midnight-minor-mode' is activated.
This should be a cons (FOREGROUND . BACKGROUND) of colors.

3

I use the following functions to reverse the colors in a PDF. Note that it uses ImageMagick under the hood and will not work without it:

(defun define-doc-view-current-cache-dir ()
  ;; doc-view-current-cache-dir was renamed to doc-view--current-cache-dir in Emacs 24.5
  (or (fboundp 'doc-view-current-cache-dir)
      (defalias 'doc-view-current-cache-dir 'doc-view--current-cache-dir)))
(eval-after-load "doc-view" '(define-doc-view-current-cache-dir))

(defun doc-view-reverse-colors ()
  "Inverts document colors.\n
Requires an installation of ImageMagick (\"convert\")."
  (interactive)
  ;; error out when ImageMagick is not installed
  (if (/= 0 (call-process-shell-command "convert -version"))
      (error "Reverse colors requires ImageMagick (convert)")
    (when (eq major-mode 'doc-view-mode)
      ;; assume current doc-view internals about cache-names
      (let ((file-name (expand-file-name (format "page-%d.png"
                                                 (doc-view-current-page))
                                         (doc-view-current-cache-dir))))
        (call-process-shell-command
         "convert" nil nil nil "-negate" file-name file-name)
        (clear-image-cache)
        (doc-view-goto-page (doc-view-current-page))))))

(defun doc-view-reverse-colors-all-pages ()
  "Inverts document colors on all pages.\n
Requires an installation of ImageMagick (\"convert\")."
  (interactive)
  ;; error out when ImageMagick is not installed
  (if (/= 0 (call-process-shell-command "convert -version"))
      (error "Reverse colors requires ImageMagick (convert)")
    (when (eq major-mode 'doc-view-mode)
      ;; assume current doc-view internals about cache-names
      (let ((orig (doc-view-current-page))
            (page nil))
        (message "Reversing video on all pages...")
        (dotimes (pnum (doc-view-last-page-number))
          (setq page (expand-file-name (format "page-%d.png" (1+ pnum))
                                       (doc-view-current-cache-dir)))
          (call-process-shell-command
           "convert" nil nil nil "-negate" page page))
        (clear-image-cache)
        (doc-view-goto-page orig)
        (message "Done reversing video!")))))
2
  • Thanks a lot for the effort. Your code is interesting indeed.
    – DJJ
    Commented Oct 14, 2015 at 20:06
  • For those people trying to use the above code without success: (doc-view-current-cache-dir) should be replaced with (doc-view--current-cache-dir) Very useful, thank you!
    – Tino
    Commented Nov 28, 2015 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.