Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions | Private Attributes
pynche.ColorDB.ColorDB Class Reference
Inheritance diagram for pynche.ColorDB.ColorDB:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def filename
def find_byrgb
def find_byname
def nearest
def unique_names
def aliases_of

Private Member Functions

def _extractrgb
def _extractname

Private Attributes

 __name
 __byrgb
 __byname
 __allnames

Detailed Description

Definition at line 38 of file ColorDB.py.


Constructor & Destructor Documentation

def pynche.ColorDB.ColorDB.__init__ (   self,
  fp 
)

Definition at line 39 of file ColorDB.py.

00039 
00040     def __init__(self, fp):
00041         lineno = 2
00042         self.__name = fp.name
00043         # Maintain several dictionaries for indexing into the color database.
00044         # Note that while Tk supports RGB intensities of 4, 8, 12, or 16 bits,
00045         # for now we only support 8 bit intensities.  At least on OpenWindows,
00046         # all intensities in the /usr/openwin/lib/rgb.txt file are 8-bit
00047         #
00048         # key is (red, green, blue) tuple, value is (name, [aliases])
00049         self.__byrgb = {}
00050         # key is name, value is (red, green, blue)
00051         self.__byname = {}
00052         # all unique names (non-aliases).  built-on demand
00053         self.__allnames = None
00054         for line in fp:
00055             # get this compiled regular expression from derived class
00056             mo = self._re.match(line)
00057             if not mo:
00058                 print('Error in', fp.name, ' line', lineno, file=sys.stderr)
00059                 lineno += 1
00060                 continue
00061             # extract the red, green, blue, and name
00062             red, green, blue = self._extractrgb(mo)
00063             name = self._extractname(mo)
00064             keyname = name.lower()
00065             # BAW: for now the `name' is just the first named color with the
00066             # rgb values we find.  Later, we might want to make the two word
00067             # version the `name', or the CapitalizedVersion, etc.
00068             key = (red, green, blue)
00069             foundname, aliases = self.__byrgb.get(key, (name, []))
00070             if foundname != name and foundname not in aliases:
00071                 aliases.append(name)
00072             self.__byrgb[key] = (foundname, aliases)
00073             # add to byname lookup
00074             self.__byname[keyname] = key
00075             lineno = lineno + 1

Here is the caller graph for this function:


Member Function Documentation

def pynche.ColorDB.ColorDB._extractname (   self,
  mo 
) [private]

Reimplemented in pynche.ColorDB.WebsafeDB, and pynche.ColorDB.LightlinkDB.

Definition at line 80 of file ColorDB.py.

00080 
00081     def _extractname(self, mo):
00082         return mo.group('name')

def pynche.ColorDB.ColorDB._extractrgb (   self,
  mo 
) [private]

Reimplemented in pynche.ColorDB.WebsafeDB, and pynche.ColorDB.HTML40DB.

Definition at line 77 of file ColorDB.py.

00077 
00078     def _extractrgb(self, mo):
00079         return [int(x) for x in mo.group('red', 'green', 'blue')]

def pynche.ColorDB.ColorDB.aliases_of (   self,
  red,
  green,
  blue 
)

Definition at line 128 of file ColorDB.py.

00128 
00129     def aliases_of(self, red, green, blue):
00130         try:
00131             name, aliases = self.__byrgb[(red, green, blue)]
00132         except KeyError:
00133             raise BadColor((red, green, blue))
00134         return [name] + aliases
00135 


Definition at line 83 of file ColorDB.py.

00083 
00084     def filename(self):
00085         return self.__name

Here is the caller graph for this function:

def pynche.ColorDB.ColorDB.find_byname (   self,
  name 
)
Return (red, green, blue) for name

Definition at line 93 of file ColorDB.py.

00093 
00094     def find_byname(self, name):
00095         """Return (red, green, blue) for name"""
00096         name = name.lower()
00097         try:
00098             return self.__byname[name]
00099         except KeyError:
00100             raise BadColor(name)

def pynche.ColorDB.ColorDB.find_byrgb (   self,
  rgbtuple 
)
Return name for rgbtuple

Definition at line 86 of file ColorDB.py.

00086 
00087     def find_byrgb(self, rgbtuple):
00088         """Return name for rgbtuple"""
00089         try:
00090             return self.__byrgb[rgbtuple]
00091         except KeyError:
00092             raise BadColor(rgbtuple)

def pynche.ColorDB.ColorDB.nearest (   self,
  red,
  green,
  blue 
)
Return the name of color nearest (red, green, blue)

Definition at line 101 of file ColorDB.py.

00101 
00102     def nearest(self, red, green, blue):
00103         """Return the name of color nearest (red, green, blue)"""
00104         # BAW: should we use Voronoi diagrams, Delaunay triangulation, or
00105         # octree for speeding up the locating of nearest point?  Exhaustive
00106         # search is inefficient, but seems fast enough.
00107         nearest = -1
00108         nearest_name = ''
00109         for name, aliases in self.__byrgb.values():
00110             r, g, b = self.__byname[name.lower()]
00111             rdelta = red - r
00112             gdelta = green - g
00113             bdelta = blue - b
00114             distance = rdelta * rdelta + gdelta * gdelta + bdelta * bdelta
00115             if nearest == -1 or distance < nearest:
00116                 nearest = distance
00117                 nearest_name = name
00118         return nearest_name

Definition at line 119 of file ColorDB.py.

00119 
00120     def unique_names(self):
00121         # sorted
00122         if not self.__allnames:
00123             self.__allnames = []
00124             for name, aliases in self.__byrgb.values():
00125                 self.__allnames.append(name)
00126             self.__allnames.sort(key=str.lower)
00127         return self.__allnames


Member Data Documentation

Definition at line 52 of file ColorDB.py.

Definition at line 50 of file ColorDB.py.

Definition at line 48 of file ColorDB.py.

Definition at line 41 of file ColorDB.py.


The documentation for this class was generated from the following file: