blob: fca80d6fd525e22d583dc26871a2c9063c9e45f6 [file] [log] [blame]
Barry Warsaw516f1891998-01-27 03:19:00 +00001#! /usr/bin/env python
2
3"""Pynche: The PYthon Natural Color and Hue Editor.
4
5Pynche is based largely on a similar color editor I wrote years ago for the
6Sunview window system. That editor was called ICE: the Interactive Color
7Editor. I'd always wanted to port the editor to X but didn't feel like
8hacking X and C code to do it. Fast forward many years, to where Python +
Barry Warsaw04c78861998-09-28 16:28:04 +00009Tkinter provides such a nice programming environment, with enough power, that
10I finally buckled down and implemented it. I changed the name because these
11days, too many other systems have the acronym `ICE'.
Barry Warsaw516f1891998-01-27 03:19:00 +000012
Barry Warsaw04c78861998-09-28 16:28:04 +000013This program currently requires Python 1.5 with Tkinter. It has only been
14tested on Solaris 2.6. Feedback is greatly appreciated. Send email to
15bwarsaw@python.org
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000016
Barry Warsaw04c78861998-09-28 16:28:04 +000017Usage: %(PROGRAM)s [-d file] [-h] [initialcolor]
Barry Warsaw516f1891998-01-27 03:19:00 +000018
19Where:
Barry Warsaw04c78861998-09-28 16:28:04 +000020 --database file
21 -d file
22 Alternate location of a color database file
Barry Warsaweab81a91998-02-11 18:56:13 +000023
Barry Warsaw516f1891998-01-27 03:19:00 +000024 --help
25 -h
26 print this message
27
Barry Warsaw04c78861998-09-28 16:28:04 +000028 initialcolor
29 initial color, as a color name or #RRGGBB format
30
Barry Warsaw516f1891998-01-27 03:19:00 +000031"""
32
33__version__ = '1.0'
34
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000035import sys
36import getopt
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000037import ColorDB
38from Tkinter import *
39from PyncheWidget import PyncheWidget
Barry Warsaw04c78861998-09-28 16:28:04 +000040from Switchboard import Switchboard
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000041
42
43
44PROGRAM = sys.argv[0]
45
46# Milliseconds between interrupt checks
47KEEPALIVE_TIMER = 500
48
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000049# Default locations of rgb.txt or other textual color database
50RGB_TXT = [
51 # Solaris OpenWindows
52 '/usr/openwin/lib/rgb.txt',
53 # add more here
54 ]
55
56
57
58def usage(status, msg=''):
59 if msg:
60 print msg
61 print __doc__ % globals()
62 sys.exit(status)
63
64
65
66app = None
67
68def keepalive():
Barry Warsaw04c78861998-09-28 16:28:04 +000069 # Exercise the Python interpreter regularly so keyboard interrupts get
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000070 # through.
71 app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive)
72
73
Barry Warsaw04c78861998-09-28 16:28:04 +000074def finished(event=None):
75 sys.exit(0)
76
77
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000078def main():
79 global app
80
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000081 try:
Barry Warsaw04c78861998-09-28 16:28:04 +000082 opts, args = getopt.getopt(
83 sys.argv[1:],
84 'hd:',
85 ['database=', 'help'])
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000086 except getopt.error, msg:
87 usage(1, msg)
88
Barry Warsaw04c78861998-09-28 16:28:04 +000089 if len(args) == 0:
90 initialcolor = 'grey50'
91 elif len(args) == 1:
92 initialcolor = args[0]
93 else:
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000094 usage(1)
95
96 for opt, arg in opts:
97 if opt in ('-h', '--help'):
98 usage(0)
Barry Warsaw04c78861998-09-28 16:28:04 +000099 elif opt in ('-d', '--database'):
100 RGB_TXT.insert(0, arg)
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000101
102 # create the windows and go
103 for f in RGB_TXT:
104 try:
105 colordb = ColorDB.get_colordb(f)
106 break
107 except IOError:
108 pass
109 else:
110 raise IOError('No color database file found')
111
Barry Warsaw04c78861998-09-28 16:28:04 +0000112 app = Tk(className='Pynche')
113 app.protocol('WM_DELETE_WINDOW', finished)
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000114 app.title('Pynche %s' % __version__)
Barry Warsaw04c78861998-09-28 16:28:04 +0000115 app.iconname('Pynche')
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000116 app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive)
Barry Warsaweab81a91998-02-11 18:56:13 +0000117
118 # get triplet for initial color
119 try:
120 red, green, blue = colordb.find_byname(initialcolor)
121 except ColorDB.BadColor:
122 # must be a #rrggbb style color
123 try:
124 red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor)
125 except ColorDB.BadColor:
Barry Warsaw04c78861998-09-28 16:28:04 +0000126 print 'Bad initial color, using default: %s' % initialcolor
127 initialcolor = 'grey50'
128 try:
129 red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor)
130 except ColorDB.BadColor:
131 usage(1, 'Cannot find an initial color to use')
Barry Warsaweab81a91998-02-11 18:56:13 +0000132
Barry Warsaw04c78861998-09-28 16:28:04 +0000133 s = Switchboard(app, colordb, red, green, blue)
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000134 try:
135 keepalive()
136 app.mainloop()
137 except KeyboardInterrupt:
138 pass
139
140
141
142if __name__ == '__main__':
143 main()