ImageColor.py 9.2 KB

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