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