15

When an image has a transparent background, Emacs of course respects that.

Unfortunately, this can lead to issues with display:

black text on dark background

Is there a way to display a specific color (e.g. white) behind the image? There appears to be some support for it in ImageMagick images.

The specific stuff I'm working with right now can have the background color set, however I have to add an extra few lines to every single figure in order to do that, which is less than ideal. I would like to get it to where inline images displayed in org-mode have the background color set by default.

(Note: this isn't org-babel specific, it just happens to be how I'm getting these images right now)

2 Answers 2

10

After much digging, there does not appear to be a built-in option to adjust this in Org or in general. The image system does not have any way to customize the default background, and org doesn't have a way to set the :background property. However, it does look like most images support the :background display property.

I added this functionality to Org by modifying (read: copy and paste into .emacs.d with a 1-line change) org-display-inline-images.

I won't reproduce the function here, because it is rather lengthy. Line 51 of the function reads:

(setq img (save-match-data (create-image file type nil :width width)))

I defined a new customizable variable org-inline-image-background, which can hold either nil (transparent background) or a color:

(defcustom org-inline-image-background nil
  "The color used as the default background for inline images.
  When nil, use the default face background."
  :group 'org
  :type '(choice color (const nil)))

Then, I added it to line 51:

(setq img (save-match-data (create-image file type nil :width width 
                                                       :background org-inline-image-background)))

This works beautifully and it can be customized using the color picker, so it satisfies all my requirements.

black text light background

8
  • 2
    Looks like you put in a lot of work to figure this thing out. Would you like to submit a patch for this or request this to be added to org-mode by emailing [email protected]? Feb 26, 2016 at 2:25
  • Once I put it through its paces (make sure it doesn't break anything) I'm hoping too. Feb 26, 2016 at 16:04
  • The new Org-mode version 8.0 seems start use overlay to display inline images, Is there a way to modify overlay default background color? Mar 17, 2016 at 2:29
  • @stardiviner that is exactly what this does Mar 17, 2016 at 17:10
  • I checked my Org version, it is 8.3.4. I checked the org-display-inline-images source code, have not found your code definition. Here is the source code: gist.github.com/649a7b36031d6adb4a96 Mar 18, 2016 at 12:24
7

I got a better solution with advice.

(defcustom org-inline-image-background nil
  "The color used as the default background for inline images.
When nil, use the default face background."
  :group 'org
  :type '(choice color (const nil)))

(defun create-image-with-background-color (args)
  "Specify background color of Org-mode inline image through modify `ARGS'."
  (let* ((file (car args))
         (type (cadr args))
         (data-p (caddr args))
         (props (cdddr args)))
    ;; Get this return result style from `create-image'.
    (append (list file type data-p)
            (list :background (or org-inline-image-background (face-background 'default)))
            props)))

(advice-add 'create-image :filter-args
            #'create-image-with-background-color)
3
  • Nice solution. Might I suggest renaming the advice function to remove the org- prefix since the solution applies much more broadly? With non-default themes, the goal is probably a white background (which is frequently not the default face background), so (list :background "white") might be a useful example.
    – ebpa
    Sep 30, 2019 at 2:07
  • 1
    That is user's option, your consider is right. I think your comment is enough for user who found this code. :) Sep 30, 2019 at 3:30
  • Does this still work for you? For me this only seems to work for SVG images with a transparent background, it's not affecting PNG images. I'm using Emacs 29.0.60 on macOS with Org version 9.6.1.
    – gsgx
    Jun 16 at 19:51

Your Answer

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

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