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 |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 38 | from PyncheWidget import PyncheWidget |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 39 | from Switchboard import Switchboard |
Barry Warsaw | 63c9e98 | 1998-09-28 23:41:53 +0000 | [diff] [blame] | 40 | from StripViewer import StripViewer |
Barry Warsaw | 4ab5d85 | 1998-10-01 16:47:06 +0000 | [diff] [blame] | 41 | from ChipViewer import ChipViewer |
| 42 | from TypeinViewer import TypeinViewer |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | |
| 46 | PROGRAM = sys.argv[0] |
| 47 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 48 | # Default locations of rgb.txt or other textual color database |
| 49 | RGB_TXT = [ |
| 50 | # Solaris OpenWindows |
| 51 | '/usr/openwin/lib/rgb.txt', |
| 52 | # add more here |
| 53 | ] |
| 54 | |
| 55 | |
| 56 | |
| 57 | def usage(status, msg=''): |
Barry Warsaw | 14e2caf | 1998-10-06 15:21:04 +0000 | [diff] [blame] | 58 | print __doc__ % globals() |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 59 | if msg: |
| 60 | print msg |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 61 | sys.exit(status) |
| 62 | |
| 63 | |
| 64 | |
Barry Warsaw | e7f4a47 | 1998-10-06 19:50:33 +0000 | [diff] [blame^] | 65 | def initial_color(s, colordb): |
| 66 | # function called on every color |
| 67 | def scan_color(s, colordb=colordb): |
| 68 | try: |
| 69 | r, g, b = colordb.find_byname(s) |
| 70 | except ColorDB.BadColor: |
| 71 | try: |
| 72 | r, g, b = ColorDB.rrggbb_to_triplet(s) |
| 73 | except ColorDB.BadColor: |
| 74 | return None, None, None |
| 75 | return r, g, b |
| 76 | # |
| 77 | # First try the passed in color |
| 78 | r, g, b = scan_color(s) |
| 79 | if r is None: |
| 80 | # try the same color with '#' prepended, since some shells require |
| 81 | # this to be escaped, which is a pain |
| 82 | r, g, b = scan_color('#' + s) |
| 83 | if r is None: |
| 84 | print 'Bad initial color, using gray50:', s |
| 85 | r, g, b = scan_color('gray50') |
| 86 | if r is None: |
| 87 | usage(1, 'Cannot find an initial color to use') |
| 88 | # does not return |
| 89 | return r, g, b |
| 90 | |
| 91 | |
| 92 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 93 | def main(): |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 94 | try: |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 95 | opts, args = getopt.getopt( |
| 96 | sys.argv[1:], |
| 97 | 'hd:', |
| 98 | ['database=', 'help']) |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 99 | except getopt.error, msg: |
| 100 | usage(1, msg) |
| 101 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 102 | if len(args) == 0: |
| 103 | initialcolor = 'grey50' |
| 104 | elif len(args) == 1: |
| 105 | initialcolor = args[0] |
| 106 | else: |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 107 | usage(1) |
| 108 | |
| 109 | for opt, arg in opts: |
| 110 | if opt in ('-h', '--help'): |
| 111 | usage(0) |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 112 | elif opt in ('-d', '--database'): |
| 113 | RGB_TXT.insert(0, arg) |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 114 | |
| 115 | # create the windows and go |
| 116 | for f in RGB_TXT: |
| 117 | try: |
| 118 | colordb = ColorDB.get_colordb(f) |
Barry Warsaw | 14e2caf | 1998-10-06 15:21:04 +0000 | [diff] [blame] | 119 | if colordb: |
| 120 | break |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 121 | except IOError: |
| 122 | pass |
| 123 | else: |
Barry Warsaw | 14e2caf | 1998-10-06 15:21:04 +0000 | [diff] [blame] | 124 | usage(1, 'No color database file found, see the -d option.') |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 125 | |
Barry Warsaw | e7f4a47 | 1998-10-06 19:50:33 +0000 | [diff] [blame^] | 126 | # get the initial color as components |
| 127 | red, green, blue = initial_color(initialcolor, colordb) |
Barry Warsaw | eab81a9 | 1998-02-11 18:56:13 +0000 | [diff] [blame] | 128 | |
Barry Warsaw | 63c9e98 | 1998-09-28 23:41:53 +0000 | [diff] [blame] | 129 | # create all output widgets |
Barry Warsaw | 4ab5d85 | 1998-10-01 16:47:06 +0000 | [diff] [blame] | 130 | s = Switchboard(colordb) |
| 131 | |
| 132 | # create the application window decorations |
| 133 | app = PyncheWidget(__version__, s) |
| 134 | parent = app.parent() |
| 135 | |
Barry Warsaw | 63c9e98 | 1998-09-28 23:41:53 +0000 | [diff] [blame] | 136 | s.add_view(StripViewer(s, parent)) |
Barry Warsaw | 4ab5d85 | 1998-10-01 16:47:06 +0000 | [diff] [blame] | 137 | s.add_view(ChipViewer(s, parent)) |
| 138 | s.add_view(TypeinViewer(s, parent)) |
Barry Warsaw | 63c9e98 | 1998-09-28 23:41:53 +0000 | [diff] [blame] | 139 | s.update_views(red, green, blue) |
| 140 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 141 | try: |
Barry Warsaw | 63c9e98 | 1998-09-28 23:41:53 +0000 | [diff] [blame] | 142 | app.start() |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 143 | except KeyboardInterrupt: |
| 144 | pass |
| 145 | |
| 146 | |
| 147 | |
| 148 | if __name__ == '__main__': |
| 149 | main() |