-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcolorful-colors.el
More file actions
363 lines (311 loc) · 13.8 KB
/
colorful-colors.el
File metadata and controls
363 lines (311 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
;;; colorful-colors.el --- Functions to enable specific colors highlighting to colorful-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Free Software Foundation, Inc
;; Author: Elias G. Perez <eg642616@gmail.com>
;; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
;; Elias G. Perez <eg642616@gmail.com>
;; Created: 2026-03-24
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file contains functions intended to be used in
;; `colorful-extra-color-keyword-functions'
;;
;; These functions must return a plist which contains:
;;
;; :keywords must be a regexp string which contains the keywords
;; to highlight
;;
;; :type (optional) is a symbol which specifies the color type.
;;
;; :match (optional) must be a number which specifies the match to
;; use, if not set, it will use 0 instead.
;;
;; :case (optional), if non-nil, then match will be case-insensitive
;;
;; :function must be a function.
;; It is called with three arguments COLOR, BEG, and END.
;;
;; COLOR is the string which contains the color matched.
;; BEG and END are color matched positions.
;;
;; The function must return either a list with the computed string hex
;; color to colorize, the beg and end buffer positions to colorize
;; with the computed color, or just the string hex color.
;;; Code:
(require 'cl-lib)
;;; Declarations
(defvar colorful-color-keywords)
(defvar colorful-html-colors-alist)
(declare-function colorful--percentage-to-absolute "colorful-mode")
(declare-function colorful--oklab-to-hex "colorful-mode")
(declare-function colorful--oklch-to-hex "colorful-mode")
(declare-function colorful--hsl-to-hex "colorful-mode")
(declare-function color-hsl-to-rgb "colorful-mode")
(declare-function colorful--find-overlay "colorful-mode")
;;; Hex
(defun colorful--hex-fn (color &rest _)
(list (string-replace "0x" "#" color)
(match-beginning 0)
(match-end 0)))
(defun colorful-add-hex-colors ()
"Enable hex color highlighting.
This is intended to be used with `colorful-extra-color-keyword-functions'."
(cl-pushnew
`( :keywords ,(rx (seq (group (or "#" "0x") (= 3 hex)) (opt hex) word-boundary))
:type hex
:match 1
:function colorful--hex-fn)
colorful-color-keywords)
(cl-pushnew
`( :keywords ,(rx (seq (group (or "#" "0x") (= 3 hex hex)) (opt hex hex) word-boundary))
:type hex
:match 1
:function colorful--hex-fn)
colorful-color-keywords)
(cl-pushnew
`( :keywords ,(rx (seq (group "#" (= 12 hex)) word-boundary))
:type hex
:match 1
:function colorful--hex-fn)
colorful-color-keywords))
;;; Color names
(defun colorful--color-names-fn (color &rest _)
(if (color-defined-p color)
color
(cdr (assoc-string color colorful-html-colors-alist t))))
(defun colorful-add-color-names ()
"Enable color name highlighting.
This includes CSS and Emacs color names.
This is intended to be used with `colorful-extra-color-keyword-functions'."
(cl-pushnew
`( :keywords ,(regexp-opt
(append
(mapcar #'car colorful-html-colors-alist)
(defined-colors))
'symbols)
:type color-name
:match 0
:case t ; HTML/CSS/Emacs color names are case insensitive.
:function colorful--color-names-fn)
colorful-color-keywords))
;;; CSS user-defined colors
;; TODO: Use _local for css vars in local scopes, use (car (syntax-ppss)) ?
(defun colorful--get-css-variable-color (regexp pos &optional _local)
"Return the CSS variable color value matches REGEXP.
Subgroup 1 of REGEXP should match the color value.
POS is the position where start the search."
(save-excursion
(goto-char pos)
(when (re-search-backward regexp nil t)
;; Get color value from colorful overlay.
;; if not color value found, use the one from 1st subgroup of REGEXP.
(or (and (colorful--find-overlay (match-beginning 1)) ; Ensure overlay exists.
(overlay-get (colorful--find-overlay
(match-beginning 1))
'colorful--color))
(match-string-no-properties 1)))))
(defun colorful--css-var-fn (_color beg &rest _)
(let ((match-1 (match-string-no-properties 1))
(match-2 (match-string-no-properties 2)))
(cond
((and (string= match-1 "@")
(or (not (member match-2 '("define_color" "define-color")))))
(colorful--get-css-variable-color
(rx-to-string
`(seq (or "@define_color"
"@define-color")
(one-or-more space)
,match-2
(one-or-more space)
(group (opt "#") (one-or-more alphanumeric))))
beg))
((string= match-1 "var")
(colorful--get-css-variable-color
(rx-to-string
`(seq ,match-2 ":" (zero-or-more space)
(group (opt "#") (one-or-more alphanumeric))))
beg)))))
(defun colorful-add-css-variables-colors ()
"Enable CSS user-defined color highlighting.
This is intended to be used with `colorful-extra-color-keyword-functions'."
(cl-pushnew
`( :keywords ,(rx (group "@") (group (one-or-more (any alphabetic "_"))))
:type css-color-variable
:function colorful--css-var-fn)
colorful-color-keywords)
(cl-pushnew
`( :keywords ,(rx (group "var") "(" (zero-or-more space)
(group (one-or-more (any alphanumeric "-")))
(zero-or-more space) ")")
:type css-color-variable
:function colorful--css-var-fn)
colorful-color-keywords))
;;; CSS rgb(a)
(defun colorful--css-rgb-fn (&rest _)
(let ((match-1 (match-string-no-properties 1)) ; r
(match-2 (match-string-no-properties 2)) ; g
(match-3 (match-string-no-properties 3))) ; b
(format "#%02x%02x%02x"
(colorful--percentage-to-absolute match-1)
(colorful--percentage-to-absolute match-2)
(colorful--percentage-to-absolute match-3))))
(defun colorful-add-rgb-colors ()
"Enable CSS RGB color highlighting.
This is intended to be used with `colorful-extra-color-keyword-functions'."
(cl-pushnew
`( :keywords ,(rx (seq "rgb" (opt "a") "(" (zero-or-more " ")
(group (repeat 1 3 digit)
(opt "." (1+ digit))
(opt "%"))
(zero-or-more " ") (opt "," (zero-or-more " "))
(group (repeat 1 3 digit)
(opt "." (1+ digit))
(opt "%"))
(zero-or-more " ") (opt "," (zero-or-more " "))
(group (repeat 1 3 digit)
(opt "." (1+ digit))
(opt "%"))
(zero-or-more " ")
(opt (or "/" ",") (zero-or-more " ")
(or (seq (zero-or-one digit)
(opt ".")
(one-or-more digit))
digit)
(opt (or "%" (zero-or-more " "))))
")"))
:type css-rgb
:function colorful--css-rgb-fn)
colorful-color-keywords))
;;; CSS oklab and oklch
(defun colorful--oklab-fn (&rest _)
(let ((match-1 (match-string-no-properties 1)) ; l
(match-2 (match-string-no-properties 2)) ; a
(match-3 (match-string-no-properties 3))) ; b
(colorful--oklab-to-hex match-1 match-2 match-3)))
(defun colorful--oklch-fn (&rest _)
(let ((match-1 (match-string-no-properties 1)) ; l
(match-2 (match-string-no-properties 2)) ; a
(match-3 (match-string-no-properties 3))) ; b
(colorful--oklch-to-hex match-1 match-2 match-3)))
(defun colorful-add-oklab-oklch-colors ()
"Add CSS OKLAB and OKLCH color highlighting.
This is intended to be used with `colorful-extra-color-keyword-functions'."
;; OKLAB
(cl-pushnew
`( :keywords ,(rx (seq "oklab(" (zero-or-more " ")
(group (repeat 1 3 digit)
(opt "." (1+ digit))
(opt "%"))
(zero-or-more " ") (opt "," (zero-or-more " "))
(group (opt "-")
digit
(opt "." (1+ digit)))
(zero-or-more " ") (opt "," (zero-or-more " "))
(group (opt "-")
digit
(opt "." (1+ digit)))
(zero-or-more " ")
(opt (or "/" ",") (zero-or-more " ")
(group (or (seq (zero-or-one digit)
(opt ".")
(one-or-more digit))
digit)
(opt (or "%" (zero-or-more " ")))))
")"))
:type css-oklab
:function colorful--oklab-fn)
colorful-color-keywords)
;; OKLCH
(cl-pushnew
`( :keywords ,(rx (seq "oklch(" (zero-or-more " ")
(group (repeat 1 3 digit)
(opt "." (1+ digit))
(opt "%"))
(zero-or-more " ") (opt "," (zero-or-more " "))
(group digit
(opt "." (1+ digit)))
(zero-or-more " ") (opt "," (zero-or-more " "))
(group (repeat 1 3 digit)
(opt "." (1+ digit)))
(zero-or-more " ")
(opt (or "/" ",") (zero-or-more " ")
(group (or (seq (zero-or-one digit)
(opt ".")
(one-or-more digit))
digit)
(opt (or "%" (zero-or-more " ")))))
")"))
:type css-oklch
:function colorful--oklch-fn)
colorful-color-keywords))
;;; CSS hsl(a)
(defun colorful--css-hsl-fn (&rest _)
(let ((match-1 (match-string-no-properties 1)) ; h
(match-2 (match-string-no-properties 2)) ; s
(match-3 (match-string-no-properties 3))) ; l
(when (<= (string-to-number match-1) 360)
(colorful--hsl-to-hex match-1 match-2 match-3))))
(defun colorful-add-hsl-colors ()
"Enable CSS HSL color highlighting.
This is intended to be used with `colorful-extra-color-keyword-functions'."
(cl-pushnew
`( :keywords ,(rx (seq "hsl" (opt "a") "(" (zero-or-more " ")
(group (repeat 1 3 digit) (opt (or "deg" "grad" "rad")))
(zero-or-more " ") (opt "," (zero-or-more " "))
(group (repeat 1 3 digit) (opt "." (1+ digit)) (opt "%"))
(zero-or-more " ") (opt "," (zero-or-more " "))
(group (repeat 1 3 digit) (opt "." (1+ digit)) (opt "%"))
(zero-or-more " ")
(opt (or "/" ",") (zero-or-more " ")
(or (seq (zero-or-one digit)
(opt ".")
(one-or-more digit))
digit)
(opt (or "%" (zero-or-more " "))))
")"))
:type css-hsl
:function colorful--css-hsl-fn)
colorful-color-keywords))
;;; All (almost) LaTeX colors
(defun colorful--latex-rgb-fn (color &rest _)
(let ((match-1 (string-to-number (match-string-no-properties 1))) ; r
(match-2 (string-to-number (match-string-no-properties 2))) ; g
(match-3 (string-to-number (match-string-no-properties 3)))) ; b
(if (string-prefix-p "{R" color) ; Check if it's RGB (shorted as "{R")
(format "#%02x%02x%02x" match-1 match-2 match-3)
(color-rgb-to-hex match-1 match-2 match-3))))
(defun colorful-latex-gray (&rest _)
(let ((match-1 (match-string-no-properties 1)))
(apply #'color-rgb-to-hex
(color-hsl-to-rgb 0 0 (string-to-number match-1)))))
(defun colorful-add-latex-colors ()
"Enable LaTeX rgb/RGB/HTML/Grey colors highlighting.
This is intended to be used with `colorful-extra-color-keyword-functions'."
(cl-pushnew
`( :keywords ,(rx (seq "{" (or "rgb" "RGB") "}{" (zero-or-more " ")
(group (one-or-more (any digit "."))) (zero-or-more " ") "," (zero-or-more " ")
(group (one-or-more (any digit "."))) (zero-or-more " ") "," (zero-or-more " ")
(group (one-or-more (any digit "."))) (zero-or-more " ") "}"))
:type latex-rgb
:function colorful--latex-rgb-fn)
colorful-color-keywords)
(cl-pushnew
`( :keywords ,(rx (seq "{HTML}{" (group (= 6 hex)) "}"))
:type latex-HTML
:function ,(lambda (&rest _) (concat "#" (match-string-no-properties 1))))
colorful-color-keywords)
(cl-pushnew
`( :keywords ,(rx (seq "{gray}{" (group (one-or-more (any digit "."))) "}"))
:type latex-gray
:function colorful-latex-gray)
colorful-color-keywords))
(provide 'colorful-colors)
;;; colorful-colors.el ends here