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 | d515a7e | 1999-04-26 23:18:07 +0000 | [diff] [blame] | 52 | __version__ = '1.0' |
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 | c2aadcd | 1999-04-23 16:24:00 +0000 | [diff] [blame] | 73 | # Linux |
| 74 | '/usr/lib/X11/rgb.txt', |
Barry Warsaw | 6bfd655 | 1998-10-06 20:44:14 +0000 | [diff] [blame] | 75 | # The X11R6.4 rgb.txt file |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 76 | os.path.join(sys.path[0], 'X/rgb.txt'), |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 77 | # add more here |
| 78 | ] |
| 79 | |
| 80 | |
| 81 | |
Barry Warsaw | d0e1e51 | 1998-12-03 19:49:13 +0000 | [diff] [blame] | 82 | def docstring(): |
| 83 | return string.rstrip(__doc__ % globals()) |
| 84 | |
| 85 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 86 | def usage(status, msg=''): |
Barry Warsaw | d0e1e51 | 1998-12-03 19:49:13 +0000 | [diff] [blame] | 87 | print docstring() |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 88 | if msg: |
Barry Warsaw | 3fe1b14 | 1998-11-18 03:45:09 +0000 | [diff] [blame] | 89 | print msg |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 90 | sys.exit(status) |
| 91 | |
| 92 | |
| 93 | |
Barry Warsaw | e7f4a47 | 1998-10-06 19:50:33 +0000 | [diff] [blame] | 94 | def initial_color(s, colordb): |
| 95 | # function called on every color |
| 96 | def scan_color(s, colordb=colordb): |
| 97 | try: |
| 98 | r, g, b = colordb.find_byname(s) |
| 99 | except ColorDB.BadColor: |
| 100 | try: |
| 101 | r, g, b = ColorDB.rrggbb_to_triplet(s) |
| 102 | except ColorDB.BadColor: |
| 103 | return None, None, None |
| 104 | return r, g, b |
| 105 | # |
| 106 | # First try the passed in color |
| 107 | r, g, b = scan_color(s) |
| 108 | if r is None: |
| 109 | # try the same color with '#' prepended, since some shells require |
| 110 | # this to be escaped, which is a pain |
| 111 | r, g, b = scan_color('#' + s) |
| 112 | if r is None: |
| 113 | print 'Bad initial color, using gray50:', s |
| 114 | r, g, b = scan_color('gray50') |
| 115 | if r is None: |
| 116 | usage(1, 'Cannot find an initial color to use') |
| 117 | # does not return |
| 118 | return r, g, b |
| 119 | |
| 120 | |
| 121 | |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 122 | def build(master=None, initialcolor=None, initfile=None, ignore=None): |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 123 | # create all output widgets |
Barry Warsaw | 0604d72 | 1999-04-26 23:17:16 +0000 | [diff] [blame] | 124 | s = Switchboard(not ignore and initfile) |
| 125 | |
| 126 | # load the color database |
| 127 | colordb = None |
| 128 | try: |
| 129 | dbfile = s.optiondb()['DBFILE'] |
| 130 | colordb = ColorDB.get_colordb(dbfile) |
| 131 | except (KeyError, IOError): |
| 132 | # scoot through the files listed above to try to find a usable color |
| 133 | # database file |
| 134 | for f in RGB_TXT: |
| 135 | try: |
| 136 | colordb = ColorDB.get_colordb(f) |
| 137 | if colordb: |
| 138 | break |
| 139 | except IOError: |
| 140 | pass |
| 141 | if not colordb: |
| 142 | usage(1, 'No color database file found, see the -d option.') |
| 143 | s.set_colordb(colordb) |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 144 | |
| 145 | # create the application window decorations |
| 146 | app = PyncheWidget(__version__, s, master=master) |
| 147 | w = app.window() |
| 148 | |
Barry Warsaw | b61a28e | 1999-04-27 18:43:47 +0000 | [diff] [blame] | 149 | # these built-in viewers live inside the main Pynche window |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 150 | s.add_view(StripViewer(s, w)) |
| 151 | s.add_view(ChipViewer(s, w)) |
| 152 | s.add_view(TypeinViewer(s, w)) |
| 153 | |
| 154 | # get the initial color as components and set the color on all views. if |
| 155 | # there was no initial color given on the command line, use the one that's |
| 156 | # stored in the option database |
| 157 | if initialcolor is None: |
| 158 | optiondb = s.optiondb() |
| 159 | red = optiondb.get('RED') |
| 160 | green = optiondb.get('GREEN') |
| 161 | blue = optiondb.get('BLUE') |
| 162 | # but if there wasn't any stored in the database, use grey50 |
| 163 | if red is None or blue is None or green is None: |
| 164 | red, green, blue = initial_color('grey50', colordb) |
| 165 | else: |
| 166 | red, green, blue = initial_color(initialcolor, colordb) |
| 167 | s.update_views(red, green, blue) |
| 168 | return app, s |
| 169 | |
| 170 | |
| 171 | def run(app, s): |
| 172 | try: |
| 173 | app.start() |
| 174 | except KeyboardInterrupt: |
| 175 | pass |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 176 | |
| 177 | |
| 178 | |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 179 | def main(): |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 180 | try: |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 181 | opts, args = getopt.getopt( |
| 182 | sys.argv[1:], |
Barry Warsaw | 3fe1b14 | 1998-11-18 03:45:09 +0000 | [diff] [blame] | 183 | 'hd:i:Xv', |
| 184 | ['database=', 'initfile=', 'ignore', 'help', 'version']) |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 185 | except getopt.error, msg: |
| 186 | usage(1, msg) |
| 187 | |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 188 | if len(args) == 0: |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 189 | initialcolor = None |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 190 | elif len(args) == 1: |
| 191 | initialcolor = args[0] |
| 192 | else: |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 193 | usage(1) |
| 194 | |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 195 | ignore = 0 |
| 196 | initfile = os.path.expanduser('~/.pynche') |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 197 | for opt, arg in opts: |
| 198 | if opt in ('-h', '--help'): |
| 199 | usage(0) |
Barry Warsaw | 3fe1b14 | 1998-11-18 03:45:09 +0000 | [diff] [blame] | 200 | elif opt in ('-v', '--version'): |
| 201 | print '''\ |
| 202 | Pynche -- The PYthon Natural Color and Hue Editor. |
Barry Warsaw | 6403991 | 1998-12-15 01:02:51 +0000 | [diff] [blame] | 203 | Contact: Barry Warsaw |
Barry Warsaw | 3fe1b14 | 1998-11-18 03:45:09 +0000 | [diff] [blame] | 204 | Email: bwarsaw@python.org |
| 205 | Version: %s''' % __version__ |
| 206 | sys.exit(0) |
Barry Warsaw | 04c7886 | 1998-09-28 16:28:04 +0000 | [diff] [blame] | 207 | elif opt in ('-d', '--database'): |
| 208 | RGB_TXT.insert(0, arg) |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 209 | elif opt in ('-X', '--ignore'): |
| 210 | ignore = 1 |
| 211 | elif opt in ('-i', '--initfile'): |
| 212 | initfile = arg |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 213 | |
Barry Warsaw | 877d299 | 1998-10-22 18:45:52 +0000 | [diff] [blame] | 214 | app, sb = build(initialcolor=initialcolor, |
| 215 | initfile=initfile, |
| 216 | ignore=ignore) |
| 217 | run(app, sb) |
| 218 | sb.save_views() |
Barry Warsaw | f7c1e5a | 1998-01-31 23:39:28 +0000 | [diff] [blame] | 219 | |
| 220 | |
| 221 | if __name__ == '__main__': |
| 222 | main() |