protesilaos / modus-themes

Highly accessible themes for GNU Emacs, conforming with the highest standard for colour contrast between background and foreground values (WCAG AAA).
https://protesilaos.com/emacs/modus-themes
GNU General Public License v3.0
544 stars 30 forks source link

Updating Colors after modus-themes-toggle #113

Closed domsch1988 closed 1 month ago

domsch1988 commented 1 month ago

I have some custom faces defined for my modeline. To make them fit the modus themes, i'm using modus-themes-get-color-value to assigne foreground and background colors:

(defface domline-highlight
  `((t :height 120 :foreground ,(modus-themes-get-color-value 'bg-main) :background ,(modus-themes-get-color-value 'modeline-info)))
  "Domline Highlight Face")

(defface domline-shadow
  `((t :height 120 :inherit shadow))
  "Domline Shadow Face")

This works fine on initially loading emacs. But the colors don't update on toggeling the theme. Running load-file on my init file after changing theme also doesn't work. I have to specifically go into the file where the defface statements are and run eval-defun on those. Even running eval-buffer on the file doesn't work.

Is there any way to Update these values after a theme change? Or am i better of "repurposing" another face and changing that with overrides to what i want? My entire config is here if it helps: https://gitlab.com/domsch1988/minemacs

protesilaos commented 1 month ago

From: domsch1988 @.***> Date: Thu, 25 Jul 2024 04:19:25 -0700

I have some custom faces defined for my modeline. To make them fit the modus themes, i'm using modus-themes-get-color-value to assigne foreground and background colors:


(defface domline-highlight
  `((t :height 120 :foreground ,(modus-themes-get-color-value 'bg-main) :background ,(modus-themes-get-color-value 'modeline-info)))
  "Domline Highlight Face")

Since you are using 'modus-themes-get-color-value' multiple times, it is probably simpler to wrap your code in the 'modus-themes-with-colors' macro.

Here is the idea:

(defface domline-highlight
  (modus-themes-with-colors
    `((t :height 120 :foreground ,bg-main :background ,modeline-info)))
  "Domline Highlight Face")

[... 6 lines elided]

But the colors don't update on toggeling the theme. Running load-file on my init file after changing theme also doesn't work. I have to specifically go into the file where the defface statements are and run eval-defun on those. Even running eval-buffer on the file doesn't work.

There are two ways to go about it. Either you will expand the definition of your faces to include light and dark variants OR you will write a function that updates the colours after a new theme is loaded. I think the latter approach is easier to apply to more situations, so here it is:

(defun my-modus-themes-update-faces (&rest _)
  (modus-themes-with-colors
    (set-face-attribute 'domline-highlight nil
                        :foreground bg-main :background modeline-info)))

(add-hook 'enable-theme-functions 'my-modus-themes-update-faces)

There are examples like this one in the manual, but let me know if you need me to expand it further.

-- Protesilaos Stavrou https://protesilaos.com

domsch1988 commented 1 month ago

Both of these work perfectly. Hooking the face update to the theme change was what i was looking at, but couldn't exactly figure out how to do that. This does the job, Thanks!

protesilaos commented 1 month ago

From: domsch1988 @.***> Date: Thu, 25 Jul 2024 22:57:28 -0700

Both of these work perfectly. Hooking the face update to the theme change was what i was looking at, but couldn't exactly figure out how to do that. This does the job, Thanks!

You are welcome!

-- Protesilaos Stavrou https://protesilaos.com