color: add more function, use them in shr-color
authorJulien Danjou <julien@danjou.info>
Thu, 25 Nov 2010 13:56:45 +0000 (14:56 +0100)
committerJulien Danjou <julien@danjou.info>
Thu, 25 Nov 2010 13:57:21 +0000 (14:57 +0100)
Signed-off-by: Julien Danjou <julien@danjou.info>
lisp/ChangeLog
lisp/color.el
lisp/shr-color.el

index c4a922a..c6e7108 100644 (file)
@@ -1,6 +1,11 @@
 2010-11-25  Julien Danjou  <julien@danjou.info>
 
+       * shr-color.el (shr-color->hexadecimal): Use color-rgb->hex
+
        * color.el: Rename from color-lab.el
+       (color-rgb->hex): Add.
+       (color-complement): Add.
+       (color-complement-hex): Add.
 
        * gnus-sum.el (gnus-summary-widget-forward): Add, and bind to [tab].
 
index e2eb1eb..67ee9e7 100644 (file)
   (unless (boundp 'float-pi)
     (defconst float-pi (* 4 (atan 1)) "The value of Pi (3.1415926...).")))
 
+(defun color-rgb->hex  (red green blue)
+  "Return hexadecimal notation for RED GREEN BLUE color.
+RED GREEN BLUE must be values between [0,1]."
+  (format "#%02x%02x%02x"
+          (* red 255) (* green 255) (* blue 255)))
+
+(defun color-complement (color)
+  "Return the color that is the complement of COLOR."
+  (let ((color (color-rgb->normalize color)))
+    (list (- 1.0 (car color))
+          (- 1.0 (cadr color))
+          (- 1.0 (caddr color)))))
+
+(defun color-complement-hex (color)
+  "Return the color that is the complement of COLOR, in hexadecimal format."
+  (apply 'color-rgb->hex (color-complement color)))
+
 (defun color-rgb->hsv (red green blue)
   "Convert RED GREEN BLUE values to HSV representation.
 Hue is in radian. Saturation and values are between 0 and 1."
@@ -63,10 +80,10 @@ Hue is in radian. Saturation and values are between 0 and 1."
 
 (defun color-rgb->hsl (red green blue)
   "Convert RED GREEN BLUE colors to their HSL representation.
-RED, GREEN and BLUE must be between 0 and 255."
-  (let* ((r (/ red 255.0))
-         (g (/ green 255.0))
-         (b (/ blue 255.0))
+RED, GREEN and BLUE must be between [0,1]."
+  (let* ((r red)
+         (g green)
+         (b blue)
          (max (max r g b))
          (min (min r g b))
          (delta (- max min))
@@ -91,7 +108,7 @@ RED, GREEN and BLUE must be between 0 and 255."
 
 (defun color-rgb->xyz (red green blue)
   "Converts RED GREEN BLUE colors to CIE XYZ representation.
-RED, BLUE and GREEN must be between 0 and 1."
+RED, BLUE and GREEN must be between [0,1]."
   (let ((r (if (<= red 0.04045)
                (/ red 12.95)
              (expt (/ (+ red 0.055) 1.055) 2.4)))
index 1c9790d..3346b43 100644 (file)
@@ -258,7 +258,7 @@ Like rgb() or hsl()."
             (l (/ (string-to-number (match-string-no-properties 3 color)) 100.0)))
         (destructuring-bind (r g b)
             (shr-color-hsl-to-rgb-fractions h s l)
-          (format "#%02X%02X%02X" (* r 255) (* g 255) (* b 255)))))
+          (color-rgb->hex r g b))))
      ;; Color names
      ((cdr (assoc-string color shr-color-html-colors-alist t)))
      ;; Unrecognized color :(