blob: 8cea31cbc17950307bd7097cda9f363c0c3647b0 [file] [log] [blame]
Barry Warsaw3fe1b141998-11-18 03:45:09 +00001"""Pynche -- The PYthon Natural Color and Hue Editor.
2
Barry Warsaw64039911998-12-15 01:02:51 +00003Contact: Barry Warsaw
Barry Warsaw3fe1b141998-11-18 03:45:09 +00004Email: bwarsaw@python.org
5Version: %(__version__)s
Barry Warsaw516f1891998-01-27 03:19:00 +00006
7Pynche is based largely on a similar color editor I wrote years ago for the
8Sunview window system. That editor was called ICE: the Interactive Color
9Editor. I'd always wanted to port the editor to X but didn't feel like
10hacking X and C code to do it. Fast forward many years, to where Python +
Barry Warsaw04c78861998-09-28 16:28:04 +000011Tkinter provides such a nice programming environment, with enough power, that
12I finally buckled down and implemented it. I changed the name because these
13days, too many other systems have the acronym `ICE'.
Barry Warsaw516f1891998-01-27 03:19:00 +000014
Barry Warsaw04c78861998-09-28 16:28:04 +000015This program currently requires Python 1.5 with Tkinter. It has only been
16tested on Solaris 2.6. Feedback is greatly appreciated. Send email to
17bwarsaw@python.org
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000018
Barry Warsaw3fe1b141998-11-18 03:45:09 +000019Usage: %(PROGRAM)s [-d file] [-i file] [-X] [-v] [-h] [initialcolor]
Barry Warsaw516f1891998-01-27 03:19:00 +000020
21Where:
Barry Warsaw04c78861998-09-28 16:28:04 +000022 --database file
23 -d file
24 Alternate location of a color database file
Barry Warsaweab81a91998-02-11 18:56:13 +000025
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000026 --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 Warsaw3fe1b141998-11-18 03:45:09 +000039 --version
40 -v
41 print the version number
42
Barry Warsaw516f1891998-01-27 03:19:00 +000043 --help
44 -h
45 print this message
46
Barry Warsaw04c78861998-09-28 16:28:04 +000047 initialcolor
48 initial color, as a color name or #RRGGBB format
Barry Warsaw516f1891998-01-27 03:19:00 +000049"""
50
Barry Warsaw60e18862001-07-10 21:42:04 +000051__version__ = '1.2'
Barry Warsaw516f1891998-01-27 03:19:00 +000052
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000053import sys
Barry Warsaw6bfd6551998-10-06 20:44:14 +000054import os
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000055import getopt
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000056import ColorDB
Barry Warsaw60e18862001-07-10 21:42:04 +000057
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000058from PyncheWidget import PyncheWidget
Barry Warsaw04c78861998-09-28 16:28:04 +000059from Switchboard import Switchboard
Barry Warsaw63c9e981998-09-28 23:41:53 +000060from StripViewer import StripViewer
Barry Warsaw4ab5d851998-10-01 16:47:06 +000061from ChipViewer import ChipViewer
62from TypeinViewer import TypeinViewer
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000063
64
65
66PROGRAM = sys.argv[0]
67
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000068# Default locations of rgb.txt or other textual color database
69RGB_TXT = [
70 # Solaris OpenWindows
71 '/usr/openwin/lib/rgb.txt',
Barry Warsawc2aadcd1999-04-23 16:24:00 +000072 # Linux
73 '/usr/lib/X11/rgb.txt',
Barry Warsaw6bfd6551998-10-06 20:44:14 +000074 # The X11R6.4 rgb.txt file
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000075 os.path.join(sys.path[0], 'X/rgb.txt'),
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000076 # add more here
77 ]
78
79
80
Barry Warsaw60e18862001-07-10 21:42:04 +000081# Do this because PyncheWidget.py wants to get at the interpolated docstring
82# too, for its Help menu.
Barry Warsawd0e1e511998-12-03 19:49:13 +000083def docstring():
Barry Warsaw60e18862001-07-10 21:42:04 +000084 return __doc__ % globals()
Barry Warsawd0e1e511998-12-03 19:49:13 +000085
86
Barry Warsaw60e18862001-07-10 21:42:04 +000087def usage(code, msg=''):
Barry Warsawd0e1e511998-12-03 19:49:13 +000088 print docstring()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000089 if msg:
Barry Warsaw3fe1b141998-11-18 03:45:09 +000090 print msg
Barry Warsaw60e18862001-07-10 21:42:04 +000091 sys.exit(code)
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000092
93
94
Barry Warsawe7f4a471998-10-06 19:50:33 +000095def 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 Warsawca07ba01998-10-22 03:25:59 +0000123def build(master=None, initialcolor=None, initfile=None, ignore=None):
Barry Warsawca07ba01998-10-22 03:25:59 +0000124 # create all output widgets
Barry Warsaw0604d721999-04-26 23:17:16 +0000125 s = Switchboard(not ignore and initfile)
126
127 # load the color database
128 colordb = None
129 try:
130 dbfile = s.optiondb()['DBFILE']
131 colordb = ColorDB.get_colordb(dbfile)
132 except (KeyError, IOError):
133 # scoot through the files listed above to try to find a usable color
134 # database file
135 for f in RGB_TXT:
136 try:
137 colordb = ColorDB.get_colordb(f)
138 if colordb:
139 break
140 except IOError:
141 pass
142 if not colordb:
143 usage(1, 'No color database file found, see the -d option.')
144 s.set_colordb(colordb)
Barry Warsawca07ba01998-10-22 03:25:59 +0000145
146 # create the application window decorations
147 app = PyncheWidget(__version__, s, master=master)
148 w = app.window()
149
Barry Warsawb61a28e1999-04-27 18:43:47 +0000150 # these built-in viewers live inside the main Pynche window
Barry Warsawca07ba01998-10-22 03:25:59 +0000151 s.add_view(StripViewer(s, w))
152 s.add_view(ChipViewer(s, w))
153 s.add_view(TypeinViewer(s, w))
154
155 # get the initial color as components and set the color on all views. if
156 # there was no initial color given on the command line, use the one that's
157 # stored in the option database
158 if initialcolor is None:
159 optiondb = s.optiondb()
160 red = optiondb.get('RED')
161 green = optiondb.get('GREEN')
162 blue = optiondb.get('BLUE')
163 # but if there wasn't any stored in the database, use grey50
164 if red is None or blue is None or green is None:
165 red, green, blue = initial_color('grey50', colordb)
166 else:
167 red, green, blue = initial_color(initialcolor, colordb)
168 s.update_views(red, green, blue)
169 return app, s
170
171
172def run(app, s):
173 try:
174 app.start()
175 except KeyboardInterrupt:
176 pass
Barry Warsawca07ba01998-10-22 03:25:59 +0000177
178
179
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000180def main():
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000181 try:
Barry Warsaw04c78861998-09-28 16:28:04 +0000182 opts, args = getopt.getopt(
183 sys.argv[1:],
Barry Warsaw3fe1b141998-11-18 03:45:09 +0000184 'hd:i:Xv',
185 ['database=', 'initfile=', 'ignore', 'help', 'version'])
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000186 except getopt.error, msg:
187 usage(1, msg)
188
Barry Warsaw04c78861998-09-28 16:28:04 +0000189 if len(args) == 0:
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000190 initialcolor = None
Barry Warsaw04c78861998-09-28 16:28:04 +0000191 elif len(args) == 1:
192 initialcolor = args[0]
193 else:
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000194 usage(1)
195
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000196 ignore = 0
197 initfile = os.path.expanduser('~/.pynche')
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000198 for opt, arg in opts:
199 if opt in ('-h', '--help'):
200 usage(0)
Barry Warsaw3fe1b141998-11-18 03:45:09 +0000201 elif opt in ('-v', '--version'):
202 print '''\
203Pynche -- The PYthon Natural Color and Hue Editor.
Barry Warsaw64039911998-12-15 01:02:51 +0000204Contact: Barry Warsaw
Barry Warsaw3fe1b141998-11-18 03:45:09 +0000205Email: bwarsaw@python.org
206Version: %s''' % __version__
207 sys.exit(0)
Barry Warsaw04c78861998-09-28 16:28:04 +0000208 elif opt in ('-d', '--database'):
209 RGB_TXT.insert(0, arg)
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000210 elif opt in ('-X', '--ignore'):
211 ignore = 1
212 elif opt in ('-i', '--initfile'):
213 initfile = arg
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000214
Barry Warsaw877d2991998-10-22 18:45:52 +0000215 app, sb = build(initialcolor=initialcolor,
216 initfile=initfile,
217 ignore=ignore)
218 run(app, sb)
219 sb.save_views()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000220
Barry Warsaw60e18862001-07-10 21:42:04 +0000221
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000222
223if __name__ == '__main__':
224 main()