diff options
| author | Protesilaos <info@protesilaos.com> | 2026-05-01 17:35:30 +0300 |
|---|---|---|
| committer | Protesilaos <info@protesilaos.com> | 2026-05-01 17:35:30 +0300 |
| commit | 488e913b1eaf67f789f6c538e5620c94e1ab5c7d (patch) | |
| tree | 911a8a855b20d59f792cbf374c5dae0f40d4c932 | |
| parent | f71db69bad2d4bac680960273b663c73c305a413 (diff) | |
Make helper function accept named colours as well as hex RGB
Otherwise we have a regression. Something like "gray50" is a valid
colour value (when list-colors-display returns it, anyway).
| -rw-r--r-- | modus-themes.el | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/modus-themes.el b/modus-themes.el index 8dceae1..bba9874 100644 --- a/modus-themes.el +++ b/modus-themes.el @@ -3741,17 +3741,30 @@ Info node `(modus-themes) Option for palette overrides'.") ;;;; Helper functions for theme setup -(defun modus-themes--hex-to-rgb (hex-color) - "Convert HEX-COLOR to a list of normalized RGB values. -Use `color-values-from-color-spec' (a C built-in since Emacs 28.1) -instead of `color-name-to-rgb' to avoid dependence on a display -connection. This matters when loading a theme during early init on -GUI Emacs, where `color-values' returns nil before the display is -ready (per <https://github.com/protesilaos/modus-themes/issues/198>)." - (mapcar - (lambda (x) - (/ x 65535.0)) - (color-values-from-color-spec hex-color))) +(defvar modus-themes--hex-regexp + (concat + "\\`#" + "\\(?:[[:xdigit:]]\\{3\\}" + "\\|" + "[[:xdigit:]]\\{6\\}\\)" + "\\'") + "Regular expression to match a color in hexadecimal RGB notation.") + +(defun modus-themes--color-hex-p (color) + "Return non-nil if COLOR is hexadecimal RGB." + (and (stringp color) (string-match-p modus-themes--hex-regexp color))) + +(defun modus-themes--hex-or-name-to-rgb (color) + "Convert COLOR to a list of normalized RGB values. +COLOR can be a hexadecimal RGB value like #123456 or a named color +like those produced by `list-colors-display'." + (cond + ((modus-themes--color-hex-p color) + (when-let* ((spec (color-values-from-color-spec color))) + (mapcar (lambda (x) (/ x 65535.0)) spec))) + ((color-name-to-rgb color)) + (t + (error "The color `%s' cannot be resolved" color)))) ;; This is the WCAG formula: https://www.w3.org/TR/WCAG20-TECHS/G18.html (defun modus-themes--wcag-contribution (channel weight) |
