Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 1 | """Pynche: The PYthon Natural Color and Hue Editor. |
| 2 | |
| 3 | Pynche is based largely on a similar color editor I wrote years ago for the |
| 4 | Sunview window system. That editor was called ICE: the Interactive Color |
| 5 | Editor. I'd always wanted to port the editor to X but didn't feel like |
| 6 | 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] | 7 | Tkinter provides such a nice programming environment, with enough power, that |
| 8 | I finally buckled down and implemented it. I changed the name because these |
| 9 | days, too many other systems have the acronym `ICE'. |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 10 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 11 | This program currently requires Python 1.5 with Tkinter. It has only been |
| 12 | tested on Solaris 2.6. Feedback is greatly appreciated. Send email to |
| 13 | bwarsaw@python.org |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 14 | |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 15 | Usage: %(PROGRAM)s [-d file] [-i file] [-X] [-h] [initialcolor] |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 16 | |
| 17 | Where: |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 18 | --database file |
| 19 | -d file |
| 20 | Alternate location of a color database file |
Barry Warsaw | eab81a9 | 1998-02-11 18:56:13 +0000 | [diff] [blame] | 21 | |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 22 | --initfile file |
| 23 | -i file |
| 24 | Alternate location of the initialization file. This file contains a |
| 25 | persistent database of the current Pynche options and color. This |
| 26 | means that Pynche restores its option settings and current color when |
| 27 | it restarts, using this file (unless the -X option is used). The |
| 28 | default is ~/.pynche |
| 29 | |
| 30 | --ignore |
| 31 | -X |
| 32 | Ignore the initialization file when starting up. Pynche will still |
| 33 | write the current option settings to this file when it quits. |
| 34 | |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 35 | --help |
| 36 | -h |
| 37 | print this message |
| 38 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 39 | initialcolor |
| 40 | initial color, as a color name or #RRGGBB format |
| 41 | |
Barry Warsaw | 516f189 | 1998-01-27 03:19:00 +0000 | [diff] [blame] | 42 | """ |
| 43 | |
| 44 | __version__ = '1.0' |
| 45 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 46 | import sys |
Barry Warsaw | 6bfd655 | 1998-10-06 20:44:14 +0000 | [diff] [blame] | 47 | import os |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 48 | import getopt |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 49 | import ColorDB |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 50 | from PyncheWidget import PyncheWidget |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 51 | from Switchboard import Switchboard |
Barry Warsaw | 63c9e98 | 1998-09-28 23:41:53 +0000 | [diff] [blame] | 52 | from StripViewer import StripViewer |
Barry Warsaw | 4ab5d85 | 1998-10-01 16:47:06 +0000 | [diff] [blame] | 53 | from ChipViewer import ChipViewer |
| 54 | from TypeinViewer import TypeinViewer |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | |
| 58 | PROGRAM = sys.argv[0] |
| 59 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 60 | # Default locations of rgb.txt or other textual color database |
| 61 | RGB_TXT = [ |
| 62 | # Solaris OpenWindows |
| 63 | '/usr/openwin/lib/rgb.txt', |
Barry Warsaw | 6bfd655 | 1998-10-06 20:44:14 +0000 | [diff] [blame] | 64 | # The X11R6.4 rgb.txt file |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 65 | os.path.join(sys.path[0], 'X/rgb.txt'), |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 66 | # add more here |
| 67 | ] |
| 68 | |
| 69 | |
| 70 | |
| 71 | def usage(status, msg=''): |
Barry Warsaw | 14e2caf | 1998-10-06 15:21:04 +0000 | [diff] [blame] | 72 | print __doc__ % globals() |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 73 | if msg: |
| 74 | print msg |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 75 | sys.exit(status) |
| 76 | |
| 77 | |
| 78 | |
Barry Warsaw | e7f4a47 | 1998-10-06 19:50:33 +0000 | [diff] [blame] | 79 | def initial_color(s, colordb): |
| 80 | # function called on every color |
| 81 | def scan_color(s, colordb=colordb): |
| 82 | try: |
| 83 | r, g, b = colordb.find_byname(s) |
| 84 | except ColorDB.BadColor: |
| 85 | try: |
| 86 | r, g, b = ColorDB.rrggbb_to_triplet(s) |
| 87 | except ColorDB.BadColor: |
| 88 | return None, None, None |
| 89 | return r, g, b |
| 90 | # |
| 91 | # First try the passed in color |
| 92 | r, g, b = scan_color(s) |
| 93 | if r is None: |
| 94 | # try the same color with '#' prepended, since some shells require |
| 95 | # this to be escaped, which is a pain |
| 96 | r, g, b = scan_color('#' + s) |
| 97 | if r is None: |
| 98 | print 'Bad initial color, using gray50:', s |
| 99 | r, g, b = scan_color('gray50') |
| 100 | if r is None: |
| 101 | usage(1, 'Cannot find an initial color to use') |
| 102 | # does not return |
| 103 | return r, g, b |
| 104 | |
| 105 | |
| 106 | |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 107 | def build(master=None, initialcolor=None, initfile=None, ignore=None): |
| 108 | # create the windows and go |
| 109 | for f in RGB_TXT: |
| 110 | try: |
| 111 | colordb = ColorDB.get_colordb(f) |
| 112 | if colordb: |
| 113 | break |
| 114 | except IOError: |
| 115 | pass |
| 116 | else: |
| 117 | usage(1, 'No color database file found, see the -d option.') |
| 118 | |
| 119 | # create all output widgets |
| 120 | s = Switchboard(colordb, not ignore and initfile) |
| 121 | |
| 122 | # create the application window decorations |
| 123 | app = PyncheWidget(__version__, s, master=master) |
| 124 | w = app.window() |
| 125 | |
| 126 | s.add_view(StripViewer(s, w)) |
| 127 | s.add_view(ChipViewer(s, w)) |
| 128 | s.add_view(TypeinViewer(s, w)) |
| 129 | |
| 130 | # get the initial color as components and set the color on all views. if |
| 131 | # there was no initial color given on the command line, use the one that's |
| 132 | # stored in the option database |
| 133 | if initialcolor is None: |
| 134 | optiondb = s.optiondb() |
| 135 | red = optiondb.get('RED') |
| 136 | green = optiondb.get('GREEN') |
| 137 | blue = optiondb.get('BLUE') |
| 138 | # but if there wasn't any stored in the database, use grey50 |
| 139 | if red is None or blue is None or green is None: |
| 140 | red, green, blue = initial_color('grey50', colordb) |
| 141 | else: |
| 142 | red, green, blue = initial_color(initialcolor, colordb) |
| 143 | s.update_views(red, green, blue) |
| 144 | return app, s |
| 145 | |
| 146 | |
| 147 | def run(app, s): |
| 148 | try: |
| 149 | app.start() |
| 150 | except KeyboardInterrupt: |
| 151 | pass |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 152 | |
| 153 | |
| 154 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 155 | def main(): |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 156 | try: |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 157 | opts, args = getopt.getopt( |
| 158 | sys.argv[1:], |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 159 | 'hd:i:X', |
| 160 | ['database=', 'initfile=', 'ignore', 'help']) |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 161 | except getopt.error, msg: |
| 162 | usage(1, msg) |
| 163 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 164 | if len(args) == 0: |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 165 | initialcolor = None |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 166 | elif len(args) == 1: |
| 167 | initialcolor = args[0] |
| 168 | else: |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 169 | usage(1) |
| 170 | |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 171 | ignore = 0 |
| 172 | initfile = os.path.expanduser('~/.pynche') |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 173 | for opt, arg in opts: |
| 174 | if opt in ('-h', '--help'): |
| 175 | usage(0) |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 176 | elif opt in ('-d', '--database'): |
| 177 | RGB_TXT.insert(0, arg) |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 178 | elif opt in ('-X', '--ignore'): |
| 179 | ignore = 1 |
| 180 | elif opt in ('-i', '--initfile'): |
| 181 | initfile = arg |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 182 | |
Barry Warsaw | 877d299 | 1998-10-22 18:45:52 +0000 | [diff] [blame^] | 183 | app, sb = build(initialcolor=initialcolor, |
| 184 | initfile=initfile, |
| 185 | ignore=ignore) |
| 186 | run(app, sb) |
| 187 | sb.save_views() |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 188 | |
| 189 | |
| 190 | if __name__ == '__main__': |
| 191 | main() |