Emacs: Toggle Background Color 🚀

By Xah Lee. Date: . Last updated: .

Toggle Background Color

(defun xah-toggle-background-color ()
  "Toggle background color between seashell and honeydew.

URL `http://xahlee.info/emacs/emacs/emacs_toggle_background_color.html'
Created: 2010-03-20
Version: 2024-11-04"
  (interactive)
  (if (get real-this-command 'xstatus)
      (progn
        (set-background-color "seashell")
        (put real-this-command 'xstatus nil))
    (progn
      (set-background-color "honeydew")
      (put real-this-command 'xstatus t))))

Cycle Background Color

(defvar xah-cycle-colors nil "A vector of color names (string) for `xah-cycle-background-color'")

(setq xah-cycle-colors ["cornsilk" "pale green" "pale turquoise" "thistle" "seashell" "honeydew"])

(defun xah-cycle-background-color ()
  "Cycle background color among values in variable `xah-cycle-colors'.

Return the new color name.

URL `http://xahlee.info/emacs/emacs/emacs_toggle_background_color.html'
Created: 2010-03-20
Version: 2024-11-04"
  (interactive)
  (let ((xvalues xah-cycle-colors)
        xstatus-old xstatus-new xcolor-new)
    (setq xstatus-old
          (if (get real-this-command 'xstatus)
              (get real-this-command 'xstatus)
            0))
    (setq xstatus-new (% (+ xstatus-old 1) (length xvalues)))
    (setq xcolor-new (aref xvalues xstatus-new))
    (put real-this-command 'xstatus xstatus-new)
    (set-background-color xcolor-new)
    (message "background color changed to %s" xcolor-new)
    xcolor-new))

Emacs, Color Theme