diff options
Diffstat (limited to 'Tools/pynche')
-rw-r--r-- | Tools/pynche/ChipViewer.py | 2 | ||||
-rw-r--r-- | Tools/pynche/ColorDB.py | 41 | ||||
-rw-r--r-- | Tools/pynche/DetailsViewer.py | 2 | ||||
-rw-r--r-- | Tools/pynche/ListViewer.py | 2 | ||||
-rw-r--r-- | Tools/pynche/Main.py | 12 | ||||
-rw-r--r-- | Tools/pynche/PyncheWidget.py | 26 | ||||
-rw-r--r-- | Tools/pynche/README | 14 | ||||
-rw-r--r-- | Tools/pynche/StripViewer.py | 54 | ||||
-rw-r--r-- | Tools/pynche/Switchboard.py | 15 | ||||
-rw-r--r-- | Tools/pynche/TextViewer.py | 2 | ||||
-rw-r--r-- | Tools/pynche/TypeinViewer.py | 2 | ||||
-rw-r--r-- | Tools/pynche/pyColorChooser.py | 8 |
12 files changed, 107 insertions, 73 deletions
diff --git a/Tools/pynche/ChipViewer.py b/Tools/pynche/ChipViewer.py index 78139f8..f59aa28 100644 --- a/Tools/pynche/ChipViewer.py +++ b/Tools/pynche/ChipViewer.py @@ -13,7 +13,7 @@ The ChipViewer class includes the entire lower left quandrant; i.e. both the selected and nearest ChipWidgets. """ -from tkinter import * +from Tkinter import * import ColorDB diff --git a/Tools/pynche/ColorDB.py b/Tools/pynche/ColorDB.py index eb76d40..84a5b07 100644 --- a/Tools/pynche/ColorDB.py +++ b/Tools/pynche/ColorDB.py @@ -23,6 +23,7 @@ color formats, and for calculating other color values. import sys import re from types import * +import operator class BadColor(Exception): pass @@ -53,7 +54,7 @@ class ColorDB: # get this compiled regular expression from derived class mo = self._re.match(line) if not mo: - print('Error in', fp.name, ' line', lineno, file=sys.stderr) + print >> sys.stderr, 'Error in', fp.name, ' line', lineno lineno += 1 continue # extract the red, green, blue, and name @@ -65,7 +66,7 @@ class ColorDB: # version the `name', or the CapitalizedVersion, etc. key = (red, green, blue) foundname, aliases = self.__byrgb.get(key, (name, [])) - if foundname != name and foundname not in aliases: + if foundname <> name and foundname not in aliases: aliases.append(name) self.__byrgb[key] = (foundname, aliases) # add to byname lookup @@ -87,7 +88,7 @@ class ColorDB: try: return self.__byrgb[rgbtuple] except KeyError: - raise BadColor(rgbtuple) from None + raise BadColor(rgbtuple) def find_byname(self, name): """Return (red, green, blue) for name""" @@ -95,7 +96,7 @@ class ColorDB: try: return self.__byname[name] except KeyError: - raise BadColor(name) from None + raise BadColor(name) def nearest(self, red, green, blue): """Return the name of color nearest (red, green, blue)""" @@ -121,30 +122,33 @@ class ColorDB: self.__allnames = [] for name, aliases in self.__byrgb.values(): self.__allnames.append(name) - self.__allnames.sort(key=str.lower) + # sort irregardless of case + def nocase_cmp(n1, n2): + return cmp(n1.lower(), n2.lower()) + self.__allnames.sort(nocase_cmp) return self.__allnames def aliases_of(self, red, green, blue): try: name, aliases = self.__byrgb[(red, green, blue)] except KeyError: - raise BadColor((red, green, blue)) from None + raise BadColor((red, green, blue)) return [name] + aliases class RGBColorDB(ColorDB): _re = re.compile( - r'\s*(?P<red>\d+)\s+(?P<green>\d+)\s+(?P<blue>\d+)\s+(?P<name>.*)') + '\s*(?P<red>\d+)\s+(?P<green>\d+)\s+(?P<blue>\d+)\s+(?P<name>.*)') class HTML40DB(ColorDB): - _re = re.compile(r'(?P<name>\S+)\s+(?P<hexrgb>#[0-9a-fA-F]{6})') + _re = re.compile('(?P<name>\S+)\s+(?P<hexrgb>#[0-9a-fA-F]{6})') def _extractrgb(self, mo): return rrggbb_to_triplet(mo.group('hexrgb')) class LightlinkDB(HTML40DB): - _re = re.compile(r'(?P<name>(.+))\s+(?P<hexrgb>#[0-9a-fA-F]{6})') + _re = re.compile('(?P<name>(.+))\s+(?P<hexrgb>#[0-9a-fA-F]{6})') def _extractname(self, mo): return mo.group('name').strip() @@ -208,7 +212,7 @@ def rrggbb_to_triplet(color): """Converts a #rrggbb color to the tuple (red, green, blue).""" rgbtuple = _namedict.get(color) if rgbtuple is None: - if color[0] != '#': + if color[0] <> '#': raise BadColor(color) red = color[1:3] green = color[3:5] @@ -229,8 +233,9 @@ def triplet_to_rrggbb(rgbtuple): return hexname +_maxtuple = (256.0,) * 3 def triplet_to_fractional_rgb(rgbtuple): - return [x / 256 for x in rgbtuple] + return map(operator.__div__, rgbtuple, _maxtuple) def triplet_to_brightness(rgbtuple): @@ -246,26 +251,26 @@ def triplet_to_brightness(rgbtuple): if __name__ == '__main__': colordb = get_colordb('/usr/openwin/lib/rgb.txt') if not colordb: - print('No parseable color database found') + print 'No parseable color database found' sys.exit(1) # on my system, this color matches exactly target = 'navy' red, green, blue = rgbtuple = colordb.find_byname(target) - print(target, ':', red, green, blue, triplet_to_rrggbb(rgbtuple)) + print target, ':', red, green, blue, triplet_to_rrggbb(rgbtuple) name, aliases = colordb.find_byrgb(rgbtuple) - print('name:', name, 'aliases:', COMMASPACE.join(aliases)) + print 'name:', name, 'aliases:', COMMASPACE.join(aliases) r, g, b = (1, 1, 128) # nearest to navy r, g, b = (145, 238, 144) # nearest to lightgreen r, g, b = (255, 251, 250) # snow - print('finding nearest to', target, '...') + print 'finding nearest to', target, '...' import time t0 = time.time() nearest = colordb.nearest(r, g, b) t1 = time.time() - print('found nearest color', nearest, 'in', t1-t0, 'seconds') + print 'found nearest color', nearest, 'in', t1-t0, 'seconds' # dump the database for n in colordb.unique_names(): r, g, b = colordb.find_byname(n) aliases = colordb.aliases_of(r, g, b) - print('%20s: (%3d/%3d/%3d) == %s' % (n, r, g, b, - SPACE.join(aliases[1:]))) + print '%20s: (%3d/%3d/%3d) == %s' % (n, r, g, b, + SPACE.join(aliases[1:])) diff --git a/Tools/pynche/DetailsViewer.py b/Tools/pynche/DetailsViewer.py index bed11f4..fb597b5 100644 --- a/Tools/pynche/DetailsViewer.py +++ b/Tools/pynche/DetailsViewer.py @@ -52,7 +52,7 @@ Shift + Left == -25 Shift + Right == +25 """ -from tkinter import * +from Tkinter import * STOP = 'Stop' WRAP = 'Wrap Around' diff --git a/Tools/pynche/ListViewer.py b/Tools/pynche/ListViewer.py index b187844..213cd08 100644 --- a/Tools/pynche/ListViewer.py +++ b/Tools/pynche/ListViewer.py @@ -15,7 +15,7 @@ You can turn off Update On Click if all you want to see is the alias for a given name, without selecting the color. """ -from tkinter import * +from Tkinter import * import ColorDB ADDTOVIEW = 'Color %List Window...' diff --git a/Tools/pynche/Main.py b/Tools/pynche/Main.py index 4db560b..1fa3f17 100644 --- a/Tools/pynche/Main.py +++ b/Tools/pynche/Main.py @@ -85,9 +85,9 @@ def docstring(): def usage(code, msg=''): - print(docstring()) + print docstring() if msg: - print(msg) + print msg sys.exit(code) @@ -111,7 +111,7 @@ def initial_color(s, colordb): # this to be escaped, which is a pain r, g, b = scan_color('#' + s) if r is None: - print('Bad initial color, using gray50:', s) + print 'Bad initial color, using gray50:', s r, g, b = scan_color('gray50') if r is None: usage(1, 'Cannot find an initial color to use') @@ -186,7 +186,7 @@ def main(): sys.argv[1:], 'hd:i:Xv', ['database=', 'initfile=', 'ignore', 'help', 'version']) - except getopt.error as msg: + except getopt.error, msg: usage(1, msg) if len(args) == 0: @@ -203,11 +203,11 @@ def main(): if opt in ('-h', '--help'): usage(0) elif opt in ('-v', '--version'): - print("""\ + print """\ Pynche -- The PYthon Natural Color and Hue Editor. Contact: %(AUTHNAME)s Email: %(AUTHEMAIL)s -Version: %(__version__)s""" % globals()) +Version: %(__version__)s""" % globals() sys.exit(0) elif opt in ('-d', '--database'): dbfile = arg diff --git a/Tools/pynche/PyncheWidget.py b/Tools/pynche/PyncheWidget.py index ef12198..fcfe7ce 100644 --- a/Tools/pynche/PyncheWidget.py +++ b/Tools/pynche/PyncheWidget.py @@ -6,8 +6,9 @@ It is used to bring up other windows. import sys import os -from tkinter import * -from tkinter import messagebox, filedialog +from Tkinter import * +import tkMessageBox +import tkFileDialog import ColorDB # Milliseconds between interrupt checks @@ -149,7 +150,7 @@ class PyncheWidget: def __popup_about(self, event=None): from Main import __version__ - messagebox.showinfo('About Pynche ' + __version__, + tkMessageBox.showinfo('About Pynche ' + __version__, '''\ Pynche %s The PYthonically Natural @@ -167,7 +168,7 @@ email: bwarsaw@python.org''' % __version__) def __load(self, event=None): while 1: idir, ifile = os.path.split(self.__sb.colordb().filename()) - file = filedialog.askopenfilename( + file = tkFileDialog.askopenfilename( filetypes=[('Text files', '*.txt'), ('All files', '*'), ], @@ -179,12 +180,12 @@ email: bwarsaw@python.org''' % __version__) try: colordb = ColorDB.get_colordb(file) except IOError: - messagebox.showerror('Read error', '''\ + tkMessageBox.showerror('Read error', '''\ Could not open file for reading: %s''' % file) continue if colordb is None: - messagebox.showerror('Unrecognized color file type', '''\ + tkMessageBox.showerror('Unrecognized color file type', '''\ Unrecognized color file type in file: %s''' % file) continue @@ -248,8 +249,6 @@ class Helpwin: -import functools -@functools.total_ordering class PopupViewer: def __init__(self, module, name, switchboard, root): self.__m = module @@ -280,15 +279,8 @@ class PopupViewer: self.__sb.add_view(self.__window) self.__window.deiconify() - def __eq__(self, other): - if isinstance(self, PopupViewer): - return self.__menutext == other.__menutext - return NotImplemented - - def __lt__(self, other): - if isinstance(self, PopupViewer): - return self.__menutext < other.__menutext - return NotImplemented + def __cmp__(self, other): + return cmp(self.__menutext, other.__menutext) def make_view_popups(switchboard, root, extrapath): diff --git a/Tools/pynche/README b/Tools/pynche/README index e026159..d20efc3 100644 --- a/Tools/pynche/README +++ b/Tools/pynche/README @@ -48,7 +48,7 @@ Running Standalone --initfile file -i file - Alternate location of the persistent initialization file. See + Alternate location of the persistent initialization file. See the section on Persistency below. --ignore @@ -80,7 +80,7 @@ Running as a Modal Dialog pyColorChooser.askcolor() - which will popup Pynche as a modal dialog, and return the selected + which will popup Pynche as a modal dialog, and return the selected color. There are some UI differences when running as a modal @@ -106,7 +106,7 @@ Running as a Modal Dialog master[*] the master window to use as the parent of the modal - dialog. Without this argument, pyColorChooser will create + dialog. Without this argument, pyColorChooser will create its own Tkinter.Tk instance as the master. This may not be what you want. @@ -170,7 +170,7 @@ The Proof Window Selected chip color exactly matches the Nearest chip color, you will see the color name appear below the color specification for the Selected chip. - + Clicking on the Nearest color chip selects that color. Color distance is calculated in the 3D space of the RGB color solid and if more than one color name is the same distance from the selected @@ -361,7 +361,7 @@ Color Name Database Files format for both values and names webcolors.txt -- The 140 color names that Tim Peters and his - sister say NS and MSIE both understand (with some controversy over + sister say NS and MSIE both understand (with some controversy over AliceBlue). namedcolors.txt -- an alternative set of Netscape colors. @@ -369,8 +369,8 @@ Color Name Database Files You can switch between files by choosing "Load palette..." from the "File" menu. This brings up a standard Tk file dialog. Choose the file you want and then click "Ok". If Pynche - understands the format in this file, it will load the database and - update the appropriate windows. If not, it will bring up an error + understands the format in this file, it will load the database and + update the appropriate windows. If not, it will bring up an error dialog. diff --git a/Tools/pynche/StripViewer.py b/Tools/pynche/StripViewer.py index 6914ca9..01bcbf6 100644 --- a/Tools/pynche/StripViewer.py +++ b/Tools/pynche/StripViewer.py @@ -24,7 +24,7 @@ select the color under the cursor while you drag it, but be forewarned that this can be slow. """ -from tkinter import * +from Tkinter import * import ColorDB # Load this script into the Tcl interpreter and call it in @@ -62,32 +62,32 @@ def constant(numchips): # red variations, green+blue = cyan constant def constant_red_generator(numchips, red, green, blue): seq = constant(numchips) - return list(zip([red] * numchips, seq, seq)) + return map(None, [red] * numchips, seq, seq) # green variations, red+blue = magenta constant def constant_green_generator(numchips, red, green, blue): seq = constant(numchips) - return list(zip(seq, [green] * numchips, seq)) + return map(None, seq, [green] * numchips, seq) # blue variations, red+green = yellow constant def constant_blue_generator(numchips, red, green, blue): seq = constant(numchips) - return list(zip(seq, seq, [blue] * numchips)) + return map(None, seq, seq, [blue] * numchips) # red variations, green+blue = cyan constant def constant_cyan_generator(numchips, red, green, blue): seq = constant(numchips) - return list(zip(seq, [green] * numchips, [blue] * numchips)) + return map(None, seq, [green] * numchips, [blue] * numchips) # green variations, red+blue = magenta constant def constant_magenta_generator(numchips, red, green, blue): seq = constant(numchips) - return list(zip([red] * numchips, seq, [blue] * numchips)) + return map(None, [red] * numchips, seq, [blue] * numchips) # blue variations, red+green = yellow constant def constant_yellow_generator(numchips, red, green, blue): seq = constant(numchips) - return list(zip([red] * numchips, [green] * numchips, seq)) + return map(None, [red] * numchips, [green] * numchips, seq) @@ -119,7 +119,7 @@ class LeftArrow: return arrow, text def _x(self): - coords = list(self._canvas.coords(self._TAG)) + coords = self._canvas.coords(self._TAG) assert coords return coords[0] @@ -151,7 +151,7 @@ class RightArrow(LeftArrow): return arrow, text def _x(self): - coords = list(self._canvas.coords(self._TAG)) + coords = self._canvas.coords(self._TAG) assert coords return coords[0] + self._ARROWWIDTH @@ -371,6 +371,22 @@ class StripViewer: command=self.__togglehex) hexbtn.grid(row=1, column=1, sticky=W) + # XXX: ignore this feature for now; it doesn't work quite right yet + +## gentypevar = self.__gentypevar = IntVar() +## self.__variations = Radiobutton(frame, +## text='Variations', +## variable=gentypevar, +## value=0, +## command=self.__togglegentype) +## self.__variations.grid(row=0, column=1, sticky=W) +## self.__constants = Radiobutton(frame, +## text='Constants', +## variable=gentypevar, +## value=1, +## command=self.__togglegentype) +## self.__constants.grid(row=1, column=1, sticky=W) + # create the white button whitebtn = Button(frame2, text='White', @@ -386,6 +402,26 @@ class StripViewer: red, green, blue = self.__sb.current_rgb() self.update_yourself(red, green, blue) +## def __togglegentype(self, event=None): +## which = self.__gentypevar.get() +## if which == 0: +## self.__reds.set(label='Red Variations', +## generator=constant_cyan_generator) +## self.__greens.set(label='Green Variations', +## generator=constant_magenta_generator) +## self.__blues.set(label='Blue Variations', +## generator=constant_yellow_generator) +## elif which == 1: +## self.__reds.set(label='Red Constant', +## generator=constant_red_generator) +## self.__greens.set(label='Green Constant', +## generator=constant_green_generator) +## self.__blues.set(label='Blue Constant', +## generator=constant_blue_generator) +## else: +## assert 0 +## self.__sb.update_views_current() + def __toblack(self, event=None): self.__sb.update_views(0, 0, 0) diff --git a/Tools/pynche/Switchboard.py b/Tools/pynche/Switchboard.py index 013bb01..ee83d47 100644 --- a/Tools/pynche/Switchboard.py +++ b/Tools/pynche/Switchboard.py @@ -42,6 +42,7 @@ master window. """ import sys +from types import DictType import marshal @@ -61,11 +62,11 @@ class Switchboard: if initfile: try: try: - fp = open(initfile, 'rb') + fp = open(initfile) self.__optiondb = marshal.load(fp) - if not isinstance(self.__optiondb, dict): - print('Problem reading options from file:', initfile, - file=sys.stderr) + if not isinstance(self.__optiondb, DictType): + print >> sys.stderr, \ + 'Problem reading options from file:', initfile self.__optiondb = {} except (IOError, EOFError, ValueError): pass @@ -116,10 +117,10 @@ class Switchboard: fp = None try: try: - fp = open(self.__initfile, 'wb') + fp = open(self.__initfile, 'w') except IOError: - print('Cannot write options to file:', \ - self.__initfile, file=sys.stderr) + print >> sys.stderr, 'Cannot write options to file:', \ + self.__initfile else: marshal.dump(self.__optiondb, fp) finally: diff --git a/Tools/pynche/TextViewer.py b/Tools/pynche/TextViewer.py index baa1e62..456bd96 100644 --- a/Tools/pynche/TextViewer.py +++ b/Tools/pynche/TextViewer.py @@ -15,7 +15,7 @@ button and drag it through some text. The Insertion is the insertion cursor in the text window (which only has a background). """ -from tkinter import * +from Tkinter import * import ColorDB ADDTOVIEW = 'Text Window...' diff --git a/Tools/pynche/TypeinViewer.py b/Tools/pynche/TypeinViewer.py index 2f93e6b..d56c1b3 100644 --- a/Tools/pynche/TypeinViewer.py +++ b/Tools/pynche/TypeinViewer.py @@ -12,7 +12,7 @@ color selection will be made on every change to the text field. Otherwise, you must hit Return or Tab to select the color. """ -from tkinter import * +from Tkinter import * diff --git a/Tools/pynche/pyColorChooser.py b/Tools/pynche/pyColorChooser.py index 3286047..56f6940 100644 --- a/Tools/pynche/pyColorChooser.py +++ b/Tools/pynche/pyColorChooser.py @@ -28,10 +28,10 @@ class Chooser: dbfile = options.get('databasefile', self.__databasefile) # load the database file colordb = None - if dbfile != self.__databasefile: + if dbfile <> self.__databasefile: colordb = ColorDB.get_colordb(dbfile) if not self.__master: - from tkinter import Tk + from Tkinter import Tk self.__master = Tk() if not self.__pw: self.__pw, self.__sb = \ @@ -81,7 +81,7 @@ def askcolor(color = None, **options): """Ask for a color""" global _chooser if not _chooser: - _chooser = Chooser(**options) + _chooser = apply(Chooser, (), options) return _chooser.show(color, options) def save(): @@ -92,7 +92,7 @@ def save(): # test stuff if __name__ == '__main__': - from tkinter import * + from Tkinter import * class Tester: def __init__(self): |