Bug 7458: Make key press like Left + right ctrl work when right is not control.
[bpt/emacs.git] / lisp / gnus / shr-color.el
CommitLineData
ef6a2907
JD
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.
37Must 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.
43This value is used to compare result for `ciede2000'. Its an
44absolute value without any unit."
45 :group 'shr
46 :type 'integer)
47
094ae2ab
G
48(defconst shr-color-html-colors-alist
49 '(("AliceBlue" . "#F0F8FF")
50 ("AntiqueWhite" . "#FAEBD7")
51 ("Aqua" . "#00FFFF")
52 ("Aquamarine" . "#7FFFD4")
53 ("Azure" . "#F0FFFF")
54 ("Beige" . "#F5F5DC")
55 ("Bisque" . "#FFE4C4")
56 ("Black" . "#000000")
57 ("BlanchedAlmond" . "#FFEBCD")
58 ("Blue" . "#0000FF")
59 ("BlueViolet" . "#8A2BE2")
60 ("Brown" . "#A52A2A")
61 ("BurlyWood" . "#DEB887")
62 ("CadetBlue" . "#5F9EA0")
63 ("Chartreuse" . "#7FFF00")
64 ("Chocolate" . "#D2691E")
65 ("Coral" . "#FF7F50")
66 ("CornflowerBlue" . "#6495ED")
67 ("Cornsilk" . "#FFF8DC")
68 ("Crimson" . "#DC143C")
69 ("Cyan" . "#00FFFF")
70 ("DarkBlue" . "#00008B")
71 ("DarkCyan" . "#008B8B")
72 ("DarkGoldenRod" . "#B8860B")
73 ("DarkGray" . "#A9A9A9")
74 ("DarkGrey" . "#A9A9A9")
75 ("DarkGreen" . "#006400")
76 ("DarkKhaki" . "#BDB76B")
77 ("DarkMagenta" . "#8B008B")
78 ("DarkOliveGreen" . "#556B2F")
79 ("Darkorange" . "#FF8C00")
80 ("DarkOrchid" . "#9932CC")
81 ("DarkRed" . "#8B0000")
82 ("DarkSalmon" . "#E9967A")
83 ("DarkSeaGreen" . "#8FBC8F")
84 ("DarkSlateBlue" . "#483D8B")
85 ("DarkSlateGray" . "#2F4F4F")
86 ("DarkSlateGrey" . "#2F4F4F")
87 ("DarkTurquoise" . "#00CED1")
88 ("DarkViolet" . "#9400D3")
89 ("DeepPink" . "#FF1493")
90 ("DeepSkyBlue" . "#00BFFF")
91 ("DimGray" . "#696969")
92 ("DimGrey" . "#696969")
93 ("DodgerBlue" . "#1E90FF")
94 ("FireBrick" . "#B22222")
95 ("FloralWhite" . "#FFFAF0")
96 ("ForestGreen" . "#228B22")
97 ("Fuchsia" . "#FF00FF")
98 ("Gainsboro" . "#DCDCDC")
99 ("GhostWhite" . "#F8F8FF")
100 ("Gold" . "#FFD700")
101 ("GoldenRod" . "#DAA520")
102 ("Gray" . "#808080")
103 ("Grey" . "#808080")
104 ("Green" . "#008000")
105 ("GreenYellow" . "#ADFF2F")
106 ("HoneyDew" . "#F0FFF0")
107 ("HotPink" . "#FF69B4")
108 ("IndianRed" . "#CD5C5C")
109 ("Indigo" . "#4B0082")
110 ("Ivory" . "#FFFFF0")
111 ("Khaki" . "#F0E68C")
112 ("Lavender" . "#E6E6FA")
113 ("LavenderBlush" . "#FFF0F5")
114 ("LawnGreen" . "#7CFC00")
115 ("LemonChiffon" . "#FFFACD")
116 ("LightBlue" . "#ADD8E6")
117 ("LightCoral" . "#F08080")
118 ("LightCyan" . "#E0FFFF")
119 ("LightGoldenRodYellow" . "#FAFAD2")
120 ("LightGray" . "#D3D3D3")
121 ("LightGrey" . "#D3D3D3")
122 ("LightGreen" . "#90EE90")
123 ("LightPink" . "#FFB6C1")
124 ("LightSalmon" . "#FFA07A")
125 ("LightSeaGreen" . "#20B2AA")
126 ("LightSkyBlue" . "#87CEFA")
127 ("LightSlateGray" . "#778899")
128 ("LightSlateGrey" . "#778899")
129 ("LightSteelBlue" . "#B0C4DE")
130 ("LightYellow" . "#FFFFE0")
131 ("Lime" . "#00FF00")
132 ("LimeGreen" . "#32CD32")
133 ("Linen" . "#FAF0E6")
134 ("Magenta" . "#FF00FF")
135 ("Maroon" . "#800000")
136 ("MediumAquaMarine" . "#66CDAA")
137 ("MediumBlue" . "#0000CD")
138 ("MediumOrchid" . "#BA55D3")
139 ("MediumPurple" . "#9370D8")
140 ("MediumSeaGreen" . "#3CB371")
141 ("MediumSlateBlue" . "#7B68EE")
142 ("MediumSpringGreen" . "#00FA9A")
143 ("MediumTurquoise" . "#48D1CC")
144 ("MediumVioletRed" . "#C71585")
145 ("MidnightBlue" . "#191970")
146 ("MintCream" . "#F5FFFA")
147 ("MistyRose" . "#FFE4E1")
148 ("Moccasin" . "#FFE4B5")
149 ("NavajoWhite" . "#FFDEAD")
150 ("Navy" . "#000080")
151 ("OldLace" . "#FDF5E6")
152 ("Olive" . "#808000")
153 ("OliveDrab" . "#6B8E23")
154 ("Orange" . "#FFA500")
155 ("OrangeRed" . "#FF4500")
156 ("Orchid" . "#DA70D6")
157 ("PaleGoldenRod" . "#EEE8AA")
158 ("PaleGreen" . "#98FB98")
159 ("PaleTurquoise" . "#AFEEEE")
160 ("PaleVioletRed" . "#D87093")
161 ("PapayaWhip" . "#FFEFD5")
162 ("PeachPuff" . "#FFDAB9")
163 ("Peru" . "#CD853F")
164 ("Pink" . "#FFC0CB")
165 ("Plum" . "#DDA0DD")
166 ("PowderBlue" . "#B0E0E6")
167 ("Purple" . "#800080")
168 ("Red" . "#FF0000")
169 ("RosyBrown" . "#BC8F8F")
170 ("RoyalBlue" . "#4169E1")
171 ("SaddleBrown" . "#8B4513")
172 ("Salmon" . "#FA8072")
173 ("SandyBrown" . "#F4A460")
174 ("SeaGreen" . "#2E8B57")
175 ("SeaShell" . "#FFF5EE")
176 ("Sienna" . "#A0522D")
177 ("Silver" . "#C0C0C0")
178 ("SkyBlue" . "#87CEEB")
179 ("SlateBlue" . "#6A5ACD")
180 ("SlateGray" . "#708090")
181 ("SlateGrey" . "#708090")
182 ("Snow" . "#FFFAFA")
183 ("SpringGreen" . "#00FF7F")
184 ("SteelBlue" . "#4682B4")
185 ("Tan" . "#D2B48C")
186 ("Teal" . "#008080")
187 ("Thistle" . "#D8BFD8")
188 ("Tomato" . "#FF6347")
189 ("Turquoise" . "#40E0D0")
190 ("Violet" . "#EE82EE")
191 ("Wheat" . "#F5DEB3")
192 ("White" . "#FFFFFF")
193 ("WhiteSmoke" . "#F5F5F5")
194 ("Yellow" . "#FFFF00")
195 ("YellowGreen" . "#9ACD32"))
196 "Alist of HTML colors.
197Each entry should have the form (COLOR-NAME . HEXADECIMAL-COLOR).")
198
ef6a2907
JD
199(defun shr-color-relative-to-absolute (number)
200 "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
201This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\"."
202 (let ((string-length (- (length number) 1)))
203 ;; Is this a number with %?
204 (if (eq (elt number string-length) ?%)
205 (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
206 (string-to-number number))))
207
a2994808
JD
208(defun shr-color-hue-to-rgb (x y h)
209 "Convert X Y H to RGB value."
210 (when (< h 0) (incf h))
211 (when (> h 1) (decf h))
212 (cond ((< h (/ 1 6.0)) (+ x (* (- y x) h 6)))
213 ((< h 0.5) y)
214 ((< h (/ 2.0 3.0)) (+ x (* (- y x) (- (/ 2.0 3.0) h) 6)))
215 (t x)))
216
ef6a2907
JD
217(defun shr-color-hsl-to-rgb-fractions (h s l)
218 "Convert H S L to fractional RGB values."
219 (let (m1 m2)
220 (if (<= l 0.5)
221 (setq m2 (* l (+ s 1)))
222 (setq m2 (- (+ l s) (* l s))))
223 (setq m1 (- (* l 2) m2))
a2994808
JD
224 (list (shr-color-hue-to-rgb m1 m2 (+ h (/ 1 3.0)))
225 (shr-color-hue-to-rgb m1 m2 h)
226 (shr-color-hue-to-rgb m1 m2 (- h (/ 1 3.0))))))
ef6a2907
JD
227
228(defun shr-color->hexadecimal (color)
229 "Convert any color format to hexadecimal representation.
230Like rgb() or hsl()."
231 (when color
094ae2ab
G
232 (cond
233 ;; Hexadecimal color: #abc or #aabbcc
234 ((string-match-p
235 "#[0-9a-fA-F]\\{3\\}[0-9a-fA-F]\\{3\\}?"
236 color)
237 color)
238 ;; rgb() or rgba() colors
239 ((or (string-match
240 "rgb(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*)"
241 color)
242 (string-match
243 "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*)"
244 color))
245 (format "#%02X%02X%02X"
246 (shr-color-relative-to-absolute (match-string-no-properties 1 color))
247 (shr-color-relative-to-absolute (match-string-no-properties 2 color))
248 (shr-color-relative-to-absolute (match-string-no-properties 3 color))))
249 ;; hsl() or hsla() colors
250 ((or (string-match
251 "hsl(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*)"
252 color)
253 (string-match
254 "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*)"
255 color))
256 (let ((h (/ (string-to-number (match-string-no-properties 1 color)) 360.0))
257 (s (/ (string-to-number (match-string-no-properties 2 color)) 100.0))
258 (l (/ (string-to-number (match-string-no-properties 3 color)) 100.0)))
259 (destructuring-bind (r g b)
260 (shr-color-hsl-to-rgb-fractions h s l)
261 (format "#%02X%02X%02X" (* r 255) (* g 255) (* b 255)))))
262 ;; Color names
4b8b6f60 263 ((cdr (assoc-string color shr-color-html-colors-alist t)))
094ae2ab
G
264 ;; Unrecognized color :(
265 (t
266 nil))))
ef6a2907
JD
267
268(defun set-minimum-interval (val1 val2 min max interval &optional fixed)
269 "Set minimum interval between VAL1 and VAL2 to INTERVAL.
270The values are bound by MIN and MAX.
271If FIXED is t, then val1 will not be touched."
272 (let ((diff (abs (- val1 val2))))
273 (unless (>= diff interval)
274 (if fixed
275 (let* ((missing (- interval diff))
276 ;; If val2 > val1, try to increase val2
277 ;; That's the "good direction"
278 (val2-good-direction
279 (if (> val2 val1)
280 (min max (+ val2 missing))
281 (max min (- val2 missing))))
282 (diff-val2-good-direction-val1 (abs (- val2-good-direction val1))))
283 (if (>= diff-val2-good-direction-val1 interval)
284 (setq val2 val2-good-direction)
285 ;; Good-direction is not so good, compute bad-direction
286 (let* ((val2-bad-direction
287 (if (> val2 val1)
288 (max min (- val1 interval))
289 (min max (+ val1 interval))))
290 (diff-val2-bad-direction-val1 (abs (- val2-bad-direction val1))))
291 (if (>= diff-val2-bad-direction-val1 interval)
292 (setq val2 val2-bad-direction)
293 ;; Still not good, pick the best and prefer good direction
294 (setq val2
295 (if (>= diff-val2-good-direction-val1 diff-val2-bad-direction-val1)
296 val2-good-direction
297 val2-bad-direction))))))
298 ;; No fixed, move val1 and val2
299 (let ((missing (/ (- interval diff) 2.0)))
300 (if (< val1 val2)
301 (setq val1 (max min (- val1 missing))
302 val2 (min max (+ val2 missing)))
303 (setq val2 (max min (- val2 missing))
304 val1 (min max (+ val1 missing))))
305 (setq diff (abs (- val1 val2))) ; Recompute diff
306 (unless (>= diff interval)
307 ;; Not ok, we hit a boundary
308 (let ((missing (- interval diff)))
309 (cond ((= val1 min)
310 (setq val2 (+ val2 missing)))
311 ((= val2 min)
312 (setq val1 (+ val1 missing)))
313 ((= val1 max)
314 (setq val2 (- val2 missing)))
315 ((= val2 max)
316 (setq val1 (- val1 missing)))))))))
317 (list val1 val2)))
318
319(defun shr-color-visible (bg fg &optional fixed-background)
320 "Check that BG and FG colors are visible if they are drawn on each other.
321Return t if they are. If they are too similar, two new colors are
322returned instead.
323If FIXED-BACKGROUND is set, and if the color are not visible, a
324new background color will not be computed. Only the foreground
325color will be adapted to be visible on BG."
326 ;; Convert fg and bg to CIE Lab
327 (let* ((fg-lab (apply 'rgb->lab (rgb->normalize fg)))
328 (bg-lab (apply 'rgb->lab (rgb->normalize bg)))
329 ;; Compute color distance using CIE DE 2000
330 (fg-bg-distance (color-lab-ciede2000 fg-lab bg-lab))
331 ;; Compute luminance distance (substract L component)
332 (luminance-distance (abs (- (car fg-lab) (car bg-lab)))))
333 (if (and (>= fg-bg-distance shr-color-visible-distance-min)
334 (>= luminance-distance shr-color-visible-luminance-min))
335 (list bg fg)
336 ;; Not visible, try to change luminance to make them visible
337 (let ((Ls (set-minimum-interval (car bg-lab) (car fg-lab) 0 100
338 shr-color-visible-luminance-min
339 fixed-background)))
340 (setcar bg-lab (car Ls))
341 (setcar fg-lab (cadr Ls))
342 (list
343 (apply 'format "#%02x%02x%02x"
344 (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) (apply 'lab->rgb bg-lab)))
345 (apply 'format "#%02x%02x%02x"
346 (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) (apply 'lab->rgb fg-lab))))))))
347
348(provide 'shr-color)
349
350;;; shr-color.el ends here