shr-color: fix several function calls
[gnus] / lisp / shr-color.el
1 ;;; shr-color.el --- Simple HTML Renderer color management
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; This package handles colors display for shr.
26
27 ;;; Code:
28
29 (require 'color-lab)
30
31 (defgroup shr-color nil
32   "Simple HTML Renderer colors"
33   :group 'shr)
34
35 (defcustom shr-color-visible-luminance-min 40
36   "Minimum luminance distance between two colors to be considered visible.
37 Must be between 0 and 100."
38   :group 'shr
39   :type 'float)
40
41 (defcustom shr-color-visible-distance-min 5
42   "Minimum color distance between two colors to be considered visible.
43 This value is used to compare result for `ciede2000'. Its an
44 absolute value without any unit."
45   :group 'shr
46   :type 'integer)
47
48 (defun shr-color-relative-to-absolute (number)
49   "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
50 This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\"."
51   (let ((string-length (- (length number) 1)))
52     ;; Is this a number with %?
53     (if (eq (elt number string-length) ?%)
54         (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
55       (string-to-number number))))
56
57 (defun shr-color-hue-to-rgb (x y h)
58   "Convert X Y H to RGB value."
59   (when (< h 0) (incf h))
60   (when (> h 1) (decf h))
61   (cond ((< h (/ 1 6.0)) (+ x (* (- y x) h 6)))
62         ((< h 0.5) y)
63         ((< h (/ 2.0 3.0)) (+ x (* (- y x) (- (/ 2.0 3.0) h) 6)))
64         (t x)))
65
66 (defun shr-color-hsl-to-rgb-fractions (h s l)
67   "Convert H S L to fractional RGB values."
68   (let (m1 m2)
69     (if (<= l 0.5)
70         (setq m2 (* l (+ s 1)))
71         (setq m2 (- (+ l s) (* l s))))
72     (setq m1 (- (* l 2) m2))
73     (list (shr-color-hue-to-rgb m1 m2 (+ h (/ 1 3.0)))
74           (shr-color-hue-to-rgb m1 m2 h)
75           (shr-color-hue-to-rgb m1 m2 (- h (/ 1 3.0))))))
76
77 (defun shr-color->hexadecimal (color)
78   "Convert any color format to hexadecimal representation.
79 Like rgb() or hsl()."
80   (when color
81     (cond ((or (string-match
82                 "rgb(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*)"
83                 color)
84                (string-match
85                 "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*)"
86                 color))
87            (format "#%02X%02X%02X"
88                    (shr-color-relative-to-absolute (match-string-no-properties 1 color))
89                    (shr-color-relative-to-absolute (match-string-no-properties 2 color))
90                    (shr-color-relative-to-absolute (match-string-no-properties 3 color))))
91           ((or (string-match
92                 "hsl(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*)"
93                 color)
94                (string-match
95                 "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*)"
96                 color))
97            (let ((h (/ (string-to-number (match-string-no-properties 1 color)) 360.0))
98                  (s (/ (string-to-number (match-string-no-properties 2 color)) 100.0))
99                  (l (/ (string-to-number (match-string-no-properties 3 color)) 100.0)))
100              (destructuring-bind (r g b)
101                  (shr-color-hsl-to-rgb-fractions h s l)
102                (format "#%02X%02X%02X" (* r 255) (* g 255) (* b 255)))))
103           (t
104            color))))
105
106 (defun set-minimum-interval (val1 val2 min max interval &optional fixed)
107   "Set minimum interval between VAL1 and VAL2 to INTERVAL.
108 The values are bound by MIN and MAX.
109 If FIXED is t, then val1 will not be touched."
110   (let ((diff (abs (- val1 val2))))
111     (unless (>= diff interval)
112       (if fixed
113           (let* ((missing (- interval diff))
114                  ;; If val2 > val1, try to increase val2
115                  ;; That's the "good direction"
116                  (val2-good-direction
117                   (if (> val2 val1)
118                       (min max (+ val2 missing))
119                     (max min (- val2 missing))))
120                  (diff-val2-good-direction-val1 (abs (- val2-good-direction val1))))
121             (if (>= diff-val2-good-direction-val1 interval)
122                 (setq val2 val2-good-direction)
123               ;; Good-direction is not so good, compute bad-direction
124               (let* ((val2-bad-direction
125                       (if (> val2 val1)
126                           (max min (- val1 interval))
127                         (min max (+ val1 interval))))
128                      (diff-val2-bad-direction-val1 (abs (- val2-bad-direction val1))))
129                 (if (>= diff-val2-bad-direction-val1 interval)
130                     (setq val2 val2-bad-direction)
131                   ;; Still not good, pick the best and prefer good direction
132                   (setq val2
133                         (if (>= diff-val2-good-direction-val1 diff-val2-bad-direction-val1)
134                             val2-good-direction
135                           val2-bad-direction))))))
136         ;; No fixed, move val1 and val2
137         (let ((missing (/ (- interval diff) 2.0)))
138           (if (< val1 val2)
139               (setq val1 (max min (- val1 missing))
140                     val2 (min max (+ val2 missing)))
141             (setq val2 (max min (- val2 missing))
142                   val1 (min max (+ val1 missing))))
143           (setq diff (abs (- val1 val2)))   ; Recompute diff
144           (unless (>= diff interval)
145             ;; Not ok, we hit a boundary
146             (let ((missing (- interval diff)))
147               (cond ((= val1 min)
148                      (setq val2 (+ val2 missing)))
149                     ((= val2 min)
150                      (setq val1 (+ val1 missing)))
151                     ((= val1 max)
152                      (setq val2 (- val2 missing)))
153                     ((= val2 max)
154                      (setq val1 (- val1 missing)))))))))
155     (list val1 val2)))
156
157 (defun shr-color-visible (bg fg &optional fixed-background)
158   "Check that BG and FG colors are visible if they are drawn on each other.
159 Return t if they are. If they are too similar, two new colors are
160 returned instead.
161 If FIXED-BACKGROUND is set, and if the color are not visible, a
162 new background color will not be computed. Only the foreground
163 color will be adapted to be visible on BG."
164   ;; Convert fg and bg to CIE Lab
165   (let* ((fg-lab (apply 'rgb->lab (rgb->normalize fg)))
166          (bg-lab (apply 'rgb->lab (rgb->normalize bg)))
167          ;; Compute color distance using CIE DE 2000
168          (fg-bg-distance (color-lab-ciede2000 fg-lab bg-lab))
169          ;; Compute luminance distance (substract L component)
170          (luminance-distance (abs (- (car fg-lab) (car bg-lab)))))
171     (if (and (>= fg-bg-distance shr-color-visible-distance-min)
172              (>= luminance-distance shr-color-visible-luminance-min))
173         (list bg fg)
174       ;; Not visible, try to change luminance to make them visible
175       (let ((Ls (set-minimum-interval (car bg-lab) (car fg-lab) 0 100
176                                       shr-color-visible-luminance-min
177                                       fixed-background)))
178         (setcar bg-lab (car Ls))
179         (setcar fg-lab (cadr Ls))
180         (list
181          (apply 'format "#%02x%02x%02x"
182                 (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) (apply 'lab->rgb bg-lab)))
183          (apply 'format "#%02x%02x%02x"
184                 (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) (apply 'lab->rgb fg-lab))))))))
185
186 (provide 'shr-color)
187
188 ;;; shr-color.el ends here