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