From b9e54ebfaa533a4f2f110942a188a07704a90d95 Mon Sep 17 00:00:00 2001 From: Przemek Kryger Date: Tue, 15 Mar 2022 17:57:27 +0000 Subject: [PATCH] Add iTerm2 template to modus-themes-exporter This adds a template for iTerm2 color theme to modus-themes-exporter. I'm by no means an expert in either themes or iTerm2, but managed to come up with something that can export Modus theme colors in a format accepted by iTerm2. --- .../prot-lisp/modus-themes-exporter.el | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/emacs/.emacs.d/prot-lisp/modus-themes-exporter.el b/emacs/.emacs.d/prot-lisp/modus-themes-exporter.el index f2580d3e6..08697eda9 100644 --- a/emacs/.emacs.d/prot-lisp/modus-themes-exporter.el +++ b/emacs/.emacs.d/prot-lisp/modus-themes-exporter.el @@ -48,9 +48,12 @@ (require 'modus-themes) +(require 'dom) +(require 'seq) (defvar modus-themes-exporter-templates-alist '(("alacritty" . modus-themes-exporter-alacritty) + ("iterm2" . modus-themes-exporter-iterm2) ("urxvt" . modus-themes-exporter-urxvt) ("vim" . modus-themes-exporter-vim) ("windows-terminal" . modus-themes-exporter-windows-terminal) @@ -556,5 +559,103 @@ file." (kill-new fn) (message "Saved to kill-ring port of %s for %s" current-theme template))))) + +(defun modus-themes-exporter--iterm2-color-component (color component) + "Generate a string value for the COMPONENT of the specified COLOR. + +The COLOR is a string in a form #RRGGBB. The component is one of: 'red, 'green, or 'blue." + (number-to-string + (/ (string-to-number (pcase component + ('red (substring color 1 3)) + ('green (substring color 3 5)) + ('blue (substring color 5 7))) + 16) + 255.0))) + +(defun modus-themes-exporter--iterm2-color-name (name) + "Generate a DOM key entry for the specified NAME." + `(key nil ,name)) + +(defun modus-themes-exporter--iterm2-color-dict (color &optional alpha) + "Generate a DOM dict entry entry for the specified COLOR and optional ALPHA. +The COLOR is a string in a form of #RRGGBB. The ALPHA is a number." + `(dict nil + (key nil "Alpha Component") + (real nil ,(number-to-string (or alpha 1))) + (key nil "Blue Component") + (real nil ,(modus-themes-exporter--iterm2-color-component color 'blue)) + (key nil "Color Space") + (string nil "sRGB") + (key nil "Green Component") + (real nil ,(modus-themes-exporter--iterm2-color-component color 'green)) + (key nil "Red Component") + (real nil ,(modus-themes-exporter--iterm2-color-component color 'red)))) + +(defun modus-themes-exporter--iterm2-dict (colors-defs) + "Generate a string representation of COLORS-DEFS alist for iTerm2 colors. + +Each item in COLORS-DEFS is in a form of (NAME . VALUE), where NAME is one of +the iTerm 2 color names and VALUE either is a color string in a form of #RRGGBB, +or is a list of color string in a from of #RRGGBB and an alpha value." + (with-temp-buffer + (dom-print + `(dict nil + ,@(apply 'cl-concatenate + 'list + (mapcar (lambda (color-name) + (list (modus-themes-exporter--iterm2-color-name (car color-name)) + (apply 'modus-themes-exporter--iterm2-color-dict (pcase (cdr color-name) + ((and (pred listp) args) args) + (arg (list arg)))))) + colors-defs))) + t t) + (buffer-string))) + + + +(defun modus-themes-exporter-iterm2 () + "Template for iTerm2." + (modus-themes-with-colors + (let ((theme-name (format "%s" (car custom-enabled-themes)))) + (with-temp-buffer + (concat + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + (modus-themes-exporter--iterm2-dict + `(("Ansi 0 Color" . "#000000") + ("Ansi 1 Color" . ,red) + ("Ansi 2 Color" . ,green) + ("Ansi 3 Color" . ,yellow) + ("Ansi 4 Color" . ,blue) + ("Ansi 5 Color" . ,magenta) + ("Ansi 6 Color" . ,cyan) + ("Ansi 7 Color" . "#bfbfbf") + ("Ansi 8 Color" . "#595959") + ("Ansi 9 Color" . ,red-alt) + ("Ansi 10 Color" . ,green-alt) + ("Ansi 11 Color" . ,yellow-alt) + ("Ansi 12 Color" . ,blue-alt) + ("Ansi 13 Color" . ,magenta-alt-other) + ("Ansi 14 Color" . ,cyan-alt-other) + ("Ansi 15 Color" . "#ffffff") + ("Background Color" . ,bg-main) + ("Badge Color" . (,red-alt 0.75)) + ("Bold Color" . ,fg-main) + ("Cursor Color" . ,fg-main) + ("Cursor Guide Color" . (,bg-hl-line 0.8)) + ("Cursor Text Color" . ,bg-main) + ("Foreground Color" . ,fg-main) + ("Link Color" . ,blue-alt) + ("Selected Text Color" . ,fg-main) + ("Selection Color" . ,bg-region) + )) + "\n" + "" "\n"))))) + + (provide 'modus-themes-exporter) ;;; modus-themes-exporter.el ends here -- GitLab