|
59 | 59 | (cl-defstruct |
60 | 60 | autothemer--theme |
61 | 61 | colors |
| 62 | + reduced-specs |
62 | 63 | defined-faces |
63 | 64 | name |
64 | 65 | description) |
@@ -218,6 +219,7 @@ bindings within both the REDUCED-SPECS and the BODY." |
218 | 219 | :name ,(symbol-name name) |
219 | 220 | :description ,description |
220 | 221 | :colors ,temp-color-structs |
| 222 | + :reduced-specs ',reduced-specs |
221 | 223 | :defined-faces ',face-names)))) |
222 | 224 | (setq ,face-specs |
223 | 225 | (autothemer--append-column |
@@ -876,6 +878,94 @@ to be `autothemer--color' structs. |
876 | 878 | "Flatten a GROUPED-PALETTE from `autothemer-group-and-sort' to a single list." |
877 | 879 | (-flatten (--map (cdr it) grouped-palette))) |
878 | 880 |
|
| 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 | + |
879 | 969 | ;;; SVG Palette generator... |
880 | 970 |
|
881 | 971 | (defun autothemer-generate-palette-svg (&optional options) |
|
0 commit comments