1 ;;; rainbow-mode.el --- prints color strings with colored background
3 ;; Copyright (C) 2010 Julien Danjou
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: strings, faces
8 ;; This file is NOT part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; This minor mode will add background to strings that matches color names.
27 ;; Will be printed 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 (defun rainbow-colorize-match (color)
119 "Return a matched string propertized with a face whose
120 background is COLOR. The foreground is computed using
121 `rainbow-color-luminance', and is either white or black."
123 (match-beginning 0) (match-end 0)
124 'face `((:foreground ,(if (> 128.0 (rainbow-x-color-luminance color))
126 (:background ,color))))
128 (defun rainbow-colorize-itself ()
129 "Colorize a match with itself."
130 (rainbow-colorize-match (match-string-no-properties 0)))
132 (defun rainbow-colorize-by-assoc (assoc-list)
133 "Colorize a match with its association from ASSOC-LIST."
134 (rainbow-colorize-match (cdr (assoc (match-string-no-properties 0) assoc-list))))
136 (defun rainbow-rgb-relative-to-absolute (number)
137 "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
138 This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\""
139 (let ((string-length (- (length number) 1)))
140 ;; Is this a number with %?
141 (if (eq (elt number string-length) ?%)
142 (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
143 (string-to-number number))))
145 (defun rainbow-colorize-rgb ()
146 "Colorize a match with itself."
147 (let ((r (rainbow-rgb-relative-to-absolute (match-string-no-properties 1)))
148 (g (rainbow-rgb-relative-to-absolute (match-string-no-properties 2)))
149 (b (rainbow-rgb-relative-to-absolute (match-string-no-properties 3))))
150 (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
152 (defun rainbow-color-luminance (red green blue)
153 "Calculate the luminance of color composed of RED, BLUE and GREEN."
154 (floor (+ (* .2126 red) (* .7152 green) (* .0722 blue)) 256))
156 (defun rainbow-x-color-luminance (color)
157 "Calculate the luminance of a color string (e.g. \"#ffaa00\", \"blue\")."
158 (let* ((values (x-color-values color))
162 (rainbow-color-luminance r g b)))
164 (defun rainbow-turn-on ()
165 "Turn on raibow-mode."
166 (font-lock-add-keywords nil
167 (list rainbow-hexadecimal-colors-font-lock-keywords))
168 ;; Activate X colors?
169 (when (or (eq rainbow-x-colors t)
170 (and (eq rainbow-x-colors 'auto)
171 (memq major-mode rainbow-x-colors-major-mode-list)))
172 (font-lock-add-keywords nil
173 (list rainbow-x-colors-font-lock-keywords)))
174 ;; Activate HTML colors?
175 (when (or (eq rainbow-html-colors t)
176 (and (eq rainbow-html-colors 'auto)
177 (memq major-mode rainbow-html-colors-major-mode-list)))
178 (setq rainbow-html-colors-font-lock-keywords
179 `(,(regexp-opt (mapcar 'car rainbow-html-colors-alist) 'words)
180 (0 (rainbow-colorize-by-assoc rainbow-html-colors-alist))))
181 (font-lock-add-keywords nil
182 `(,rainbow-html-colors-font-lock-keywords
183 ,@rainbow-html-rgb-colors-font-lock-keywords))))
185 (defun rainbow-turn-off ()
186 "Turn off rainbow-mode."
187 (font-lock-remove-keywords
190 rainbow-hexadecimal-colors-font-lock-keywords
191 rainbow-html-colors-font-lock-keywords
192 rainbow-x-colors-font-lock-keywords
193 rainbow-html-rgb-colors-font-lock-keywords)))
196 (define-minor-mode rainbow-mode
197 "Colorize strings that represent colors.
198 This will fontify with colors the string like \"#aabbcc\" or \"blue\""
207 (provide 'rainbow-mode)