(gnus-blocked-images): Clarify privacy implications
[emacs.git] / lisp / net / shr-color.el
blob6303141c8980a341630f290f57359e9dc2b2ad89
1 ;;; shr-color.el --- Simple HTML Renderer color management -*- lexical-binding:t -*-
3 ;; Copyright (C) 2010-2018 Free Software Foundation, Inc.
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: html
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 <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This package handles colors display for shr.
27 ;;; Code:
29 (require 'color)
30 (eval-when-compile (require 'cl-lib))
32 (defgroup shr-color nil
33 "Simple HTML Renderer colors"
34 :group 'shr)
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."
39 :group 'shr-color
40 :type 'number)
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'. It's an
45 absolute value without any unit."
46 :group 'shr-color
47 :type 'integer)
49 (defconst shr-color-html-colors-alist
50 '(("AliceBlue" . "#F0F8FF")
51 ("AntiqueWhite" . "#FAEBD7")
52 ("Aqua" . "#00FFFF")
53 ("Aquamarine" . "#7FFFD4")
54 ("Azure" . "#F0FFFF")
55 ("Beige" . "#F5F5DC")
56 ("Bisque" . "#FFE4C4")
57 ("Black" . "#000000")
58 ("BlanchedAlmond" . "#FFEBCD")
59 ("Blue" . "#0000FF")
60 ("BlueViolet" . "#8A2BE2")
61 ("Brown" . "#A52A2A")
62 ("BurlyWood" . "#DEB887")
63 ("CadetBlue" . "#5F9EA0")
64 ("Chartreuse" . "#7FFF00")
65 ("Chocolate" . "#D2691E")
66 ("Coral" . "#FF7F50")
67 ("CornflowerBlue" . "#6495ED")
68 ("Cornsilk" . "#FFF8DC")
69 ("Crimson" . "#DC143C")
70 ("Cyan" . "#00FFFF")
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")
101 ("Gold" . "#FFD700")
102 ("GoldenRod" . "#DAA520")
103 ("Gray" . "#808080")
104 ("Grey" . "#808080")
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")
132 ("Lime" . "#00FF00")
133 ("LimeGreen" . "#32CD32")
134 ("Linen" . "#FAF0E6")
135 ("Magenta" . "#FF00FF")
136 ("Maroon" . "#800000")
137 ("MediumAquaMarine" . "#66CDAA")
138 ("MediumBlue" . "#0000CD")
139 ("MediumOrchid" . "#BA55D3")
140 ("MediumPurple" . "#9370DB")
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")
151 ("Navy" . "#000080")
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" . "#DB7093")
162 ("PapayaWhip" . "#FFEFD5")
163 ("PeachPuff" . "#FFDAB9")
164 ("Peru" . "#CD853F")
165 ("Pink" . "#FFC0CB")
166 ("Plum" . "#DDA0DD")
167 ("PowderBlue" . "#B0E0E6")
168 ("Purple" . "#800080")
169 ("RebeccaPurple" . "#663399")
170 ("Red" . "#FF0000")
171 ("RosyBrown" . "#BC8F8F")
172 ("RoyalBlue" . "#4169E1")
173 ("SaddleBrown" . "#8B4513")
174 ("Salmon" . "#FA8072")
175 ("SandyBrown" . "#F4A460")
176 ("SeaGreen" . "#2E8B57")
177 ("SeaShell" . "#FFF5EE")
178 ("Sienna" . "#A0522D")
179 ("Silver" . "#C0C0C0")
180 ("SkyBlue" . "#87CEEB")
181 ("SlateBlue" . "#6A5ACD")
182 ("SlateGray" . "#708090")
183 ("SlateGrey" . "#708090")
184 ("Snow" . "#FFFAFA")
185 ("SpringGreen" . "#00FF7F")
186 ("SteelBlue" . "#4682B4")
187 ("Tan" . "#D2B48C")
188 ("Teal" . "#008080")
189 ("Thistle" . "#D8BFD8")
190 ("Tomato" . "#FF6347")
191 ("Turquoise" . "#40E0D0")
192 ("Violet" . "#EE82EE")
193 ("Wheat" . "#F5DEB3")
194 ("White" . "#FFFFFF")
195 ("WhiteSmoke" . "#F5F5F5")
196 ("Yellow" . "#FFFF00")
197 ("YellowGreen" . "#9ACD32"))
198 "Alist of HTML colors.
199 Each entry should have the form (COLOR-NAME . HEXADECIMAL-COLOR).")
201 (defun shr-color-relative-to-absolute (number)
202 "Convert a relative NUMBER to absolute.
203 If NUMBER is absolute, return NUMBER.
204 This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\"."
205 (let ((string-length (- (length number) 1)))
206 ;; Is this a number with %?
207 (if (eq (elt number string-length) ?%)
208 (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
209 (string-to-number number))))
211 (defun shr-color-hue-to-rgb (x y h)
212 "Convert X Y H to RGB value."
213 (when (< h 0) (cl-incf h))
214 (when (> h 1) (cl-decf h))
215 (cond ((< h (/ 6.0)) (+ x (* (- y x) h 6)))
216 ((< h 0.5) y)
217 ((< h (/ 2.0 3.0)) (+ x (* (- y x) (- (/ 2.0 3.0) h) 6)))
218 (t x)))
220 (defun shr-color-hsl-to-rgb-fractions (h s l)
221 "Convert H S L to fractional RGB values."
222 (let (m1 m2)
223 (if (<= l 0.5)
224 (setq m2 (* l (+ s 1)))
225 (setq m2 (- (+ l s) (* l s))))
226 (setq m1 (- (* l 2) m2))
227 (list (shr-color-hue-to-rgb m1 m2 (+ h (/ 3.0)))
228 (shr-color-hue-to-rgb m1 m2 h)
229 (shr-color-hue-to-rgb m1 m2 (- h (/ 3.0))))))
231 (defun shr-color->hexadecimal (color)
232 "Convert any color format to hexadecimal representation.
233 Like rgb() or hsl()."
234 (when color
235 (cond
236 ;; Hexadecimal color: #abc or #aabbcc
237 ((string-match
238 "\\(#[0-9a-fA-F]\\{3\\}[0-9a-fA-F]\\{3\\}?\\)"
239 color)
240 (match-string 1 color))
241 ;; rgb() or rgba() colors
242 ((or (string-match
243 "rgb(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*)"
244 color)
245 (string-match
246 "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*)"
247 color))
248 (format "#%02X%02X%02X"
249 (shr-color-relative-to-absolute (match-string-no-properties 1 color))
250 (shr-color-relative-to-absolute (match-string-no-properties 2 color))
251 (shr-color-relative-to-absolute (match-string-no-properties 3 color))))
252 ;; hsl() or hsla() colors
253 ((or (string-match
254 "hsl(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*)"
255 color)
256 (string-match
257 "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*)"
258 color))
259 (let ((h (/ (string-to-number (match-string-no-properties 1 color)) 360.0))
260 (s (/ (string-to-number (match-string-no-properties 2 color)) 100.0))
261 (l (/ (string-to-number (match-string-no-properties 3 color)) 100.0)))
262 (pcase-let ((`(,r ,g ,b) (shr-color-hsl-to-rgb-fractions h s l)))
263 (color-rgb-to-hex r g b 2))))
264 ;; Color names
265 ((cdr (assoc-string color shr-color-html-colors-alist t)))
266 ;; Unrecognized color :(
268 nil))))
270 (defun shr-color-set-minimum-interval (val1 val2 min max interval
271 &optional fixed)
272 "Set minimum interval between VAL1 and VAL2 to INTERVAL.
273 The values are bound by MIN and MAX.
274 If FIXED is t, then VAL1 will not be touched."
275 (let ((diff (abs (- val1 val2))))
276 (unless (>= diff interval)
277 (if fixed
278 (let* ((missing (- interval diff))
279 ;; If val2 > val1, try to increase val2
280 ;; That's the "good direction"
281 (val2-good-direction
282 (if (> val2 val1)
283 (min max (+ val2 missing))
284 (max min (- val2 missing))))
285 (diff-val2-good-direction-val1 (abs (- val2-good-direction val1))))
286 (if (>= diff-val2-good-direction-val1 interval)
287 (setq val2 val2-good-direction)
288 ;; Good-direction is not so good, compute bad-direction
289 (let* ((val2-bad-direction
290 (if (> val2 val1)
291 (max min (- val1 interval))
292 (min max (+ val1 interval))))
293 (diff-val2-bad-direction-val1 (abs (- val2-bad-direction val1))))
294 (if (>= diff-val2-bad-direction-val1 interval)
295 (setq val2 val2-bad-direction)
296 ;; Still not good, pick the best and prefer good direction
297 (setq val2
298 (if (>= diff-val2-good-direction-val1 diff-val2-bad-direction-val1)
299 val2-good-direction
300 val2-bad-direction))))))
301 ;; No fixed, move val1 and val2
302 (let ((missing (/ (- interval diff) 2.0)))
303 (if (< val1 val2)
304 (setq val1 (max min (- val1 missing))
305 val2 (min max (+ val2 missing)))
306 (setq val2 (max min (- val2 missing))
307 val1 (min max (+ val1 missing))))
308 (setq diff (abs (- val1 val2))) ; Recompute diff
309 (unless (>= diff interval)
310 ;; Not ok, we hit a boundary
311 (let ((missing (- interval diff)))
312 (cond ((= val1 min)
313 (setq val2 (+ val2 missing)))
314 ((= val2 min)
315 (setq val1 (+ val1 missing)))
316 ((= val1 max)
317 (setq val2 (- val2 missing)))
318 ((= val2 max)
319 (setq val1 (- val1 missing)))))))))
320 (list val1 val2)))
322 (defun shr-color-visible (bg fg &optional fixed-background)
323 "Check that BG and FG colors are visible if they are drawn on each other.
324 Return (bg fg) if they are. If they are too similar, two new
325 colors are returned instead.
326 If FIXED-BACKGROUND is set, and if the color are not visible, a
327 new background color will not be computed. Only the foreground
328 color will be adapted to be visible on BG."
329 ;; Convert fg and bg to CIE Lab
330 (let ((fg-norm (color-name-to-rgb fg))
331 (bg-norm (color-name-to-rgb bg)))
332 (if (or (null fg-norm)
333 (null bg-norm))
334 (list bg fg)
335 (let* ((fg-lab (apply 'color-srgb-to-lab fg-norm))
336 (bg-lab (apply 'color-srgb-to-lab bg-norm))
337 ;; Compute color distance using CIE DE 2000
338 (fg-bg-distance (color-cie-de2000 fg-lab bg-lab))
339 ;; Compute luminance distance (subtract L component)
340 (luminance-distance (abs (- (car fg-lab) (car bg-lab)))))
341 (if (and (>= fg-bg-distance shr-color-visible-distance-min)
342 (>= luminance-distance shr-color-visible-luminance-min))
343 (list bg fg)
344 ;; Not visible, try to change luminance to make them visible
345 (let ((Ls (shr-color-set-minimum-interval
346 (car bg-lab) (car fg-lab) 0 100
347 shr-color-visible-luminance-min fixed-background)))
348 (unless fixed-background
349 (setcar bg-lab (car Ls)))
350 (setcar fg-lab (cadr Ls))
351 (list
352 (if fixed-background
354 (apply 'format "#%02x%02x%02x"
355 (mapcar (lambda (x) (* (max (min 1 x) 0) 255))
356 (apply 'color-lab-to-srgb bg-lab))))
357 (apply 'format "#%02x%02x%02x"
358 (mapcar (lambda (x) (* (max (min 1 x) 0) 255))
359 (apply 'color-lab-to-srgb fg-lab))))))))))
361 (provide 'shr-color)
363 ;;; shr-color.el ends here