-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrainbow-vector-notation.el
More file actions
65 lines (52 loc) · 1.99 KB
/
rainbow-vector-notation.el
File metadata and controls
65 lines (52 loc) · 1.99 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
;;; rainbow-vector-notation.el --- Colorize RGB vector notation in buffers -*- lexical-binding: nil -*-
;; Keywords: faces
;; Version: 1.0
;;; Commentary:
;;
;; This minor mode extends rainbow-mode to colorize RGB vector notation,
;; e.g. [55 86 163] is displayed with a blue background.
;;
;; Requires rainbow-mode to be loaded first.
;;; Code:
(require 'rainbow-mode)
;;; RGB Vector colors
(defvar rainbow-vector-colors-font-lock-keywords
'(("\\[\\s-*\\([0-9]\\{1,3\\}\\)\\s-*\\([0-9]\\{1,3\\}\\)\\s-*\\([0-9]\\{1,3\\}\\)\\s-*\\]"
(0 (rainbow-colorize-vector))))
"Font-lock keywords to add for RGB vector colors.")
(defun rainbow-colorize-vector ()
"Colorize an RGB vector match with itself."
(let ((r (string-to-number (match-string-no-properties 1)))
(g (string-to-number (match-string-no-properties 2)))
(b (string-to-number (match-string-no-properties 3))))
;; Validate that all values are between 0 and 255
(when (and (>= r 0) (<= r 255)
(>= g 0) (<= g 255)
(>= b 0) (<= b 255))
(rainbow-colorize-match (format "#%02X%02X%02X" r g b)))))
;;; Mode
(defun rainbow-vector-notation-turn-on ()
"Turn on rainbow-vector-notation-mode."
(font-lock-add-keywords nil
rainbow-vector-colors-font-lock-keywords
t))
(defun rainbow-vector-notation-turn-off ()
"Turn off rainbow-vector-notation-mode."
(font-lock-remove-keywords
nil
rainbow-vector-colors-font-lock-keywords))
;;;###autoload
(define-minor-mode rainbow-vector-notation-mode
"Colorize RGB vector notation like [55 86 163].
This extends rainbow-mode to recognize and colorize RGB vector notation."
:lighter " Rvec"
(if rainbow-vector-notation-mode
(rainbow-vector-notation-turn-on)
(rainbow-vector-notation-turn-off))
;; Call font-lock-mode to refresh the buffer
(font-lock-mode 1))
(provide 'rainbow-vector-notation)
;;; Local Variables:
;;; indent-tabs-mode: nil
;;; End:
;;; rainbow-vector-notation.el ends here