/p23_lib/pil/ImageColor.py

https://github.com/chu888chu888/Python-web.py-ibrouteur · Python · 244 lines · 210 code · 6 blank · 28 comment · 14 complexity · f08ee1cd6f765623ec1c38e506d9fe89 MD5 · raw file

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