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