shr-color.el 13 KB

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