Back to index

python3.2  3.2.2
Classes | Functions | Variables
pynche.ColorDB Namespace Reference

Classes

class  BadColor
class  ColorDB
class  RGBColorDB
class  HTML40DB
class  LightlinkDB
class  WebsafeDB

Functions

def get_colordb
def rrggbb_to_triplet
def triplet_to_rrggbb
def triplet_to_fractional_rgb
def triplet_to_brightness

Variables

 DEFAULT_DB = None
string SPACE = ' '
string COMMASPACE = ', '
list FILETYPES
dictionary _namedict = {}
dictionary _tripdict = {}
tuple _maxtuple = (256.0,)
tuple colordb = get_colordb('/usr/openwin/lib/rgb.txt')
string target = 'navy'
tuple t0 = time.time()
tuple nearest = colordb.nearest(r, g, b)
tuple t1 = time.time()
tuple aliases = colordb.aliases_of(r, g, b)

Detailed Description

Color Database.

This file contains one class, called ColorDB, and several utility functions.
The class must be instantiated by the get_colordb() function in this file,
passing it a filename to read a database out of.

The get_colordb() function will try to examine the file to figure out what the
format of the file is.  If it can't figure out the file format, or it has
trouble reading the file, None is returned.  You can pass get_colordb() an
optional filetype argument.

Supporte file types are:

    X_RGB_TXT -- X Consortium rgb.txt format files.  Three columns of numbers
         from 0 .. 255 separated by whitespace.  Arbitrary trailing
         columns used as the color name.

The utility functions are useful for converting between the various expected
color formats, and for calculating other color values.

Function Documentation

def pynche.ColorDB.get_colordb (   file,
  filetype = None 
)

Definition at line 176 of file ColorDB.py.

00176 
00177 def get_colordb(file, filetype=None):
00178     colordb = None
00179     fp = open(file)
00180     try:
00181         line = fp.readline()
00182         if not line:
00183             return None
00184         # try to determine the type of RGB file it is
00185         if filetype is None:
00186             filetypes = FILETYPES
00187         else:
00188             filetypes = [filetype]
00189         for typere, class_ in filetypes:
00190             mo = typere.search(line)
00191             if mo:
00192                 break
00193         else:
00194             # no matching type
00195             return None
00196         # we know the type and the class to grok the type, so suck it in
00197         colordb = class_(fp)
00198     finally:
00199         fp.close()
00200     # save a global copy
00201     global DEFAULT_DB
00202     DEFAULT_DB = colordb
00203     return colordb
00204 
00205 

Converts a #rrggbb color to the tuple (red, green, blue).

Definition at line 208 of file ColorDB.py.

00208 
00209 def rrggbb_to_triplet(color):
00210     """Converts a #rrggbb color to the tuple (red, green, blue)."""
00211     rgbtuple = _namedict.get(color)
00212     if rgbtuple is None:
00213         if color[0] != '#':
00214             raise BadColor(color)
00215         red = color[1:3]
00216         green = color[3:5]
00217         blue = color[5:7]
00218         rgbtuple = int(red, 16), int(green, 16), int(blue, 16)
00219         _namedict[color] = rgbtuple
00220     return rgbtuple
00221 

Here is the caller graph for this function:

Definition at line 238 of file ColorDB.py.

00238 
00239 def triplet_to_brightness(rgbtuple):
00240     # return the brightness (grey level) along the scale 0.0==black to
00241     # 1.0==white
00242     r = 0.299
00243     g = 0.587
00244     b = 0.114
00245     return r*rgbtuple[0] + g*rgbtuple[1] + b*rgbtuple[2]
00246 
00247 


Definition at line 234 of file ColorDB.py.

00234 
00235 def triplet_to_fractional_rgb(rgbtuple):
00236     return list(map(operator.__div__, rgbtuple, _maxtuple))
00237 

Converts a (red, green, blue) tuple to #rrggbb.

Definition at line 223 of file ColorDB.py.

00223 
00224 def triplet_to_rrggbb(rgbtuple):
00225     """Converts a (red, green, blue) tuple to #rrggbb."""
00226     global _tripdict
00227     hexname = _tripdict.get(rgbtuple)
00228     if hexname is None:
00229         hexname = '#%02x%02x%02x' % rgbtuple
00230         _tripdict[rgbtuple] = hexname
00231     return hexname
00232 


Variable Documentation

tuple pynche.ColorDB._maxtuple = (256.0,)

Definition at line 233 of file ColorDB.py.

Definition at line 206 of file ColorDB.py.

Definition at line 222 of file ColorDB.py.

tuple pynche.ColorDB.aliases = colordb.aliases_of(r, g, b)

Definition at line 271 of file ColorDB.py.

tuple pynche.ColorDB.colordb = get_colordb('/usr/openwin/lib/rgb.txt')

Definition at line 249 of file ColorDB.py.

Definition at line 33 of file ColorDB.py.

Definition at line 31 of file ColorDB.py.

Initial value:
00001 [
00002     (re.compile('Xorg'), RGBColorDB),
00003     (re.compile('XConsortium'), RGBColorDB),
00004     (re.compile('HTML'), HTML40DB),
00005     (re.compile('lightlink'), LightlinkDB),
00006     (re.compile('Websafe'), WebsafeDB),
00007     ]

Definition at line 168 of file ColorDB.py.

tuple pynche.ColorDB.nearest = colordb.nearest(r, g, b)

Definition at line 265 of file ColorDB.py.

Definition at line 32 of file ColorDB.py.

tuple pynche.ColorDB.t0 = time.time()

Definition at line 264 of file ColorDB.py.

tuple pynche.ColorDB.t1 = time.time()

Definition at line 266 of file ColorDB.py.

Definition at line 254 of file ColorDB.py.