| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ License |
|---|
| 4 |
##~ |
|---|
| 5 |
##- The RuneBlade Foundation library is intended to ease some |
|---|
| 6 |
##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.) |
|---|
| 7 |
##- applications, while providing the flexibility to modularly change the |
|---|
| 8 |
##- architecture. Enjoy. |
|---|
| 9 |
##~ |
|---|
| 10 |
##~ Copyright (C) 2002 TechGame Networks, LLC. |
|---|
| 11 |
##~ |
|---|
| 12 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 13 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 14 |
##~ LICENSE file included with this distribution. |
|---|
| 15 |
##~ |
|---|
| 16 |
##~ TechGame Networks, LLC can be reached at: |
|---|
| 17 |
##~ 3578 E. Hartsel Drive #211 |
|---|
| 18 |
##~ Colorado Springs, Colorado, USA, 80920 |
|---|
| 19 |
##~ |
|---|
| 20 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 23 |
#~ Imports |
|---|
| 24 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 25 |
|
|---|
| 26 |
import re |
|---|
| 27 |
|
|---|
| 28 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 29 |
#~ Constants / Variables / Etc. |
|---|
| 30 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 31 |
|
|---|
| 32 |
colorlookuptable = { |
|---|
| 33 |
'none': None, |
|---|
| 34 |
|
|---|
| 35 |
'black': (0x00, 0x00, 0x00), 'gray': (0x80, 0x80, 0x80), |
|---|
| 36 |
'silver': (0xc0, 0xc0, 0xc0), 'white': (0xff, 0xff, 0xff), |
|---|
| 37 |
|
|---|
| 38 |
'blue': (0x00, 0x00, 0xff), 'lime': (0x00, 0xff, 0x00), 'red': (0xff, 0x00, 0x00), |
|---|
| 39 |
'navy': (0x00, 0x00, 0x80), 'green': (0x00, 0x80, 0x00), 'maroon': (0x80, 0x00, 0x00), |
|---|
| 40 |
|
|---|
| 41 |
'teal': (0x00, 0x80, 0x80), 'aqua': (0x00, 0xff, 0xff), |
|---|
| 42 |
'purple': (0x80, 0x00, 0x80), 'fuchsia': (0xff, 0x00, 0xff), |
|---|
| 43 |
'olive': (0x80, 0x80, 0x00), 'yellow': (0xff, 0xff, 0x00), |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
import re |
|---|
| 47 |
recolorhex6 = re.compile('#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})') |
|---|
| 48 |
recolorhex3 = re.compile('#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])') |
|---|
| 49 |
recolorfn = re.compile('rgb\(([0-9.]+),\s*([0-9.]+),\s*([0-9.]+),{0,1}\s*\)') |
|---|
| 50 |
|
|---|
| 51 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 52 |
#~ Definitions |
|---|
| 53 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 54 |
|
|---|
| 55 |
def RGBToColorString(r, g, b): |
|---|
| 56 |
return "#%2x%2x%2x" % (r, g, b) |
|---|
| 57 |
|
|---|
| 58 |
def ColorStringToRGB(colorstr): |
|---|
| 59 |
""" |
|---|
| 60 |
Valid color strings: |
|---|
| 61 |
"none" |
|---|
| 62 |
"black" # color string constant lookup |
|---|
| 63 |
"#ff0088" |
|---|
| 64 |
"#f08" # expanded to #ff0088 |
|---|
| 65 |
"rgb(255, 0, 136)" |
|---|
| 66 |
"rgb(1.0, 0.0, 0.53125)" |
|---|
| 67 |
""" |
|---|
| 68 |
try: return colorlookuptable[colorstr.lower()] |
|---|
| 69 |
except KeyError: pass |
|---|
| 70 |
|
|---|
| 71 |
try: r, g, b = [int(x, 0x10) for x in recolorhex6.split(colorstr)[1:-1]] |
|---|
| 72 |
except ValueError: pass # Unpack tuple of wrong size |
|---|
| 73 |
else: return r, g, b |
|---|
| 74 |
|
|---|
| 75 |
try: r, g, b = [int(x, 0x10) for x in recolorhex3.split(colorstr)[1:-1]] |
|---|
| 76 |
except ValueError: pass # Unpack tuple of wrong size |
|---|
| 77 |
else: return r*0x11, g*0x11, b*0x11 |
|---|
| 78 |
|
|---|
| 79 |
try: r, g, b = [float(x) for x in recolorfn.split(colorstr)[1:-1]] |
|---|
| 80 |
except ValueError: pass # Unpack tuple of wrong size |
|---|
| 81 |
else: |
|---|
| 82 |
if 0<r<1 or 0<g<1 or 0<b<1: |
|---|
| 83 |
return r*255, g*255, b*255 |
|---|
| 84 |
else: |
|---|
| 85 |
return r, g, b |
|---|
| 86 |
|
|---|
| 87 |
raise ValueError, "Could not interpret color value %r" % (colorstr, ) |
|---|
| 88 |
|
|---|