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