| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ Copyright (C) 2002-2004 TechGame Networks, LLC. |
|---|
| 4 |
##~ |
|---|
| 5 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 6 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 7 |
##~ LICENSE file included with this distribution. |
|---|
| 8 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 9 |
|
|---|
| 10 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 11 |
#~ Imports |
|---|
| 12 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 13 |
|
|---|
| 14 |
import re |
|---|
| 15 |
import colorsys |
|---|
| 16 |
|
|---|
| 17 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 18 |
#~ Constants / Variables / Etc. |
|---|
| 19 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 20 |
|
|---|
| 21 |
rgbColorTable = { |
|---|
| 22 |
'none': None, |
|---|
| 23 |
'aliceblue': 0xf0f8ff, |
|---|
| 24 |
'antiquewhite': 0xfaebd7, |
|---|
| 25 |
'aqua': 0x00ffff, |
|---|
| 26 |
'aquamarine': 0x7fffd4, |
|---|
| 27 |
'azure': 0xf0ffff, |
|---|
| 28 |
'beige': 0xf5f5dc, |
|---|
| 29 |
'bisque': 0xffe4c4, |
|---|
| 30 |
'black': 0x000000, |
|---|
| 31 |
'blanchedalmond': 0xffebcd, |
|---|
| 32 |
'blue': 0x0000ff, |
|---|
| 33 |
'blueviolet': 0x8a2be2, |
|---|
| 34 |
'brown': 0xa52a2a, |
|---|
| 35 |
'burlywood': 0xdeb887, |
|---|
| 36 |
'cadetblue': 0x5f9ea0, |
|---|
| 37 |
'chartreuse': 0x7fff00, |
|---|
| 38 |
'chocolate': 0xd2691e, |
|---|
| 39 |
'coral': 0xff7f50, |
|---|
| 40 |
'cornflowerblue': 0x6495ed, |
|---|
| 41 |
'cornsilk': 0xfff8dc, |
|---|
| 42 |
'crimson': 0xdc143c, |
|---|
| 43 |
'cyan': 0x00ffff, |
|---|
| 44 |
'darkblue': 0x00008b, |
|---|
| 45 |
'darkcyan': 0x008b8b, |
|---|
| 46 |
'darkgoldenrod': 0xb8860b, |
|---|
| 47 |
'darkgray': 0xa9a9a9, |
|---|
| 48 |
'darkgreen': 0x006400, |
|---|
| 49 |
'darkgrey': 0xa9a9a9, |
|---|
| 50 |
'darkkhaki': 0xbdb76b, |
|---|
| 51 |
'darkmagenta': 0x8b008b, |
|---|
| 52 |
'darkolivegreen': 0x556b2f, |
|---|
| 53 |
'darkorange': 0xff8c00, |
|---|
| 54 |
'darkorchid': 0x9932cc, |
|---|
| 55 |
'darkred': 0x8b0000, |
|---|
| 56 |
'darksalmon': 0xe9967a, |
|---|
| 57 |
'darkseagreen': 0x8fbc8f, |
|---|
| 58 |
'darkslateblue': 0x483d8b, |
|---|
| 59 |
'darkslategray': 0x2f4f4f, |
|---|
| 60 |
'darkslategrey': 0x2f4f4f, |
|---|
| 61 |
'darkturquoise': 0x00ced1, |
|---|
| 62 |
'darkviolet': 0x9400d3, |
|---|
| 63 |
'deeppink': 0xff1493, |
|---|
| 64 |
'deepskyblue': 0x00bfff, |
|---|
| 65 |
'dimgray': 0x696969, |
|---|
| 66 |
'dimgrey': 0x696969, |
|---|
| 67 |
'dodgerblue': 0x1e90ff, |
|---|
| 68 |
'firebrick': 0xb22222, |
|---|
| 69 |
'floralwhite': 0xfffaf0, |
|---|
| 70 |
'forestgreen': 0x228b22, |
|---|
| 71 |
'fuchsia': 0xff00ff, |
|---|
| 72 |
'gainsboro': 0xdcdcdc, |
|---|
| 73 |
'ghostwhite': 0xf8f8ff, |
|---|
| 74 |
'gold': 0xffd700, |
|---|
| 75 |
'goldenrod': 0xdaa520, |
|---|
| 76 |
'gray': 0x808080, |
|---|
| 77 |
'green': 0x008000, |
|---|
| 78 |
'greenyellow': 0xadff2f, |
|---|
| 79 |
'grey': 0x808080, |
|---|
| 80 |
'honeydew': 0xf0fff0, |
|---|
| 81 |
'hotpink': 0xff69b4, |
|---|
| 82 |
'indianred': 0xcd5c5c, |
|---|
| 83 |
'indigo': 0x4b0082, |
|---|
| 84 |
'ivory': 0xfffff0, |
|---|
| 85 |
'khaki': 0xf0e68c, |
|---|
| 86 |
'lavender': 0xe6e6fa, |
|---|
| 87 |
'lavenderblush': 0xfff0f5, |
|---|
| 88 |
'lawngreen': 0x7cfc00, |
|---|
| 89 |
'lemonchiffon': 0xfffacd, |
|---|
| 90 |
'lightblue': 0xadd8e6, |
|---|
| 91 |
'lightcoral': 0xf08080, |
|---|
| 92 |
'lightcyan': 0xe0ffff, |
|---|
| 93 |
'lightgoldenrodyellow': 0xfafad2, |
|---|
| 94 |
'lightgray': 0xd3d3d3, |
|---|
| 95 |
'lightgreen': 0x90ee90, |
|---|
| 96 |
'lightgrey': 0xd3d3d3, |
|---|
| 97 |
'lightpink': 0xffb6c1, |
|---|
| 98 |
'lightsalmon': 0xffa07a, |
|---|
| 99 |
'lightseagreen': 0x20b2aa, |
|---|
| 100 |
'lightskyblue': 0x87cefa, |
|---|
| 101 |
'lightslategray': 0x778899, |
|---|
| 102 |
'lightslategrey': 0x778899, |
|---|
| 103 |
'lightsteelblue': 0xb0c4de, |
|---|
| 104 |
'lightyellow': 0xffffe0, |
|---|
| 105 |
'lime': 0x00ff00, |
|---|
| 106 |
'limegreen': 0x32cd32, |
|---|
| 107 |
'linen': 0xfaf0e6, |
|---|
| 108 |
'magenta': 0xff00ff, |
|---|
| 109 |
'maroon': 0x800000, |
|---|
| 110 |
'mediumaquamarine': 0x66cdaa, |
|---|
| 111 |
'mediumblue': 0x0000cd, |
|---|
| 112 |
'mediumorchid': 0xba55d3, |
|---|
| 113 |
'mediumpurple': 0x9370db, |
|---|
| 114 |
'mediumseagreen': 0x3cb371, |
|---|
| 115 |
'mediumslateblue': 0x7b68ee, |
|---|
| 116 |
'mediumspringgreen': 0x00fa9a, |
|---|
| 117 |
'mediumturquoise': 0x48d1cc, |
|---|
| 118 |
'mediumvioletred': 0xc71585, |
|---|
| 119 |
'midnightblue': 0x191970, |
|---|
| 120 |
'mintcream': 0xf5fffa, |
|---|
| 121 |
'mistyrose': 0xffe4e1, |
|---|
| 122 |
'moccasin': 0xffe4b5, |
|---|
| 123 |
'navajowhite': 0xffdead, |
|---|
| 124 |
'navy': 0x000080, |
|---|
| 125 |
'oldlace': 0xfdf5e6, |
|---|
| 126 |
'olive': 0x808000, |
|---|
| 127 |
'olivedrab': 0x6b8e23, |
|---|
| 128 |
'orange': 0xffa500, |
|---|
| 129 |
'orangered': 0xff4500, |
|---|
| 130 |
'orchid': 0xda70d6, |
|---|
| 131 |
'palegoldenrod': 0xeee8aa, |
|---|
| 132 |
'palegreen': 0x98fb98, |
|---|
| 133 |
'paleturquoise': 0xafeeee, |
|---|
| 134 |
'palevioletred': 0xdb7093, |
|---|
| 135 |
'papayawhip': 0xffefd5, |
|---|
| 136 |
'peachpuff': 0xffdab9, |
|---|
| 137 |
'peru': 0xcd853f, |
|---|
| 138 |
'pink': 0xffc0cb, |
|---|
| 139 |
'plum': 0xdda0dd, |
|---|
| 140 |
'powderblue': 0xb0e0e6, |
|---|
| 141 |
'purple': 0x800080, |
|---|
| 142 |
'red': 0xff0000, |
|---|
| 143 |
'rosybrown': 0xbc8f8f, |
|---|
| 144 |
'royalblue': 0x4169e1, |
|---|
| 145 |
'saddlebrown': 0x8b4513, |
|---|
| 146 |
'salmon': 0xfa8072, |
|---|
| 147 |
'sandybrown': 0xf4a460, |
|---|
| 148 |
'seagreen': 0x2e8b57, |
|---|
| 149 |
'seashell': 0xfff5ee, |
|---|
| 150 |
'sienna': 0xa0522d, |
|---|
| 151 |
'silver': 0xc0c0c0, |
|---|
| 152 |
'skyblue': 0x87ceeb, |
|---|
| 153 |
'slateblue': 0x6a5acd, |
|---|
| 154 |
'slategray': 0x708090, |
|---|
| 155 |
'slategrey': 0x708090, |
|---|
| 156 |
'snow': 0xfffafa, |
|---|
| 157 |
'springgreen': 0x00ff7f, |
|---|
| 158 |
'steelblue': 0x4682b4, |
|---|
| 159 |
'tan': 0xd2b48c, |
|---|
| 160 |
'teal': 0x008080, |
|---|
| 161 |
'thistle': 0xd8bfd8, |
|---|
| 162 |
'tomato': 0xff6347, |
|---|
| 163 |
'turquoise': 0x40e0d0, |
|---|
| 164 |
'violet': 0xee82ee, |
|---|
| 165 |
'wheat': 0xf5deb3, |
|---|
| 166 |
'white': 0xffffff, |
|---|
| 167 |
'whitesmoke': 0xf5f5f5, |
|---|
| 168 |
'yellow': 0xffff00, |
|---|
| 169 |
'yellowgreen': 0x9acd32 |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
_recolorhex6 = re.compile('#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})') |
|---|
| 173 |
_recolorhex3 = re.compile('#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])') |
|---|
| 174 |
_recolorfn = re.compile('([a-zA-Z]+)\(([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,{0,1}\s*\)\s*;?\s*') |
|---|
| 175 |
|
|---|
| 176 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 177 |
#~ Definitions |
|---|
| 178 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 179 |
|
|---|
| 180 |
class ColorFns(object): |
|---|
| 181 |
def rgb(r, g, b): |
|---|
| 182 |
if 0<r<1: r = r*255 |
|---|
| 183 |
if 0<g<1: g = g*255 |
|---|
| 184 |
if 0<b<1: b = b*255 |
|---|
| 185 |
return tuple([int(v) for v in (r, g, b)]) |
|---|
| 186 |
rgb = staticmethod(rgb) |
|---|
| 187 |
|
|---|
| 188 |
def hsv(h, s, v): |
|---|
| 189 |
return tuple([int(255*v) for v in colorsys.hsv_to_rgb(h, s, v)]) |
|---|
| 190 |
hsv = staticmethod(hsv) |
|---|
| 191 |
|
|---|
| 192 |
def hls(h, l, s): |
|---|
| 193 |
return tuple([int(255*v) for v in colorsys.hls_to_rgb(h, l, s)]) |
|---|
| 194 |
hls = staticmethod(hls) |
|---|
| 195 |
|
|---|
| 196 |
def yiq(y, i, q): |
|---|
| 197 |
return tuple([int(255*v) for v in colorsys.yiq_to_rgb(y,i,q)]) |
|---|
| 198 |
yiq = staticmethod(yiq) |
|---|
| 199 |
|
|---|
| 200 |
def rgbToHexColor(r, g, b): |
|---|
| 201 |
return "#%2x%2x%2x" % (r, g, b) |
|---|
| 202 |
|
|---|
| 203 |
def intToRGB_byte(value): |
|---|
| 204 |
if value is None: |
|---|
| 205 |
return None |
|---|
| 206 |
return (value >> 16) & 0xff, (value >> 8) & 0xff, (value >> 0) & 0xff, |
|---|
| 207 |
|
|---|
| 208 |
def colorToRGB_float(colorstr): |
|---|
| 209 |
return tuple([v/255.0 for v in colorToRGB_byte(colorstr)]) |
|---|
| 210 |
|
|---|
| 211 |
def colorToRGB_byte(colorstr): |
|---|
| 212 |
""" |
|---|
| 213 |
Valid color strings: |
|---|
| 214 |
"none" |
|---|
| 215 |
"black" # color string constant lookup |
|---|
| 216 |
"#ff0088" |
|---|
| 217 |
"#f08" # expanded to #ff0088 |
|---|
| 218 |
"rgb(255, 0, 136)" |
|---|
| 219 |
"rgb(1.0, 0.0, 0.53125)" |
|---|
| 220 |
"hsv(0.25, 0.5, 0.4)" |
|---|
| 221 |
""" |
|---|
| 222 |
try: |
|---|
| 223 |
return intToRGB_byte(rgbColorTable[colorstr.lower()]) |
|---|
| 224 |
except KeyError: |
|---|
| 225 |
pass # not a color name |
|---|
| 226 |
|
|---|
| 227 |
try: |
|---|
| 228 |
bytes = _recolorhex6.split(colorstr)[1:-1] |
|---|
| 229 |
if bytes: |
|---|
| 230 |
return tuple([int(x, 16) for x in bytes]) |
|---|
| 231 |
except ValueError: |
|---|
| 232 |
pass # Unpack tuple of wrong size |
|---|
| 233 |
|
|---|
| 234 |
try: |
|---|
| 235 |
bytes = _recolorhex3.split(colorstr)[1:-1] |
|---|
| 236 |
if bytes: |
|---|
| 237 |
return tuple([0x11 * int(x, 16) for x in bytes]) |
|---|
| 238 |
except ValueError: |
|---|
| 239 |
pass # Unpack tuple of wrong size |
|---|
| 240 |
|
|---|
| 241 |
try: |
|---|
| 242 |
colorFnAndArgs = _recolorfn.split(colorstr)[1:-1] |
|---|
| 243 |
except ValueError: |
|---|
| 244 |
pass # Unpack tuple of wrong size |
|---|
| 245 |
else: |
|---|
| 246 |
if colorFnAndArgs: |
|---|
| 247 |
fnName, fnArgs = colorFnAndArgs[0], colorFnAndArgs[1:] |
|---|
| 248 |
fnArgs = map(float, fnArgs) |
|---|
| 249 |
fn = getattr(ColorFns, fnName) |
|---|
| 250 |
return tuple(fn(*fnArgs)) |
|---|
| 251 |
|
|---|
| 252 |
raise ValueError("Could not interpret color value %r" % colorstr) |
|---|
| 253 |
|
|---|
| 254 |
# alias |
|---|
| 255 |
colorToRGB = colorToRGB_byte |
|---|
| 256 |
|
|---|