blob: f266a8d184fec2e0ebca3c1a66e31a0b6e5b0bfc [file] [log] [blame]
Barry Warsaw516f1891998-01-27 03:19:00 +00001"""Pynche: The PYthon Natural Color and Hue Editor.
2
3Pynche is based largely on a similar color editor I wrote years ago for the
4Sunview window system. That editor was called ICE: the Interactive Color
5Editor. I'd always wanted to port the editor to X but didn't feel like
6hacking X and C code to do it. Fast forward many years, to where Python +
Barry Warsaw04c78861998-09-28 16:28:04 +00007Tkinter provides such a nice programming environment, with enough power, that
8I finally buckled down and implemented it. I changed the name because these
9days, too many other systems have the acronym `ICE'.
Barry Warsaw516f1891998-01-27 03:19:00 +000010
Barry Warsaw04c78861998-09-28 16:28:04 +000011This program currently requires Python 1.5 with Tkinter. It has only been
12tested on Solaris 2.6. Feedback is greatly appreciated. Send email to
13bwarsaw@python.org
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000014
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000015Usage: %(PROGRAM)s [-d file] [-i file] [-X] [-h] [initialcolor]
Barry Warsaw516f1891998-01-27 03:19:00 +000016
17Where:
Barry Warsaw04c78861998-09-28 16:28:04 +000018 --database file
19 -d file
20 Alternate location of a color database file
Barry Warsaweab81a91998-02-11 18:56:13 +000021
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000022 --initfile file
23 -i file
24 Alternate location of the initialization file. This file contains a
25 persistent database of the current Pynche options and color. This
26 means that Pynche restores its option settings and current color when
27 it restarts, using this file (unless the -X option is used). The
28 default is ~/.pynche
29
30 --ignore
31 -X
32 Ignore the initialization file when starting up. Pynche will still
33 write the current option settings to this file when it quits.
34
Barry Warsaw516f1891998-01-27 03:19:00 +000035 --help
36 -h
37 print this message
38
Barry Warsaw04c78861998-09-28 16:28:04 +000039 initialcolor
40 initial color, as a color name or #RRGGBB format
41
Barry Warsaw516f1891998-01-27 03:19:00 +000042"""
43
44__version__ = '1.0'
45
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000046import sys
Barry Warsaw6bfd6551998-10-06 20:44:14 +000047import os
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000048import getopt
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000049import ColorDB
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000050from PyncheWidget import PyncheWidget
Barry Warsaw04c78861998-09-28 16:28:04 +000051from Switchboard import Switchboard
Barry Warsaw63c9e981998-09-28 23:41:53 +000052from StripViewer import StripViewer
Barry Warsaw4ab5d851998-10-01 16:47:06 +000053from ChipViewer import ChipViewer
54from TypeinViewer import TypeinViewer
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000055
56
57
58PROGRAM = sys.argv[0]
59
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000060# Default locations of rgb.txt or other textual color database
61RGB_TXT = [
62 # Solaris OpenWindows
63 '/usr/openwin/lib/rgb.txt',
Barry Warsaw6bfd6551998-10-06 20:44:14 +000064 # The X11R6.4 rgb.txt file
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000065 os.path.join(sys.path[0], 'X/rgb.txt'),
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000066 # add more here
67 ]
68
69
70
71def usage(status, msg=''):
Barry Warsaw14e2caf1998-10-06 15:21:04 +000072 print __doc__ % globals()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000073 if msg:
74 print msg
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000075 sys.exit(status)
76
77
78
Barry Warsawe7f4a471998-10-06 19:50:33 +000079def initial_color(s, colordb):
80 # function called on every color
81 def scan_color(s, colordb=colordb):
82 try:
83 r, g, b = colordb.find_byname(s)
84 except ColorDB.BadColor:
85 try:
86 r, g, b = ColorDB.rrggbb_to_triplet(s)
87 except ColorDB.BadColor:
88 return None, None, None
89 return r, g, b
90 #
91 # First try the passed in color
92 r, g, b = scan_color(s)
93 if r is None:
94 # try the same color with '#' prepended, since some shells require
95 # this to be escaped, which is a pain
96 r, g, b = scan_color('#' + s)
97 if r is None:
98 print 'Bad initial color, using gray50:', s
99 r, g, b = scan_color('gray50')
100 if r is None:
101 usage(1, 'Cannot find an initial color to use')
102 # does not return
103 return r, g, b
104
105
106
Barry Warsawca07ba01998-10-22 03:25:59 +0000107def build(master=None, initialcolor=None, initfile=None, ignore=None):
108 # create the windows and go
109 for f in RGB_TXT:
110 try:
111 colordb = ColorDB.get_colordb(f)
112 if colordb:
113 break
114 except IOError:
115 pass
116 else:
117 usage(1, 'No color database file found, see the -d option.')
118
119 # create all output widgets
120 s = Switchboard(colordb, not ignore and initfile)
121
122 # create the application window decorations
123 app = PyncheWidget(__version__, s, master=master)
124 w = app.window()
125
126 s.add_view(StripViewer(s, w))
127 s.add_view(ChipViewer(s, w))
128 s.add_view(TypeinViewer(s, w))
129
130 # get the initial color as components and set the color on all views. if
131 # there was no initial color given on the command line, use the one that's
132 # stored in the option database
133 if initialcolor is None:
134 optiondb = s.optiondb()
135 red = optiondb.get('RED')
136 green = optiondb.get('GREEN')
137 blue = optiondb.get('BLUE')
138 # but if there wasn't any stored in the database, use grey50
139 if red is None or blue is None or green is None:
140 red, green, blue = initial_color('grey50', colordb)
141 else:
142 red, green, blue = initial_color(initialcolor, colordb)
143 s.update_views(red, green, blue)
144 return app, s
145
146
147def run(app, s):
148 try:
149 app.start()
150 except KeyboardInterrupt:
151 pass
152 # save the option database
153 s.save_views()
154
155
156
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000157def main():
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000158 try:
Barry Warsaw04c78861998-09-28 16:28:04 +0000159 opts, args = getopt.getopt(
160 sys.argv[1:],
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000161 'hd:i:X',
162 ['database=', 'initfile=', 'ignore', 'help'])
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000163 except getopt.error, msg:
164 usage(1, msg)
165
Barry Warsaw04c78861998-09-28 16:28:04 +0000166 if len(args) == 0:
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000167 initialcolor = None
Barry Warsaw04c78861998-09-28 16:28:04 +0000168 elif len(args) == 1:
169 initialcolor = args[0]
170 else:
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000171 usage(1)
172
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000173 ignore = 0
174 initfile = os.path.expanduser('~/.pynche')
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000175 for opt, arg in opts:
176 if opt in ('-h', '--help'):
177 usage(0)
Barry Warsaw04c78861998-09-28 16:28:04 +0000178 elif opt in ('-d', '--database'):
179 RGB_TXT.insert(0, arg)
Barry Warsaw8a09e1c1998-10-20 20:45:46 +0000180 elif opt in ('-X', '--ignore'):
181 ignore = 1
182 elif opt in ('-i', '--initfile'):
183 initfile = arg
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000184
Barry Warsawca07ba01998-10-22 03:25:59 +0000185 run()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000186
187
188if __name__ == '__main__':
189 main()