blob: 1007838fb8363c6551752d5624cf8349c26eac22 [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 +
9Tkinter + Pmw provides such a nice programming environment, with enough
10power, that I finally buckled down and implemented it. I changed the name
11because these days, too many other systems have the acronym `ICE'.
12
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000013This program currently requires Python 1.5 with Tkinter. It also requires at
14least Pmw 0.6.1. It has only been tested on Solaris 2.6. Feedback is greatly
15appreciated. Send email to bwarsaw@python.org
16
Barry Warsaweab81a91998-02-11 18:56:13 +000017Usage: %(PROGRAM)s [-c color] [-h]
Barry Warsaw516f1891998-01-27 03:19:00 +000018
19Where:
Barry Warsaweab81a91998-02-11 18:56:13 +000020 --color color
21 -c color
22 initial color, as an X color name or #RRGGBB format
23
Barry Warsaw516f1891998-01-27 03:19:00 +000024 --help
25 -h
26 print this message
27
28"""
29
30__version__ = '1.0'
31
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000032import sys
33import getopt
34import Pmw
35import ColorDB
36from Tkinter import *
37from PyncheWidget import PyncheWidget
38
39
40
41PROGRAM = sys.argv[0]
42
43# Milliseconds between interrupt checks
44KEEPALIVE_TIMER = 500
45
46RGBCOLOR = 1
47HSICOLOR = 2
48NAMEDCOLOR = 3
49
50# Default locations of rgb.txt or other textual color database
51RGB_TXT = [
52 # Solaris OpenWindows
53 '/usr/openwin/lib/rgb.txt',
54 # add more here
55 ]
56
57
58
59def usage(status, msg=''):
60 if msg:
61 print msg
62 print __doc__ % globals()
63 sys.exit(status)
64
65
66
67app = None
68
69def keepalive():
70 # Exercise the Python interpreter regularly so keybard interrupts get
71 # through.
72 app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive)
73
74
75def main():
76 global app
77
Barry Warsaweab81a91998-02-11 18:56:13 +000078 initialcolor = (128, 128, 128)
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000079 try:
80 opts, args = getopt.getopt(sys.argv[1:],
81 'hc:',
82 ['color=', 'help'])
83 except getopt.error, msg:
84 usage(1, msg)
85
86 if args:
87 usage(1)
88
89 for opt, arg in opts:
90 if opt in ('-h', '--help'):
91 usage(0)
92 elif opt in ('-c', '--color'):
93 initialcolor = arg
94
95 # create the windows and go
96 for f in RGB_TXT:
97 try:
98 colordb = ColorDB.get_colordb(f)
99 break
100 except IOError:
101 pass
102 else:
103 raise IOError('No color database file found')
104
105 app = Pmw.initialise(fontScheme='pmw1')
106 app.title('Pynche %s' % __version__)
107 app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive)
Barry Warsaweab81a91998-02-11 18:56:13 +0000108
109 # get triplet for initial color
110 try:
111 red, green, blue = colordb.find_byname(initialcolor)
112 except ColorDB.BadColor:
113 # must be a #rrggbb style color
114 try:
115 red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor)
116 except ColorDB.BadColor:
117 usage(1, 'Bad initial color: %s' % initialcolor)
118
119 p = PyncheWidget(colordb, app, color=(red, green, blue))
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000120 try:
121 keepalive()
122 app.mainloop()
123 except KeyboardInterrupt:
124 pass
125
126
127
128if __name__ == '__main__':
129 main()