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