]> code.delx.au - gnu-emacs-elpa/blob - packages/rainbow-mode/rainbow-mode.el
Update rainbow-mode to version 0.2
[gnu-emacs-elpa] / packages / rainbow-mode / rainbow-mode.el
1 ;;; rainbow-mode.el --- Colorize color names in buffers
2
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: faces
7 ;; Version: 0.2
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25 ;;
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.
28 ;;
29
30 ;;; Code:
31
32 (eval-when-compile
33 (require 'cl))
34
35 (require 'regexp-opt)
36 (require 'faces)
37
38 (defgroup rainbow nil
39 "Show color strings with a background color."
40 :tag "Rainbow"
41 :group 'help)
42
43 ;; Hexadecimal colors
44 (defvar rainbow-hexadecimal-colors-font-lock-keywords
45 '(("#\\(?:[0-9a-fA-F]\\{3\\}\\)+\\{1,4\\}"
46 (0 (rainbow-colorize-itself)))
47 ("[Rr][Gg][Bb]:[0-9a-fA-F]\\{1,4\\}/[0-9a-fA-F]\\{1,4\\}/[0-9a-fA-F]\\{1,4\\}"
48 (0 (rainbow-colorize-itself)))
49 ("[Rr][Gg][Bb][Ii]:[0-9.]+/[0-9.]+/[0-9.]+"
50 (0 (rainbow-colorize-itself)))
51 ("\\(?:[Cc][Ii][Ee]\\(?:[Xx][Yy][Zz]\\|[Uu][Vv][Yy]\\|[Xx][Yy][Yy]\\|[Ll][Aa][Bb]\\|[Ll][Uu][Vv]\\)\\|[Tt][Ee][Kk][Hh][Vv][Cc]\\):[+-]?[0-9.]+\\(?:[Ee][+-]?[0-9]+\\)?/[+-]?[0-9.]+\\(?:[Ee][+-]?[0-9]+\\)?/[+-]?[0-9.]+\\(?:[Ee][+-]?[0-9]+\\)?"
52 (0 (rainbow-colorize-itself))))
53 "Font-lock keywords to add for hexadecimal colors.")
54
55 ;; rgb() colors
56 (defvar rainbow-html-rgb-colors-font-lock-keywords
57 '(("rgb(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*)"
58 (0 (rainbow-colorize-rgb)))
59 ("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]*\.?[0-9]+\s*%?\s*)"
60 (0 (rainbow-colorize-rgb)))
61 ("hsl(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*)"
62 (0 (rainbow-colorize-hsl)))
63 ("hsla(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*[0-9]*\.?[0-9]+\s*%?\s*)"
64 (0 (rainbow-colorize-hsl))))
65 "Font-lock keywords to add for RGB colors.")
66
67 ;; HTML colors name
68 (defvar rainbow-html-colors-font-lock-keywords nil
69 "Font-lock keywords to add for HTML colors.")
70 (make-variable-buffer-local 'rainbow-html-colors-font-lock-keywords)
71
72 (defcustom rainbow-html-colors-alist
73 '(("AliceBlue" . "#F0F8FF")
74 ("AntiqueWhite" . "#FAEBD7")
75 ("Aqua" . "#00FFFF")
76 ("Aquamarine" . "#7FFFD4")
77 ("Azure" . "#F0FFFF")
78 ("Beige" . "#F5F5DC")
79 ("Bisque" . "#FFE4C4")
80 ("Black" . "#000000")
81 ("BlanchedAlmond" . "#FFEBCD")
82 ("Blue" . "#0000FF")
83 ("BlueViolet" . "#8A2BE2")
84 ("Brown" . "#A52A2A")
85 ("BurlyWood" . "#DEB887")
86 ("CadetBlue" . "#5F9EA0")
87 ("Chartreuse" . "#7FFF00")
88 ("Chocolate" . "#D2691E")
89 ("Coral" . "#FF7F50")
90 ("CornflowerBlue" . "#6495ED")
91 ("Cornsilk" . "#FFF8DC")
92 ("Crimson" . "#DC143C")
93 ("Cyan" . "#00FFFF")
94 ("DarkBlue" . "#00008B")
95 ("DarkCyan" . "#008B8B")
96 ("DarkGoldenRod" . "#B8860B")
97 ("DarkGray" . "#A9A9A9")
98 ("DarkGrey" . "#A9A9A9")
99 ("DarkGreen" . "#006400")
100 ("DarkKhaki" . "#BDB76B")
101 ("DarkMagenta" . "#8B008B")
102 ("DarkOliveGreen" . "#556B2F")
103 ("Darkorange" . "#FF8C00")
104 ("DarkOrchid" . "#9932CC")
105 ("DarkRed" . "#8B0000")
106 ("DarkSalmon" . "#E9967A")
107 ("DarkSeaGreen" . "#8FBC8F")
108 ("DarkSlateBlue" . "#483D8B")
109 ("DarkSlateGray" . "#2F4F4F")
110 ("DarkSlateGrey" . "#2F4F4F")
111 ("DarkTurquoise" . "#00CED1")
112 ("DarkViolet" . "#9400D3")
113 ("DeepPink" . "#FF1493")
114 ("DeepSkyBlue" . "#00BFFF")
115 ("DimGray" . "#696969")
116 ("DimGrey" . "#696969")
117 ("DodgerBlue" . "#1E90FF")
118 ("FireBrick" . "#B22222")
119 ("FloralWhite" . "#FFFAF0")
120 ("ForestGreen" . "#228B22")
121 ("Fuchsia" . "#FF00FF")
122 ("Gainsboro" . "#DCDCDC")
123 ("GhostWhite" . "#F8F8FF")
124 ("Gold" . "#FFD700")
125 ("GoldenRod" . "#DAA520")
126 ("Gray" . "#808080")
127 ("Grey" . "#808080")
128 ("Green" . "#008000")
129 ("GreenYellow" . "#ADFF2F")
130 ("HoneyDew" . "#F0FFF0")
131 ("HotPink" . "#FF69B4")
132 ("IndianRed" . "#CD5C5C")
133 ("Indigo" . "#4B0082")
134 ("Ivory" . "#FFFFF0")
135 ("Khaki" . "#F0E68C")
136 ("Lavender" . "#E6E6FA")
137 ("LavenderBlush" . "#FFF0F5")
138 ("LawnGreen" . "#7CFC00")
139 ("LemonChiffon" . "#FFFACD")
140 ("LightBlue" . "#ADD8E6")
141 ("LightCoral" . "#F08080")
142 ("LightCyan" . "#E0FFFF")
143 ("LightGoldenRodYellow" . "#FAFAD2")
144 ("LightGray" . "#D3D3D3")
145 ("LightGrey" . "#D3D3D3")
146 ("LightGreen" . "#90EE90")
147 ("LightPink" . "#FFB6C1")
148 ("LightSalmon" . "#FFA07A")
149 ("LightSeaGreen" . "#20B2AA")
150 ("LightSkyBlue" . "#87CEFA")
151 ("LightSlateGray" . "#778899")
152 ("LightSlateGrey" . "#778899")
153 ("LightSteelBlue" . "#B0C4DE")
154 ("LightYellow" . "#FFFFE0")
155 ("Lime" . "#00FF00")
156 ("LimeGreen" . "#32CD32")
157 ("Linen" . "#FAF0E6")
158 ("Magenta" . "#FF00FF")
159 ("Maroon" . "#800000")
160 ("MediumAquaMarine" . "#66CDAA")
161 ("MediumBlue" . "#0000CD")
162 ("MediumOrchid" . "#BA55D3")
163 ("MediumPurple" . "#9370D8")
164 ("MediumSeaGreen" . "#3CB371")
165 ("MediumSlateBlue" . "#7B68EE")
166 ("MediumSpringGreen" . "#00FA9A")
167 ("MediumTurquoise" . "#48D1CC")
168 ("MediumVioletRed" . "#C71585")
169 ("MidnightBlue" . "#191970")
170 ("MintCream" . "#F5FFFA")
171 ("MistyRose" . "#FFE4E1")
172 ("Moccasin" . "#FFE4B5")
173 ("NavajoWhite" . "#FFDEAD")
174 ("Navy" . "#000080")
175 ("OldLace" . "#FDF5E6")
176 ("Olive" . "#808000")
177 ("OliveDrab" . "#6B8E23")
178 ("Orange" . "#FFA500")
179 ("OrangeRed" . "#FF4500")
180 ("Orchid" . "#DA70D6")
181 ("PaleGoldenRod" . "#EEE8AA")
182 ("PaleGreen" . "#98FB98")
183 ("PaleTurquoise" . "#AFEEEE")
184 ("PaleVioletRed" . "#D87093")
185 ("PapayaWhip" . "#FFEFD5")
186 ("PeachPuff" . "#FFDAB9")
187 ("Peru" . "#CD853F")
188 ("Pink" . "#FFC0CB")
189 ("Plum" . "#DDA0DD")
190 ("PowderBlue" . "#B0E0E6")
191 ("Purple" . "#800080")
192 ("Red" . "#FF0000")
193 ("RosyBrown" . "#BC8F8F")
194 ("RoyalBlue" . "#4169E1")
195 ("SaddleBrown" . "#8B4513")
196 ("Salmon" . "#FA8072")
197 ("SandyBrown" . "#F4A460")
198 ("SeaGreen" . "#2E8B57")
199 ("SeaShell" . "#FFF5EE")
200 ("Sienna" . "#A0522D")
201 ("Silver" . "#C0C0C0")
202 ("SkyBlue" . "#87CEEB")
203 ("SlateBlue" . "#6A5ACD")
204 ("SlateGray" . "#708090")
205 ("SlateGrey" . "#708090")
206 ("Snow" . "#FFFAFA")
207 ("SpringGreen" . "#00FF7F")
208 ("SteelBlue" . "#4682B4")
209 ("Tan" . "#D2B48C")
210 ("Teal" . "#008080")
211 ("Thistle" . "#D8BFD8")
212 ("Tomato" . "#FF6347")
213 ("Turquoise" . "#40E0D0")
214 ("Violet" . "#EE82EE")
215 ("Wheat" . "#F5DEB3")
216 ("White" . "#FFFFFF")
217 ("WhiteSmoke" . "#F5F5F5")
218 ("Yellow" . "#FFFF00")
219 ("YellowGreen" . "#9ACD32"))
220 "Alist of HTML colors.
221 Each entry should have the form (COLOR-NAME . HEXADECIMAL-COLOR)."
222 :group 'rainbow)
223
224 (defcustom rainbow-html-colors-major-mode-list
225 '(html-mode css-mode php-mode nxml-mode xml-mode)
226 "List of major mode where HTML colors are enabled when
227 `rainbow-html-colors' is set to auto."
228 :group 'rainbow)
229
230 (defcustom rainbow-html-colors 'auto
231 "When to enable HTML colors.
232 If set to t, the HTML colors will be enabled. If set to nil, the
233 HTML colors will not be enabled. If set to auto, the HTML colors
234 will be enabled if a major mode has been detected from the
235 `rainbow-html-colors-major-mode-list'."
236 :group 'rainbow)
237
238 ;; X colors
239 (defvar rainbow-x-colors-font-lock-keywords
240 `((,(regexp-opt (x-defined-colors) 'words)
241 (0 (rainbow-colorize-itself))))
242 "Font-lock keywords to add for X colors.")
243
244 (defcustom rainbow-x-colors-major-mode-list
245 '(emacs-lisp-mode lisp-interaction-mode c-mode c++-mode java-mode)
246 "List of major mode where X colors are enabled when
247 `rainbow-x-colors' is set to auto."
248 :group 'rainbow)
249
250 (defcustom rainbow-x-colors 'auto
251 "When to enable X colors.
252 If set to t, the X colors will be enabled. If set to nil, the
253 X colors will not be enabled. If set to auto, the X colors
254 will be enabled if a major mode has been detected from the
255 `rainbow-x-colors-major-mode-list'."
256 :group 'rainbow)
257
258 ;; LaTeX colors
259 (defvar rainbow-latex-rgb-colors-font-lock-keywords
260 '(("{rgb}{\\([0-9.]+\\),\\([0-9.]+\\),\\([0-9.]+\\)}"
261 (0 (rainbow-colorize-rgb-float)))
262 ("{RGB}{\\([0-9]\\{1,3\\}\\),\\([0-9]\\{1,3\\}\\),\\([0-9]\\{1,3\\}\\)}"
263 (0 (rainbow-colorize-rgb)))
264 ("{HTML}{\\([0-9A-Fa-f]\\{6\\}\\)}"
265 (0 (rainbow-colorize-hexadecimal-without-sharp))))
266 "Font-lock keywords to add for X colors.")
267
268 (defcustom rainbow-latex-colors-major-mode-list
269 '(latex-mode)
270 "List of major mode where X colors are enabled when
271 `rainbow-x-colors' is set to auto."
272 :group 'rainbow)
273
274 (defcustom rainbow-latex-colors 'auto
275 "When to enable LaTeX colors.
276 If set to t, the LaTeX colors will be enabled. If set to nil, the
277 X colors will not be enabled. If set to auto, the LaTeX colors
278 will be enabled if a major mode has been detected from the
279 `rainbow-latex-colors-major-mode-list'."
280 :group 'rainbow)
281
282 ;; Functions
283 (defun rainbow-colorize-match (color)
284 "Return a matched string propertized with a face whose
285 background is COLOR. The foreground is computed using
286 `rainbow-color-luminance', and is either white or black."
287 (put-text-property
288 (match-beginning 0) (match-end 0)
289 'face `((:foreground ,(if (> 0.5 (rainbow-x-color-luminance color))
290 "white" "black"))
291 (:background ,color))))
292
293 (defun rainbow-colorize-itself ()
294 "Colorize a match with itself."
295 (rainbow-colorize-match (match-string-no-properties 0)))
296
297 (defun rainbow-colorize-hexadecimal-without-sharp ()
298 "Colorize an hexadecimal colors and prepend # to it."
299 (rainbow-colorize-match (concat "#" (match-string-no-properties 1))))
300
301 (defun rainbow-colorize-by-assoc (assoc-list)
302 "Colorize a match with its association from ASSOC-LIST."
303 (rainbow-colorize-match (cdr (assoc-string (match-string-no-properties 0)
304 assoc-list t))))
305
306 (defun rainbow-rgb-relative-to-absolute (number)
307 "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
308 This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\"."
309 (let ((string-length (- (length number) 1)))
310 ;; Is this a number with %?
311 (if (eq (elt number string-length) ?%)
312 (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
313 (string-to-number number))))
314
315 (defun rainbow-hue-to-rgb (x y h)
316 "Convert X Y H to RGB value."
317 (when (< h 0) (incf h))
318 (when (> h 1) (decf h))
319 (cond ((< h (/ 1 6.0)) (+ x (* (- y x) h 6)))
320 ((< h 0.5) y)
321 ((< h (/ 2.0 3.0)) (+ x (* (- y x) (- (/ 2.0 3.0) h) 6)))
322 (t x)))
323
324 (defun rainbow-hsl-to-rgb-fractions (h s l)
325 "Convert H S L to fractional RGB values."
326 (let (m1 m2)
327 (if (<= l 0.5)
328 (setq m2 (* l (+ s 1)))
329 (setq m2 (- (+ l s) (* l s))))
330 (setq m1 (- (* l 2) m2))
331 (list (rainbow-hue-to-rgb m1 m2 (+ h (/ 1 3.0)))
332 (rainbow-hue-to-rgb m1 m2 h)
333 (rainbow-hue-to-rgb m1 m2 (- h (/ 1 3.0))))))
334
335 (defun rainbow-colorize-hsl ()
336 "Colorize a match with itself."
337 (let ((h (/ (string-to-number (match-string-no-properties 1)) 360.0))
338 (s (/ (string-to-number (match-string-no-properties 2)) 100.0))
339 (l (/ (string-to-number (match-string-no-properties 3)) 100.0)))
340 (rainbow-colorize-match
341 (multiple-value-bind (r g b)
342 (rainbow-hsl-to-rgb-fractions h s l)
343 (format "#%02X%02X%02X" (* r 255) (* g 255) (* b 255))))))
344
345 (defun rainbow-colorize-rgb ()
346 "Colorize a match with itself."
347 (let ((r (rainbow-rgb-relative-to-absolute (match-string-no-properties 1)))
348 (g (rainbow-rgb-relative-to-absolute (match-string-no-properties 2)))
349 (b (rainbow-rgb-relative-to-absolute (match-string-no-properties 3))))
350 (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
351
352 (defun rainbow-colorize-rgb-float ()
353 "Colorize a match with itself, with relative value."
354 (let ((r (* (string-to-number (match-string-no-properties 1)) 255.0))
355 (g (* (string-to-number (match-string-no-properties 2)) 255.0))
356 (b (* (string-to-number (match-string-no-properties 3)) 255.0)))
357 (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
358
359 (defun rainbow-color-luminance (red green blue)
360 "Calculate the luminance of color composed of RED, BLUE and GREEN.
361 Return a value between 0 and 1."
362 (/ (+ (* .2126 red) (* .7152 green) (* .0722 blue)) 256))
363
364 (defun rainbow-x-color-luminance (color)
365 "Calculate the luminance of a color string (e.g. \"#ffaa00\", \"blue\").
366 Return a value between 0 and 1."
367 (let* ((values (x-color-values color))
368 (r (/ (car values) 256.0))
369 (g (/ (cadr values) 256.0))
370 (b (/ (caddr values) 256.0)))
371 (rainbow-color-luminance r g b)))
372
373 (defun rainbow-turn-on ()
374 "Turn on raibow-mode."
375 (font-lock-add-keywords nil
376 rainbow-hexadecimal-colors-font-lock-keywords)
377 ;; Activate X colors?
378 (when (or (eq rainbow-x-colors t)
379 (and (eq rainbow-x-colors 'auto)
380 (memq major-mode rainbow-x-colors-major-mode-list)))
381 (font-lock-add-keywords nil
382 rainbow-x-colors-font-lock-keywords))
383 ;; Activate LaTeX colors?
384 (when (or (eq rainbow-latex-colors t)
385 (and (eq rainbow-latex-colors 'auto)
386 (memq major-mode rainbow-latex-colors-major-mode-list)))
387 (font-lock-add-keywords nil
388 rainbow-latex-rgb-colors-font-lock-keywords))
389 ;; Activate HTML colors?
390 (when (or (eq rainbow-html-colors t)
391 (and (eq rainbow-html-colors 'auto)
392 (memq major-mode rainbow-html-colors-major-mode-list)))
393 (setq rainbow-html-colors-font-lock-keywords
394 `((,(regexp-opt (mapcar 'car rainbow-html-colors-alist) 'words)
395 (0 (rainbow-colorize-by-assoc rainbow-html-colors-alist)))))
396 (font-lock-add-keywords nil
397 `(,@rainbow-html-colors-font-lock-keywords
398 ,@rainbow-html-rgb-colors-font-lock-keywords))))
399
400 (defun rainbow-turn-off ()
401 "Turn off rainbow-mode."
402 (font-lock-remove-keywords
403 nil
404 `(,@rainbow-hexadecimal-colors-font-lock-keywords
405 ,@rainbow-x-colors-font-lock-keywords
406 ,@rainbow-latex-rgb-colors-font-lock-keywords
407 ,@rainbow-html-colors-font-lock-keywords
408 ,@rainbow-html-rgb-colors-font-lock-keywords)))
409
410 ;;;###autoload
411 (define-minor-mode rainbow-mode
412 "Colorize strings that represent colors.
413 This will fontify with colors the string like \"#aabbcc\" or \"blue\"."
414 :lighter " Rbow"
415 (progn
416 (if rainbow-mode
417 (rainbow-turn-on)
418 (rainbow-turn-off))
419 ;; Turn on font lock
420 (font-lock-mode 1)))
421
422 (provide 'rainbow-mode)
423
424 ;;; rainbow-mode.el ends here