Skip to content

Commit e62bf83

Browse files
committed
Add theme and palette export to JSON format for external tool integration
1 parent 0d3ed8d commit e62bf83

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,6 @@ Make sure you eval all the theme's elisp files before enabling the theme.)
572572

573573
### Themes using Autothemer
574574

575-
- [Patrick-Poitras/emacs-material-ocean](https://github.com/Patrick-Poitras/emacs-material-ocean)
576575
- [ajgrf/Parchment](https://github.com/ajgrf/parchment/)
577576
- [ed9w2in6/wood-theme](https://github.com/ed9w2in6/wood-theme)
578577
- [emacsfodder/Creamsody](https://github.com/emacsfodder/emacs-theme-creamsody)
@@ -585,6 +584,7 @@ Make sure you eval all the theme's elisp files before enabling the theme.)
585584
- [greduan/Gruvbox](https://github.com/greduan/emacs-theme-gruvbox)
586585
- [mtreca/Sorcery](https://github.com/mtreca/emacs-theme-sorcery)
587586
- [ogdenwebb/Kaolin](https://github.com/ogdenwebb/emacs-kaolin-themes)
587+
- [Patrick-Poitras/emacs-material-ocean](https://github.com/Patrick-Poitras/emacs-material-ocean)
588588
- [thongpv87/Rose Pine](https://github.com/thongpv87/rose-pine-emacs)
589589

590590
If you are creating themes with Autothemer, please let us know, you can add the

autothemer.el

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
(cl-defstruct
6060
autothemer--theme
6161
colors
62+
reduced-specs
6263
defined-faces
6364
name
6465
description)
@@ -218,6 +219,7 @@ bindings within both the REDUCED-SPECS and the BODY."
218219
:name ,(symbol-name name)
219220
:description ,description
220221
:colors ,temp-color-structs
222+
:reduced-specs ',reduced-specs
221223
:defined-faces ',face-names))))
222224
(setq ,face-specs
223225
(autothemer--append-column
@@ -876,6 +878,94 @@ to be `autothemer--color' structs.
876878
"Flatten a GROUPED-PALETTE from `autothemer-group-and-sort' to a single list."
877879
(-flatten (--map (cdr it) grouped-palette)))
878880

881+
882+
;;; Theme Tools generator...
883+
884+
(defun autothemer--generate-theme-json ()
885+
"Generate JSON for the autothemer theme with palette and faces."
886+
(let* ((theme autothemer-current-theme) ; Access the current theme (it's a variable, not a function)
887+
(theme-name (autothemer--theme-name theme)) ; Extract theme name
888+
(theme-description (autothemer--theme-description theme)) ; Extract description
889+
(reduced-specs (autothemer--theme-reduced-specs theme)) ; Reduced specs
890+
(faces-json (autothemer--generate-faces-json reduced-specs)) ; Process faces into JSON
891+
(palette-json (autothemer--generate-palette-json)) ; Use the existing palette-to-JSON
892+
(theme-json `((("name" . ,theme-name)
893+
("description" . ,theme-description)
894+
("palette" . ,palette-json)
895+
("faces" . ,faces-json)))))
896+
(json-encode theme-json)))
897+
898+
(defun autothemer--generate-faces-json (reduced-specs)
899+
"Generate the faces portion of the JSON from the reduced specs."
900+
(--map
901+
(let* ((face-name (symbol-name (car it))) ; Extract the face name (as string)
902+
(attrs (cdr it)) ; Get the face attributes
903+
(json-attrs (autothemer--attrs-to-json attrs))) ; Convert attributes to JSON format
904+
`(("name" . ,face-name)
905+
("spec" . ,json-attrs)))
906+
reduced-specs))
907+
908+
(defun autothemer--attrs-to-json (attrs)
909+
"Convert face attributes to JSON format, including handling `:inherit`."
910+
(let (json-attrs)
911+
(dolist (attr attrs json-attrs)
912+
(let* ((key (car attr)) ; The attribute key (e.g., :foreground)
913+
(val (cdr attr)) ; The attribute value (color or other)
914+
(json-key (substring (symbol-name key) 1)) ; Convert symbol to string
915+
(json-val (cond
916+
((eq val t) t) ; Convert `t` to `true`
917+
((eq val nil) 'undefined) ; Convert `nil` to `undefined`
918+
((symbolp val) (symbol-name val)) ; Convert symbol to string
919+
(t val)))) ; Default to the value as-is
920+
(push (cons json-key json-val) json-attrs)))))
921+
922+
(defun autothemer--generate-palette-json ()
923+
"Return the current Autothemer palette as JSON in the correct format."
924+
(autothemer--current-theme-guard)
925+
(let* ((colors (autothemer--theme-colors autothemer-current-theme)) ; Get the theme's colors
926+
(json-array
927+
(--map
928+
(let ((color-name (symbol-name (autothemer--color-name it))) ; Extract the color name
929+
(color-value (autothemer--color-value it))) ; Extract the color value (e.g., #ff0000)
930+
`(("name" . ,color-name) ; Structure as a JSON object
931+
("color" . ,color-value)))
932+
colors)))
933+
json-array))
934+
935+
(defun autothemer-generate-palette-json (&optional options)
936+
"Return the current Autothemer palette as JSON in the form:
937+
[
938+
{\"name\": NAME, \"color\": COLOR},
939+
...
940+
]
941+
942+
OPTIONS is currently unused, reserved for future extension."
943+
(autothemer--current-theme-guard)
944+
(let* ((colors (autothemer--theme-colors autothemer-current-theme))
945+
(json-array
946+
(--map
947+
`(("name" . ,(symbol-name (autothemer--color-name it)))
948+
("color" . ,(autothemer--color-value it)))
949+
colors)))
950+
(json-encode json-array)))
951+
952+
(defun autothemer-write-theme-json (file)
953+
"Write the current Autothemer theme JSON to the given FILE."
954+
(interactive "FWrite palette JSON to file: ")
955+
(let ((json (autothemer--generate-theme-json)))
956+
(with-temp-file file
957+
(insert json))
958+
(message "Theme JSON written to %s" file)))
959+
960+
(defun autothemer-write-palette-json (file)
961+
"Write the current Autothemer palette JSON to the given FILE."
962+
(interactive "FWrite palette JSON to file: ")
963+
(let ((json (autothemer-generate-palette-json)))
964+
(with-temp-file file
965+
(insert json))
966+
(message "Palette JSON written to %s" file)))
967+
968+
879969
;;; SVG Palette generator...
880970

881971
(defun autothemer-generate-palette-svg (&optional options)

0 commit comments

Comments
 (0)