/PIL/ImageColor.py

https://bitbucket.org/jasonrglasgow/gdata · Python · 263 lines · 223 code · 6 blank · 34 comment · 17 complexity · 943f258f843731d3276e0aa8d8f5c0e2 MD5 · raw file

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