org-latex-preview: Cache formatted color strings

* lisp/org-latex-preview.el (org-latex-preview--format-color,
org-latex-preview--format-color-cache): Introduce a color cache alist, and use
it.
This commit is contained in:
TEC 2023-01-03 19:25:23 +08:00
parent 7a4925cac6
commit a27cc761ff
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 12 additions and 3 deletions

View File

@ -1606,11 +1606,20 @@ a HTML file."
"Return a RGB color for the LaTeX color package."
(org-latex-preview--format-color (face-attribute 'default attr nil)))
(defvar org-latex-preview--format-color-cache nil
"Cache for `org-latex-preview--format-color'.
Because `org-latex-preview--format-color' is called multiple
times for every fragment, even though only few colors will be
used it can be worth storing the results to avoid re-computing.")
(defun org-latex-preview--format-color (color-name)
"Convert COLOR-NAME to a RGB color value."
(apply #'format "%s,%s,%s"
(mapcar 'org-latex-preview--normalize-color
(color-values color-name))))
(or (alist-get color-name org-latex-preview--format-color-cache nil nil #'equal)
(cdar (push (cons color-name
(apply #'format "%s,%s,%s"
(mapcar 'org-latex-preview--normalize-color
(color-values color-name))))
org-latex-preview--format-color-cache))))
(defun org-latex-preview--normalize-color (value)
"Return string to be used as color value for an RGB component."