ImageColor.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # map CSS3-style colour description strings to RGB
  6. #
  7. # History:
  8. # 2002-10-24 fl Added support for CSS-style color strings
  9. # 2002-12-15 fl Added RGBA support
  10. # 2004-03-27 fl Fixed remaining int() problems for Python 1.5.2
  11. # 2004-07-19 fl Fixed gray/grey spelling issues
  12. # 2009-03-05 fl Fixed rounding error in grayscale calculation
  13. #
  14. # Copyright (c) 2002-2004 by Secret Labs AB
  15. # Copyright (c) 2002-2004 by Fredrik Lundh
  16. #
  17. # See the README file for information on usage and redistribution.
  18. #
  19. import re
  20. from . import Image
  21. def getrgb(color):
  22. """
  23. Convert a color string to an RGB tuple. If the string cannot be parsed,
  24. this function raises a :py:exc:`ValueError` exception.
  25. .. versionadded:: 1.1.4
  26. :param color: A color string
  27. :return: ``(red, green, blue[, alpha])``
  28. """
  29. color = color.lower()
  30. rgb = colormap.get(color, None)
  31. if rgb:
  32. if isinstance(rgb, tuple):
  33. return rgb
  34. colormap[color] = rgb = getrgb(rgb)
  35. return rgb
  36. # check for known string formats
  37. if re.match("#[a-f0-9]{3}$", color):
  38. return (int(color[1] * 2, 16), int(color[2] * 2, 16), int(color[3] * 2, 16))
  39. if re.match("#[a-f0-9]{4}$", color):
  40. return (
  41. int(color[1] * 2, 16),
  42. int(color[2] * 2, 16),
  43. int(color[3] * 2, 16),
  44. int(color[4] * 2, 16),
  45. )
  46. if re.match("#[a-f0-9]{6}$", color):
  47. return (int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16))
  48. if re.match("#[a-f0-9]{8}$", color):
  49. return (
  50. int(color[1:3], 16),
  51. int(color[3:5], 16),
  52. int(color[5:7], 16),
  53. int(color[7:9], 16),
  54. )
  55. m = re.match(r"rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$", color)
  56. if m:
  57. return (int(m.group(1)), int(m.group(2)), int(m.group(3)))
  58. m = re.match(r"rgb\(\s*(\d+)%\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)$", color)
  59. if m:
  60. return (
  61. int((int(m.group(1)) * 255) / 100.0 + 0.5),
  62. int((int(m.group(2)) * 255) / 100.0 + 0.5),
  63. int((int(m.group(3)) * 255) / 100.0 + 0.5),
  64. )
  65. m = re.match(
  66. r"hsl\(\s*(\d+\.?\d*)\s*,\s*(\d+\.?\d*)%\s*,\s*(\d+\.?\d*)%\s*\)$", color
  67. )
  68. if m:
  69. from colorsys import hls_to_rgb
  70. rgb = hls_to_rgb(
  71. float(m.group(1)) / 360.0,
  72. float(m.group(3)) / 100.0,
  73. float(m.group(2)) / 100.0,
  74. )
  75. return (
  76. int(rgb[0] * 255 + 0.5),
  77. int(rgb[1] * 255 + 0.5),
  78. int(rgb[2] * 255 + 0.5),
  79. )
  80. m = re.match(
  81. r"hs[bv]\(\s*(\d+\.?\d*)\s*,\s*(\d+\.?\d*)%\s*,\s*(\d+\.?\d*)%\s*\)$", color
  82. )
  83. if m:
  84. from colorsys import hsv_to_rgb
  85. rgb = hsv_to_rgb(
  86. float(m.group(1)) / 360.0,
  87. float(m.group(2)) / 100.0,
  88. float(m.group(3)) / 100.0,
  89. )
  90. return (
  91. int(rgb[0] * 255 + 0.5),
  92. int(rgb[1] * 255 + 0.5),
  93. int(rgb[2] * 255 + 0.5),
  94. )
  95. m = re.match(r"rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$", color)
  96. if m:
  97. return (int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4)))
  98. raise ValueError(f"unknown color specifier: {repr(color)}")
  99. def getcolor(color, mode):
  100. """
  101. Same as :py:func:`~PIL.ImageColor.getrgb`, but converts the RGB value to a
  102. greyscale value if the mode is not color or a palette image. If the string
  103. cannot be parsed, this function raises a :py:exc:`ValueError` exception.
  104. .. versionadded:: 1.1.4
  105. :param color: A color string
  106. :return: ``(graylevel [, alpha]) or (red, green, blue[, alpha])``
  107. """
  108. # same as getrgb, but converts the result to the given mode
  109. color, alpha = getrgb(color), 255
  110. if len(color) == 4:
  111. color, alpha = color[0:3], color[3]
  112. if Image.getmodebase(mode) == "L":
  113. r, g, b = color
  114. # ITU-R Recommendation 601-2 for nonlinear RGB
  115. # scaled to 24 bits to match the convert's implementation.
  116. color = (r * 19595 + g * 38470 + b * 7471 + 0x8000) >> 16
  117. if mode[-1] == "A":
  118. return (color, alpha)
  119. else:
  120. if mode[-1] == "A":
  121. return color + (alpha,)
  122. return color
  123. colormap = {
  124. # X11 colour table from https://drafts.csswg.org/css-color-4/, with
  125. # gray/grey spelling issues fixed. This is a superset of HTML 4.0
  126. # colour names used in CSS 1.
  127. "aliceblue": "#f0f8ff",
  128. "antiquewhite": "#faebd7",
  129. "aqua": "#00ffff",
  130. "aquamarine": "#7fffd4",
  131. "azure": "#f0ffff",
  132. "beige": "#f5f5dc",
  133. "bisque": "#ffe4c4",
  134. "black": "#000000",
  135. "blanchedalmond": "#ffebcd",
  136. "blue": "#0000ff",
  137. "blueviolet": "#8a2be2",
  138. "brown": "#a52a2a",
  139. "burlywood": "#deb887",
  140. "cadetblue": "#5f9ea0",
  141. "chartreuse": "#7fff00",
  142. "chocolate": "#d2691e",
  143. "coral": "#ff7f50",
  144. "cornflowerblue": "#6495ed",
  145. "cornsilk": "#fff8dc",
  146. "crimson": "#dc143c",
  147. "cyan": "#00ffff",
  148. "darkblue": "#00008b",
  149. "darkcyan": "#008b8b",
  150. "darkgoldenrod": "#b8860b",
  151. "darkgray": "#a9a9a9",
  152. "darkgrey": "#a9a9a9",
  153. "darkgreen": "#006400",
  154. "darkkhaki": "#bdb76b",
  155. "darkmagenta": "#8b008b",
  156. "darkolivegreen": "#556b2f",
  157. "darkorange": "#ff8c00",
  158. "darkorchid": "#9932cc",
  159. "darkred": "#8b0000",
  160. "darksalmon": "#e9967a",
  161. "darkseagreen": "#8fbc8f",
  162. "darkslateblue": "#483d8b",
  163. "darkslategray": "#2f4f4f",
  164. "darkslategrey": "#2f4f4f",
  165. "darkturquoise": "#00ced1",
  166. "darkviolet": "#9400d3",
  167. "deeppink": "#ff1493",
  168. "deepskyblue": "#00bfff",
  169. "dimgray": "#696969",
  170. "dimgrey": "#696969",
  171. "dodgerblue": "#1e90ff",
  172. "firebrick": "#b22222",
  173. "floralwhite": "#fffaf0",
  174. "forestgreen": "#228b22",
  175. "fuchsia": "#ff00ff",
  176. "gainsboro": "#dcdcdc",
  177. "ghostwhite": "#f8f8ff",
  178. "gold": "#ffd700",
  179. "goldenrod": "#daa520",
  180. "gray": "#808080",
  181. "grey": "#808080",
  182. "green": "#008000",
  183. "greenyellow": "#adff2f",
  184. "honeydew": "#f0fff0",
  185. "hotpink": "#ff69b4",
  186. "indianred": "#cd5c5c",
  187. "indigo": "#4b0082",
  188. "ivory": "#fffff0",
  189. "khaki": "#f0e68c",
  190. "lavender": "#e6e6fa",
  191. "lavenderblush": "#fff0f5",
  192. "lawngreen": "#7cfc00",
  193. "lemonchiffon": "#fffacd",
  194. "lightblue": "#add8e6",
  195. "lightcoral": "#f08080",
  196. "lightcyan": "#e0ffff",
  197. "lightgoldenrodyellow": "#fafad2",
  198. "lightgreen": "#90ee90",
  199. "lightgray": "#d3d3d3",
  200. "lightgrey": "#d3d3d3",
  201. "lightpink": "#ffb6c1",
  202. "lightsalmon": "#ffa07a",
  203. "lightseagreen": "#20b2aa",
  204. "lightskyblue": "#87cefa",
  205. "lightslategray": "#778899",
  206. "lightslategrey": "#778899",
  207. "lightsteelblue": "#b0c4de",
  208. "lightyellow": "#ffffe0",
  209. "lime": "#00ff00",
  210. "limegreen": "#32cd32",
  211. "linen": "#faf0e6",
  212. "magenta": "#ff00ff",
  213. "maroon": "#800000",
  214. "mediumaquamarine": "#66cdaa",
  215. "mediumblue": "#0000cd",
  216. "mediumorchid": "#ba55d3",
  217. "mediumpurple": "#9370db",
  218. "mediumseagreen": "#3cb371",
  219. "mediumslateblue": "#7b68ee",
  220. "mediumspringgreen": "#00fa9a",
  221. "mediumturquoise": "#48d1cc",
  222. "mediumvioletred": "#c71585",
  223. "midnightblue": "#191970",
  224. "mintcream": "#f5fffa",
  225. "mistyrose": "#ffe4e1",
  226. "moccasin": "#ffe4b5",
  227. "navajowhite": "#ffdead",
  228. "navy": "#000080",
  229. "oldlace": "#fdf5e6",
  230. "olive": "#808000",
  231. "olivedrab": "#6b8e23",
  232. "orange": "#ffa500",
  233. "orangered": "#ff4500",
  234. "orchid": "#da70d6",
  235. "palegoldenrod": "#eee8aa",
  236. "palegreen": "#98fb98",
  237. "paleturquoise": "#afeeee",
  238. "palevioletred": "#db7093",
  239. "papayawhip": "#ffefd5",
  240. "peachpuff": "#ffdab9",
  241. "peru": "#cd853f",
  242. "pink": "#ffc0cb",
  243. "plum": "#dda0dd",
  244. "powderblue": "#b0e0e6",
  245. "purple": "#800080",
  246. "rebeccapurple": "#663399",
  247. "red": "#ff0000",
  248. "rosybrown": "#bc8f8f",
  249. "royalblue": "#4169e1",
  250. "saddlebrown": "#8b4513",
  251. "salmon": "#fa8072",
  252. "sandybrown": "#f4a460",
  253. "seagreen": "#2e8b57",
  254. "seashell": "#fff5ee",
  255. "sienna": "#a0522d",
  256. "silver": "#c0c0c0",
  257. "skyblue": "#87ceeb",
  258. "slateblue": "#6a5acd",
  259. "slategray": "#708090",
  260. "slategrey": "#708090",
  261. "snow": "#fffafa",
  262. "springgreen": "#00ff7f",
  263. "steelblue": "#4682b4",
  264. "tan": "#d2b48c",
  265. "teal": "#008080",
  266. "thistle": "#d8bfd8",
  267. "tomato": "#ff6347",
  268. "turquoise": "#40e0d0",
  269. "violet": "#ee82ee",
  270. "wheat": "#f5deb3",
  271. "white": "#ffffff",
  272. "whitesmoke": "#f5f5f5",
  273. "yellow": "#ffff00",
  274. "yellowgreen": "#9acd32",
  275. }