Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Pynche: The PYthon Natural Color and Hue Editor. |
| 4 | |
| 5 | Pynche is based largely on a similar color editor I wrote years ago for the |
| 6 | Sunview window system. That editor was called ICE: the Interactive Color |
| 7 | Editor. I'd always wanted to port the editor to X but didn't feel like |
| 8 | hacking X and C code to do it. Fast forward many years, to where Python + |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 9 | Tkinter provides such a nice programming environment, with enough power, that |
| 10 | I finally buckled down and implemented it. I changed the name because these |
| 11 | days, too many other systems have the acronym `ICE'. |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 12 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 13 | This program currently requires Python 1.5 with Tkinter. It has only been |
| 14 | tested on Solaris 2.6. Feedback is greatly appreciated. Send email to |
| 15 | bwarsaw@python.org |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 16 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 17 | Usage: %(PROGRAM)s [-d file] [-h] [initialcolor] |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 18 | |
| 19 | Where: |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 20 | --database file |
| 21 | -d file |
| 22 | Alternate location of a color database file |
Barry Warsaw | eab81a9 | 1998-02-11 18:56:13 +0000 | [diff] [blame] | 23 | |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 24 | --help |
| 25 | -h |
| 26 | print this message |
| 27 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 28 | initialcolor |
| 29 | initial color, as a color name or #RRGGBB format |
| 30 | |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 31 | """ |
| 32 | |
| 33 | __version__ = '1.0' |
| 34 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 35 | import sys |
| 36 | import getopt |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 37 | import ColorDB |
| 38 | from Tkinter import * |
| 39 | from PyncheWidget import PyncheWidget |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 40 | from Switchboard import Switchboard |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | |
| 44 | PROGRAM = sys.argv[0] |
| 45 | |
| 46 | # Milliseconds between interrupt checks |
| 47 | KEEPALIVE_TIMER = 500 |
| 48 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 49 | # Default locations of rgb.txt or other textual color database |
| 50 | RGB_TXT = [ |
| 51 | # Solaris OpenWindows |
| 52 | '/usr/openwin/lib/rgb.txt', |
| 53 | # add more here |
| 54 | ] |
| 55 | |
| 56 | |
| 57 | |
| 58 | def usage(status, msg=''): |
| 59 | if msg: |
| 60 | print msg |
| 61 | print __doc__ % globals() |
| 62 | sys.exit(status) |
| 63 | |
| 64 | |
| 65 | |
| 66 | app = None |
| 67 | |
| 68 | def keepalive(): |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 69 | # Exercise the Python interpreter regularly so keyboard interrupts get |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 70 | # through. |
| 71 | app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive) |
| 72 | |
| 73 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 74 | def finished(event=None): |
| 75 | sys.exit(0) |
| 76 | |
| 77 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 78 | def main(): |
| 79 | global app |
| 80 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 81 | try: |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 82 | opts, args = getopt.getopt( |
| 83 | sys.argv[1:], |
| 84 | 'hd:', |
| 85 | ['database=', 'help']) |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 86 | except getopt.error, msg: |
| 87 | usage(1, msg) |
| 88 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 89 | if len(args) == 0: |
| 90 | initialcolor = 'grey50' |
| 91 | elif len(args) == 1: |
| 92 | initialcolor = args[0] |
| 93 | else: |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 94 | usage(1) |
| 95 | |
| 96 | for opt, arg in opts: |
| 97 | if opt in ('-h', '--help'): |
| 98 | usage(0) |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 99 | elif opt in ('-d', '--database'): |
| 100 | RGB_TXT.insert(0, arg) |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 101 | |
| 102 | # create the windows and go |
| 103 | for f in RGB_TXT: |
| 104 | try: |
| 105 | colordb = ColorDB.get_colordb(f) |
| 106 | break |
| 107 | except IOError: |
| 108 | pass |
| 109 | else: |
| 110 | raise IOError('No color database file found') |
| 111 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 112 | app = Tk(className='Pynche') |
| 113 | app.protocol('WM_DELETE_WINDOW', finished) |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 114 | app.title('Pynche %s' % __version__) |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 115 | app.iconname('Pynche') |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 116 | app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive) |
Barry Warsaw | eab81a9 | 1998-02-11 18:56:13 +0000 | [diff] [blame] | 117 | |
| 118 | # get triplet for initial color |
| 119 | try: |
| 120 | red, green, blue = colordb.find_byname(initialcolor) |
| 121 | except ColorDB.BadColor: |
| 122 | # must be a #rrggbb style color |
| 123 | try: |
| 124 | red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor) |
| 125 | except ColorDB.BadColor: |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 126 | print 'Bad initial color, using default: %s' % initialcolor |
| 127 | initialcolor = 'grey50' |
| 128 | try: |
| 129 | red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor) |
| 130 | except ColorDB.BadColor: |
| 131 | usage(1, 'Cannot find an initial color to use') |
Barry Warsaw | eab81a9 | 1998-02-11 18:56:13 +0000 | [diff] [blame] | 132 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame^] | 133 | s = Switchboard(app, colordb, red, green, blue) |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 134 | try: |
| 135 | keepalive() |
| 136 | app.mainloop() |
| 137 | except KeyboardInterrupt: |
| 138 | pass |
| 139 | |
| 140 | |
| 141 | |
| 142 | if __name__ == '__main__': |
| 143 | main() |