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 + |
| 9 | Tkinter + Pmw provides such a nice programming environment, with enough |
| 10 | power, that I finally buckled down and implemented it. I changed the name |
| 11 | because these days, too many other systems have the acronym `ICE'. |
| 12 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 13 | This program currently requires Python 1.5 with Tkinter. It also requires at |
| 14 | least Pmw 0.6.1. It has only been tested on Solaris 2.6. Feedback is greatly |
| 15 | appreciated. Send email to bwarsaw@python.org |
| 16 | |
Barry Warsaw | eab81a9 | 1998-02-11 18:56:13 +0000 | [diff] [blame] | 17 | Usage: %(PROGRAM)s [-c color] [-h] |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 18 | |
| 19 | Where: |
Barry Warsaw | eab81a9 | 1998-02-11 18:56:13 +0000 | [diff] [blame] | 20 | --color color |
| 21 | -c color |
| 22 | initial color, as an X color name or #RRGGBB format |
| 23 | |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 24 | --help |
| 25 | -h |
| 26 | print this message |
| 27 | |
| 28 | """ |
| 29 | |
| 30 | __version__ = '1.0' |
| 31 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 32 | import sys |
| 33 | import getopt |
| 34 | import Pmw |
| 35 | import ColorDB |
| 36 | from Tkinter import * |
| 37 | from PyncheWidget import PyncheWidget |
| 38 | |
| 39 | |
| 40 | |
| 41 | PROGRAM = sys.argv[0] |
| 42 | |
| 43 | # Milliseconds between interrupt checks |
| 44 | KEEPALIVE_TIMER = 500 |
| 45 | |
| 46 | RGBCOLOR = 1 |
| 47 | HSICOLOR = 2 |
| 48 | NAMEDCOLOR = 3 |
| 49 | |
| 50 | # Default locations of rgb.txt or other textual color database |
| 51 | RGB_TXT = [ |
| 52 | # Solaris OpenWindows |
| 53 | '/usr/openwin/lib/rgb.txt', |
| 54 | # add more here |
| 55 | ] |
| 56 | |
| 57 | |
| 58 | |
| 59 | def usage(status, msg=''): |
| 60 | if msg: |
| 61 | print msg |
| 62 | print __doc__ % globals() |
| 63 | sys.exit(status) |
| 64 | |
| 65 | |
| 66 | |
| 67 | app = None |
| 68 | |
| 69 | def keepalive(): |
| 70 | # Exercise the Python interpreter regularly so keybard interrupts get |
| 71 | # through. |
| 72 | app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive) |
| 73 | |
| 74 | |
| 75 | def main(): |
| 76 | global app |
| 77 | |
Barry Warsaw | f7e4793 | 1998-02-12 19:53:01 +0000 | [diff] [blame] | 78 | initialcolor = 'grey50' |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 79 | try: |
| 80 | opts, args = getopt.getopt(sys.argv[1:], |
| 81 | 'hc:', |
| 82 | ['color=', 'help']) |
| 83 | except getopt.error, msg: |
| 84 | usage(1, msg) |
| 85 | |
| 86 | if args: |
| 87 | usage(1) |
| 88 | |
| 89 | for opt, arg in opts: |
| 90 | if opt in ('-h', '--help'): |
| 91 | usage(0) |
| 92 | elif opt in ('-c', '--color'): |
| 93 | initialcolor = arg |
| 94 | |
| 95 | # create the windows and go |
| 96 | for f in RGB_TXT: |
| 97 | try: |
| 98 | colordb = ColorDB.get_colordb(f) |
| 99 | break |
| 100 | except IOError: |
| 101 | pass |
| 102 | else: |
| 103 | raise IOError('No color database file found') |
| 104 | |
| 105 | app = Pmw.initialise(fontScheme='pmw1') |
| 106 | app.title('Pynche %s' % __version__) |
| 107 | app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive) |
Barry Warsaw | eab81a9 | 1998-02-11 18:56:13 +0000 | [diff] [blame] | 108 | |
| 109 | # get triplet for initial color |
| 110 | try: |
| 111 | red, green, blue = colordb.find_byname(initialcolor) |
| 112 | except ColorDB.BadColor: |
| 113 | # must be a #rrggbb style color |
| 114 | try: |
| 115 | red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor) |
| 116 | except ColorDB.BadColor: |
| 117 | usage(1, 'Bad initial color: %s' % initialcolor) |
| 118 | |
| 119 | p = PyncheWidget(colordb, app, color=(red, green, blue)) |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 120 | try: |
| 121 | keepalive() |
| 122 | app.mainloop() |
| 123 | except KeyboardInterrupt: |
| 124 | pass |
| 125 | |
| 126 | |
| 127 | |
| 128 | if __name__ == '__main__': |
| 129 | main() |