1 ;;; shr-color.el --- Simple HTML Renderer color management
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Julien Danjou <julien@danjou.info>
8 ;; This file is 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/>.
25 ;; This package handles colors display for shr.
31 (defgroup shr-color nil
32 "Simple HTML Renderer colors"
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."
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."
48 (defconst shr-color-html-colors-alist
49 '(("AliceBlue" .
"#F0F8FF")
50 ("AntiqueWhite" .
"#FAEBD7")
52 ("Aquamarine" .
"#7FFFD4")
55 ("Bisque" .
"#FFE4C4")
57 ("BlanchedAlmond" .
"#FFEBCD")
59 ("BlueViolet" .
"#8A2BE2")
61 ("BurlyWood" .
"#DEB887")
62 ("CadetBlue" .
"#5F9EA0")
63 ("Chartreuse" .
"#7FFF00")
64 ("Chocolate" .
"#D2691E")
66 ("CornflowerBlue" .
"#6495ED")
67 ("Cornsilk" .
"#FFF8DC")
68 ("Crimson" .
"#DC143C")
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")
101 ("GoldenRod" .
"#DAA520")
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")
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")
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")
166 ("PowderBlue" .
"#B0E0E6")
167 ("Purple" .
"#800080")
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")
183 ("SpringGreen" .
"#00FF7F")
184 ("SteelBlue" .
"#4682B4")
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.
197 Each entry should have the form (COLOR-NAME . HEXADECIMAL-COLOR).")
199 (defun shr-color-relative-to-absolute (number)
200 "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
201 This 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
))))
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)))
214 ((< h
(/ 2.0 3.0)) (+ x
(* (- y x
) (- (/ 2.0 3.0) h
) 6)))
217 (defun shr-color-hsl-to-rgb-fractions (h s l
)
218 "Convert H S L to fractional RGB values."
221 (setq m2
(* l
(+ s
1)))
222 (setq m2
(- (+ l s
) (* l s
))))
223 (setq m1
(- (* l
2) m2
))
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))))))
228 (defun shr-color->hexadecimal
(color)
229 "Convert any color format to hexadecimal representation.
230 Like rgb() or hsl()."
233 ;; Hexadecimal color: #abc or #aabbcc
235 "\\(#[0-9a-fA-F]\\{3\\}[0-9a-fA-F]\\{3\\}?\\)"
237 (match-string 1 color
))
238 ;; rgb() or rgba() colors
240 "rgb(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*)"
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*)"
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
251 "hsl(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*)"
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*)"
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 (color-rgb->hex r g b
))))
263 ((cdr (assoc-string color shr-color-html-colors-alist t
)))
264 ;; Unrecognized color :(
268 (defun set-minimum-interval (val1 val2 min max interval
&optional fixed
)
269 "Set minimum interval between VAL1 and VAL2 to INTERVAL.
270 The values are bound by MIN and MAX.
271 If FIXED is t, then val1 will not be touched."
272 (let ((diff (abs (- val1 val2
))))
273 (unless (>= diff interval
)
275 (let* ((missing (- interval diff
))
276 ;; If val2 > val1, try to increase val2
277 ;; That's the "good direction"
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
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
295 (if (>= diff-val2-good-direction-val1 diff-val2-bad-direction-val1
)
297 val2-bad-direction
))))))
298 ;; No fixed, move val1 and val2
299 (let ((missing (/ (- interval diff
) 2.0)))
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
)))
310 (setq val2
(+ val2 missing
)))
312 (setq val1
(+ val1 missing
)))
314 (setq val2
(- val2 missing
)))
316 (setq val1
(- val1 missing
)))))))))
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.
321 Return (bg fg) if they are. If they are too similar, two new
322 colors are returned instead.
323 If FIXED-BACKGROUND is set, and if the color are not visible, a
324 new background color will not be computed. Only the foreground
325 color will be adapted to be visible on BG."
326 ;; Convert fg and bg to CIE Lab
327 (let ((fg-norm (color-rgb->normalize fg
))
328 (bg-norm (color-rgb->normalize bg
)))
329 (if (or (null fg-norm
)
332 (let* ((fg-lab (apply 'color-rgb-
>lab fg-norm
))
333 (bg-lab (apply 'color-rgb-
>lab bg-norm
))
334 ;; Compute color distance using CIE DE 2000
335 (fg-bg-distance (color-cie-de2000 fg-lab bg-lab
))
336 ;; Compute luminance distance (substract L component)
337 (luminance-distance (abs (- (car fg-lab
) (car bg-lab
)))))
338 (if (and (>= fg-bg-distance shr-color-visible-distance-min
)
339 (>= luminance-distance shr-color-visible-luminance-min
))
341 ;; Not visible, try to change luminance to make them visible
342 (let ((Ls (set-minimum-interval (car bg-lab
) (car fg-lab
) 0 100
343 shr-color-visible-luminance-min
345 (unless fixed-background
346 (setcar bg-lab
(car Ls
)))
347 (setcar fg-lab
(cadr Ls
))
351 (apply 'format
"#%02x%02x%02x"
352 (mapcar (lambda (x) (* (max (min 1 x
) 0) 255))
353 (apply 'color-lab-
>rgb bg-lab
))))
354 (apply 'format
"#%02x%02x%02x"
355 (mapcar (lambda (x) (* (max (min 1 x
) 0) 255))
356 (apply 'color-lab-
>rgb fg-lab
))))))))))
360 ;;; shr-color.el ends here