blob: 320b50320a1b93c5d2641fab00803b6f0490a79c [file] [log] [blame]
Barry Warsaw3fe1b141998-11-18 03:45:09 +00001"""Pynche -- The PYthon Natural Color and Hue Editor.
2
3Author: Barry Warsaw
4Email: 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
49
Barry Warsaw516f1891998-01-27 03:19:00 +000050"""
51
Barry Warsaw93990de1998-11-18 03:09:18 +000052__version__ = '0.1'
Barry Warsaw516f1891998-01-27 03:19:00 +000053
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000054import sys
Barry Warsaw6bfd6551998-10-06 20:44:14 +000055import os
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000056import getopt
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000057import ColorDB
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 Warsaw6bfd6551998-10-06 20:44:14 +000072 # The X11R6.4 rgb.txt file
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000073 os.path.join(sys.path[0], 'X/rgb.txt'),
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000074 # add more here
75 ]
76
77
78
79def usage(status, msg=''):
Barry Warsaw14e2caf1998-10-06 15:21:04 +000080 print __doc__ % globals()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000081 if msg:
Barry Warsaw3fe1b141998-11-18 03:45:09 +000082 print msg
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000083 sys.exit(status)
84
85
86
Barry Warsawe7f4a471998-10-06 19:50:33 +000087def initial_color(s, colordb):
88 # function called on every color
89 def scan_color(s, colordb=colordb):
90 try:
91 r, g, b = colordb.find_byname(s)
92 except ColorDB.BadColor:
93 try:
94 r, g, b = ColorDB.rrggbb_to_triplet(s)
95 except ColorDB.BadColor:
96 return None, None, None
97 return r, g, b
98 #
99 # First try the passed in color
100 r, g, b = scan_color(s)
101 if r is None:
102 # try the same color with '#' prepended, since some shells require
103 # this to be escaped, which is a pain
104 r, g, b = scan_color('#' + s)
105 if r is None:
106 print 'Bad initial color, using gray50:', s
107 r, g, b = scan_color('gray50')
108 if r is None:
109 usage(1, 'Cannot find an initial color to use')
110 # does not return
111 return r, g, b
112
113
114
Barry Warsawca07ba01998-10-22 03:25:59 +0000115def build(master=None, initialcolor=None, initfile=None, ignore=None):
116 # create the windows and go
117 for f in RGB_TXT:
118 try:
119 colordb = ColorDB.get_colordb(f)
120 if colordb:
121 break
122 except IOError:
123 pass
124 else:
125 usage(1, 'No color database file found, see the -d option.')
126
127 # create all output widgets
128 s = Switchboard(colordb, not ignore and initfile)
129
130 # create the application window decorations
131 app = PyncheWidget(__version__, s, master=master)
132 w = app.window()
133
134 s.add_view(StripViewer(s, w))
135 s.add_view(ChipViewer(s, w))
136 s.add_view(TypeinViewer(s, w))
137
138 # get the initial color as components and set the color on all views. if
139 # there was no initial color given on the command line, use the one that's
140 # stored in the option database
141 if initialcolor is None:
142 optiondb = s.optiondb()
143 red = optiondb.get('RED')
144 green = optiondb.get('GREEN')
145 blue = optiondb.get('BLUE')
146 # but if there wasn't any stored in the database, use grey50
147 if red is None or blue is None or green is None:
148 red, green, blue = initial_color('grey50', colordb)
149 else:
150 red, green, blue = initial_color(initialcolor, colordb)
151 s.update_views(red, green, blue)
152 return app, s
153
154
155def run(app, s):
156 try:
157 app.start()
158 except KeyboardInterrupt:
159 pass
Barry Warsawca07ba01998-10-22 03:25:59 +0000160
161
162
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000163def main():
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000164 try:
Barry Warsaw04c78861998-09-28 16:28:04 +0000165 opts, args = getopt.getopt(
166 sys.argv[1:],
Barry Warsaw3fe1b141998-11-18 03:45:09 +0000167 'hd:i:Xv',
168 ['database=', 'initfile=', 'ignore', 'help', 'version'])
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000169 except getopt.error, msg:
170 usage(1, msg)
171
Barry Warsaw04c78861998-09-28 16:28:04 +0000172 if len(args) == 0:
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000173 initialcolor = None
Barry Warsaw04c78861998-09-28 16:28:04 +0000174 elif len(args) == 1:
175 initialcolor = args[0]
176 else:
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000177 usage(1)
178
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000179 ignore = 0
180 initfile = os.path.expanduser('~/.pynche')
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000181 for opt, arg in opts:
182 if opt in ('-h', '--help'):
183 usage(0)
Barry Warsaw3fe1b141998-11-18 03:45:09 +0000184 elif opt in ('-v', '--version'):
185 print '''\
186Pynche -- The PYthon Natural Color and Hue Editor.
187Author: Barry Warsaw
188Email: bwarsaw@python.org
189Version: %s''' % __version__
190 sys.exit(0)
Barry Warsaw04c78861998-09-28 16:28:04 +0000191 elif opt in ('-d', '--database'):
192 RGB_TXT.insert(0, arg)
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000193 elif opt in ('-X', '--ignore'):
194 ignore = 1
195 elif opt in ('-i', '--initfile'):
196 initfile = arg
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000197
Barry Warsaw877d2991998-10-22 18:45:52 +0000198 app, sb = build(initialcolor=initialcolor,
199 initfile=initfile,
200 ignore=ignore)
201 run(app, sb)
202 sb.save_views()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000203
204
205if __name__ == '__main__':
206 main()