;;; rainbow-mode.el --- Colorize color names in buffers
-;; Copyright (C) 2010-2011 Free Software Foundation, Inc
+;; Copyright (C) 2010-2012 Free Software Foundation, Inc
;; Author: Julien Danjou <julien@danjou.info>
;; Keywords: faces
-;; Version: 0.2
+;; Version: 0.5
;; This file is part of GNU Emacs.
(require 'regexp-opt)
(require 'faces)
+(require 'color)
(defgroup rainbow nil
"Show color strings with a background color."
;; Hexadecimal colors
(defvar rainbow-hexadecimal-colors-font-lock-keywords
- '(("#\\(?:[0-9a-fA-F]\\{3\\}\\)+\\{1,4\\}"
+ '(("[^&]\\(#\\(?:[0-9a-fA-F]\\{3\\}\\)+\\{1,4\\}\\)"
+ (1 (rainbow-colorize-itself 1)))
+ ("^\\(#\\(?:[0-9a-fA-F]\\{3\\}\\)+\\{1,4\\}\\)"
(0 (rainbow-colorize-itself)))
("[Rr][Gg][Bb]:[0-9a-fA-F]\\{1,4\\}/[0-9a-fA-F]\\{1,4\\}/[0-9a-fA-F]\\{1,4\\}"
(0 (rainbow-colorize-itself)))
(0 (rainbow-colorize-rgb)))
("{HTML}{\\([0-9A-Fa-f]\\{6\\}\\)}"
(0 (rainbow-colorize-hexadecimal-without-sharp))))
- "Font-lock keywords to add for X colors.")
+ "Font-lock keywords to add for LaTeX colors.")
(defcustom rainbow-latex-colors-major-mode-list
'(latex-mode)
- "List of major mode where X colors are enabled when
+ "List of major mode where LaTeX colors are enabled when
`rainbow-x-colors' is set to auto."
:group 'rainbow)
(defcustom rainbow-latex-colors 'auto
"When to enable LaTeX colors.
If set to t, the LaTeX colors will be enabled. If set to nil, the
-X colors will not be enabled. If set to auto, the LaTeX colors
+LaTeX colors will not be enabled. If set to auto, the LaTeX colors
will be enabled if a major mode has been detected from the
`rainbow-latex-colors-major-mode-list'."
:group 'rainbow)
;; Functions
-(defun rainbow-colorize-match (color)
+(defun rainbow-colorize-match (color &optional match)
"Return a matched string propertized with a face whose
background is COLOR. The foreground is computed using
`rainbow-color-luminance', and is either white or black."
- (put-text-property
- (match-beginning 0) (match-end 0)
- 'face `((:foreground ,(if (> 0.5 (rainbow-x-color-luminance color))
- "white" "black"))
- (:background ,color))))
-
-(defun rainbow-colorize-itself ()
+ (let ((match (or match 0)))
+ (put-text-property
+ (match-beginning match) (match-end match)
+ 'face `((:foreground ,(if (> 0.5 (rainbow-x-color-luminance color))
+ "white" "black"))
+ (:background ,color)))))
+
+(defun rainbow-colorize-itself (&optional match)
"Colorize a match with itself."
- (rainbow-colorize-match (match-string-no-properties 0)))
+ (rainbow-colorize-match (match-string-no-properties (or match 0)) match))
(defun rainbow-colorize-hexadecimal-without-sharp ()
"Colorize an hexadecimal colors and prepend # to it."
(/ (* (string-to-number (substring number 0 string-length)) 255) 100)
(string-to-number number))))
-(defun rainbow-hue-to-rgb (x y h)
- "Convert X Y H to RGB value."
- (when (< h 0) (incf h))
- (when (> h 1) (decf h))
- (cond ((< h (/ 1 6.0)) (+ x (* (- y x) h 6)))
- ((< h 0.5) y)
- ((< h (/ 2.0 3.0)) (+ x (* (- y x) (- (/ 2.0 3.0) h) 6)))
- (t x)))
-
-(defun rainbow-hsl-to-rgb-fractions (h s l)
- "Convert H S L to fractional RGB values."
- (let (m1 m2)
- (if (<= l 0.5)
- (setq m2 (* l (+ s 1)))
- (setq m2 (- (+ l s) (* l s))))
- (setq m1 (- (* l 2) m2))
- (list (rainbow-hue-to-rgb m1 m2 (+ h (/ 1 3.0)))
- (rainbow-hue-to-rgb m1 m2 h)
- (rainbow-hue-to-rgb m1 m2 (- h (/ 1 3.0))))))
-
(defun rainbow-colorize-hsl ()
"Colorize a match with itself."
(let ((h (/ (string-to-number (match-string-no-properties 1)) 360.0))
(l (/ (string-to-number (match-string-no-properties 3)) 100.0)))
(rainbow-colorize-match
(multiple-value-bind (r g b)
- (rainbow-hsl-to-rgb-fractions h s l)
+ (color-hsl-to-rgb h s l)
(format "#%02X%02X%02X" (* r 255) (* g 255) (* b 255))))))
(defun rainbow-colorize-rgb ()