1 ;;; rainbow-mode.el --- Colorize color names in buffers
3 ;; Copyright (C) 2010 Free Software Foundation, Inc
5 ;; Author: Julien Danjou <julien@danjou.info>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This minor mode sets background color to strings that match color
27 ;; names, e.g. #0000ff is displayed in white with a blue background.
39 "Show color strings with a background color."
44 (defvar rainbow-hexadecimal-colors-font-lock-keywords
45 '(("#[0-9a-fA-F]\\{3\\}[0-9a-fA-F]\\{3\\}?"
46 (0 (rainbow-colorize-itself))))
47 "Font-lock keywords to add for hexadecimal colors.")
50 (defvar rainbow-html-rgb-colors-font-lock-keywords
51 '(("rgb(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*)"
52 (0 (rainbow-colorize-rgb)))
53 ("rgba(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*[0-9]\\{1,3\\}\s*%?\s*)"
54 (0 (rainbow-colorize-rgb))))
55 "Font-lock keywords to add for RGB colors.")
58 (defvar rainbow-html-colors-font-lock-keywords nil
59 "Font-lock keywords to add for HTML colors.")
60 (make-variable-buffer-local 'rainbow-html-colors-font-lock-keywords)
62 (defcustom rainbow-html-colors-alist
63 '(("black" . "#000000")
64 ("silver" . "#C0C0C0")
67 ("maroon" . "#800000")
69 ("purple" . "#800080")
70 ("fuchsia" . "#FF00FF")
74 ("yellow" . "#FFFF00")
79 "Alist of HTML colors.
80 Each entry should have the form (COLOR-NAME . HEXADECIMAL-COLOR)."
83 (defcustom rainbow-html-colors-major-mode-list
84 '(html-mode css-mode php-mode nxml-mode xml-mode)
85 "List of major mode where HTML colors are enabled when
86 `rainbow-html-colors' is set to auto."
89 (defcustom rainbow-html-colors 'auto
90 "When to enable HTML colors.
91 If set to t, the HTML colors will be enabled. If set to nil, the
92 HTML colors will not be enabled. If set to auto, the HTML colors
93 will be enabled if a major mode has been detected from the
94 `rainbow-html-colors-major-mode-list'."
98 (defvar rainbow-x-colors-font-lock-keywords
99 `((,(regexp-opt (x-defined-colors) 'words)
100 (0 (rainbow-colorize-itself))))
101 "Font-lock keywords to add for X colors.")
103 (defcustom rainbow-x-colors-major-mode-list
104 '(emacs-lisp-mode lisp-interaction-mode c-mode c++-mode java-mode)
105 "List of major mode where X colors are enabled when
106 `rainbow-x-colors' is set to auto."
109 (defcustom rainbow-x-colors 'auto
110 "When to enable X colors.
111 If set to t, the X colors will be enabled. If set to nil, the
112 X colors will not be enabled. If set to auto, the X colors
113 will be enabled if a major mode has been detected from the
114 `rainbow-x-colors-major-mode-list'."
118 (defvar rainbow-latex-rgb-colors-font-lock-keywords
119 '(("{rgb}{\\([0-9.]+\\),\\([0-9.]+\\),\\([0-9.]+\\)}"
120 (0 (rainbow-colorize-rgb-float)))
121 ("{RGB}{\\([0-9]\\{1,3\\}\\),\\([0-9]\\{1,3\\}\\),\\([0-9]\\{1,3\\}\\)}"
122 (0 (rainbow-colorize-rgb)))
123 ("{HTML}{\\([0-9A-Fa-f]\\{6\\}\\)}"
124 (0 (rainbow-colorize-hexadecimal-without-sharp))))
125 "Font-lock keywords to add for X colors.")
127 (defcustom rainbow-latex-colors-major-mode-list
129 "List of major mode where X colors are enabled when
130 `rainbow-x-colors' is set to auto."
133 (defcustom rainbow-latex-colors 'auto
134 "When to enable LaTeX colors.
135 If set to t, the LaTeX colors will be enabled. If set to nil, the
136 X colors will not be enabled. If set to auto, the LaTeX colors
137 will be enabled if a major mode has been detected from the
138 `rainbow-latex-colors-major-mode-list'."
142 (defun rainbow-colorize-match (color)
143 "Return a matched string propertized with a face whose
144 background is COLOR. The foreground is computed using
145 `rainbow-color-luminance', and is either white or black."
147 (match-beginning 0) (match-end 0)
148 'face `((:foreground ,(if (> 128.0 (rainbow-x-color-luminance color))
150 (:background ,color))))
152 (defun rainbow-colorize-itself ()
153 "Colorize a match with itself."
154 (rainbow-colorize-match (match-string-no-properties 0)))
156 (defun rainbow-colorize-hexadecimal-without-sharp ()
157 "Colorize an hexadecimal colors and prepend # to it."
158 (rainbow-colorize-match (concat "#" (match-string-no-properties 1))))
160 (defun rainbow-colorize-by-assoc (assoc-list)
161 "Colorize a match with its association from ASSOC-LIST."
162 (rainbow-colorize-match (cdr (assoc (match-string-no-properties 0) assoc-list))))
164 (defun rainbow-rgb-relative-to-absolute (number)
165 "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
166 This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\"."
167 (let ((string-length (- (length number) 1)))
168 ;; Is this a number with %?
169 (if (eq (elt number string-length) ?%)
170 (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
171 (string-to-number number))))
173 (defun rainbow-colorize-rgb ()
174 "Colorize a match with itself."
175 (let ((r (rainbow-rgb-relative-to-absolute (match-string-no-properties 1)))
176 (g (rainbow-rgb-relative-to-absolute (match-string-no-properties 2)))
177 (b (rainbow-rgb-relative-to-absolute (match-string-no-properties 3))))
178 (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
180 (defun rainbow-colorize-rgb-float ()
181 "Colorize a match with itself, with relative value."
182 (let ((r (* (string-to-number (match-string-no-properties 1)) 255.0))
183 (g (* (string-to-number (match-string-no-properties 2)) 255.0))
184 (b (* (string-to-number (match-string-no-properties 3)) 255.0)))
185 (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
187 (defun rainbow-color-luminance (red green blue)
188 "Calculate the luminance of color composed of RED, BLUE and GREEN."
189 (floor (+ (* .2126 red) (* .7152 green) (* .0722 blue)) 256))
191 (defun rainbow-x-color-luminance (color)
192 "Calculate the luminance of a color string (e.g. \"#ffaa00\", \"blue\")."
193 (let* ((values (x-color-values color))
197 (rainbow-color-luminance r g b)))
199 (defun rainbow-turn-on ()
200 "Turn on raibow-mode."
201 (font-lock-add-keywords nil
202 rainbow-hexadecimal-colors-font-lock-keywords)
203 ;; Activate X colors?
204 (when (or (eq rainbow-x-colors t)
205 (and (eq rainbow-x-colors 'auto)
206 (memq major-mode rainbow-x-colors-major-mode-list)))
207 (font-lock-add-keywords nil
208 rainbow-x-colors-font-lock-keywords))
209 ;; Activate LaTeX colors?
210 (when (or (eq rainbow-latex-colors t)
211 (and (eq rainbow-latex-colors 'auto)
212 (memq major-mode rainbow-latex-colors-major-mode-list)))
213 (font-lock-add-keywords nil
214 rainbow-latex-rgb-colors-font-lock-keywords))
215 ;; Activate HTML colors?
216 (when (or (eq rainbow-html-colors t)
217 (and (eq rainbow-html-colors 'auto)
218 (memq major-mode rainbow-html-colors-major-mode-list)))
219 (setq rainbow-html-colors-font-lock-keywords
220 `((,(regexp-opt (mapcar 'car rainbow-html-colors-alist) 'words)
221 (0 (rainbow-colorize-by-assoc rainbow-html-colors-alist)))))
222 (font-lock-add-keywords nil
223 `(,@rainbow-html-colors-font-lock-keywords
224 ,@rainbow-html-rgb-colors-font-lock-keywords))))
226 (defun rainbow-turn-off ()
227 "Turn off rainbow-mode."
228 (font-lock-remove-keywords
230 `(,@rainbow-hexadecimal-colors-font-lock-keywords
231 ,@rainbow-x-colors-font-lock-keywords
232 ,@rainbow-latex-rgb-colors-font-lock-keywords
233 ,@rainbow-html-colors-font-lock-keywords
234 ,@rainbow-html-rgb-colors-font-lock-keywords)))
237 (define-minor-mode rainbow-mode
238 "Colorize strings that represent colors.
239 This will fontify with colors the string like \"#aabbcc\" or \"blue\"."
248 (provide 'rainbow-mode)
250 ;;; rainbow-mode.el ends here