Skip to content

Use theme colors in code with modus-themes-with-colors

[ Note that for common cases the following is not not needed. Just rely on the comprehensive overrides we provide (Option for palette overrides). ]

Advanced users may want to apply many colors from the palette of the active Modus theme in their custom code. In such a case, retrieving each value with the function modus-themes-get-color-value is inefficient (Get a single color from the palette). The Lisp macro modus-themes-with-colors provides the requisite functionality. It supplies the current theme’s palette to the code called from inside of it. For example:

emacs-lisp
(modus-themes-with-colors
  (list blue-warmer magenta-cooler fg-added warning variable fg-heading-4))
;; => ("#354fcf" "#531ab6" "#005000" "#884900" "#005e8b" "#721045")

The above return value is for modus-operandi when that is the active theme. Switching to another theme and evaluating this code anew will return the relevant results for that theme (remember that since version 4, the Modus themes consist of many items (Overview)). The same with modus-vivendi as the active theme:

emacs-lisp
(modus-themes-with-colors
  (list blue-warmer magenta-cooler fg-added warning variable fg-heading-4))
;; => ("#79a8ff" "#b6a0ff" "#a0e0a0" "#fec43f" "#00d3d0" "#feacd0")

The modus-themes-with-colors has access to the whole palette of the active theme, meaning that it can instantiate both (i) named colors like ‘blue-warmer’ and (ii) semantic color mappings like ‘warning’. We provide commands to inspect those (Preview theme colors).

Others sections in this manual show how to use the aforementioned macro (Advanced customization). In practice, the use of a hook will also be needed (DIY Use a hook at the post-load-theme phase).