blob: b86bea235da35fa427d1b73ff8a71f4f4a559619 [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 Warsaw04c78861998-09-28 16:28:04 +000015Usage: %(PROGRAM)s [-d file] [-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 Warsaw516f1891998-01-27 03:19:00 +000022 --help
23 -h
24 print this message
25
Barry Warsaw04c78861998-09-28 16:28:04 +000026 initialcolor
27 initial color, as a color name or #RRGGBB format
28
Barry Warsaw516f1891998-01-27 03:19:00 +000029"""
30
31__version__ = '1.0'
32
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000033import sys
Barry Warsaw6bfd6551998-10-06 20:44:14 +000034import os
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000035import getopt
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000036import ColorDB
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000037from PyncheWidget import PyncheWidget
Barry Warsaw04c78861998-09-28 16:28:04 +000038from Switchboard import Switchboard
Barry Warsaw63c9e981998-09-28 23:41:53 +000039from StripViewer import StripViewer
Barry Warsaw4ab5d851998-10-01 16:47:06 +000040from ChipViewer import ChipViewer
41from TypeinViewer import TypeinViewer
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000042
43
44
45PROGRAM = sys.argv[0]
46
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000047# Default locations of rgb.txt or other textual color database
48RGB_TXT = [
49 # Solaris OpenWindows
50 '/usr/openwin/lib/rgb.txt',
Barry Warsaw6bfd6551998-10-06 20:44:14 +000051 # The X11R6.4 rgb.txt file
52 os.path.join(sys.path[0], 'rgb.txt'),
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000053 # add more here
54 ]
55
56
57
58def usage(status, msg=''):
Barry Warsaw14e2caf1998-10-06 15:21:04 +000059 print __doc__ % globals()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000060 if msg:
61 print msg
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000062 sys.exit(status)
63
64
65
Barry Warsawe7f4a471998-10-06 19:50:33 +000066def initial_color(s, colordb):
67 # function called on every color
68 def scan_color(s, colordb=colordb):
69 try:
70 r, g, b = colordb.find_byname(s)
71 except ColorDB.BadColor:
72 try:
73 r, g, b = ColorDB.rrggbb_to_triplet(s)
74 except ColorDB.BadColor:
75 return None, None, None
76 return r, g, b
77 #
78 # First try the passed in color
79 r, g, b = scan_color(s)
80 if r is None:
81 # try the same color with '#' prepended, since some shells require
82 # this to be escaped, which is a pain
83 r, g, b = scan_color('#' + s)
84 if r is None:
85 print 'Bad initial color, using gray50:', s
86 r, g, b = scan_color('gray50')
87 if r is None:
88 usage(1, 'Cannot find an initial color to use')
89 # does not return
90 return r, g, b
91
92
93
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000094def main():
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000095 try:
Barry Warsaw04c78861998-09-28 16:28:04 +000096 opts, args = getopt.getopt(
97 sys.argv[1:],
98 'hd:',
99 ['database=', 'help'])
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000100 except getopt.error, msg:
101 usage(1, msg)
102
Barry Warsaw04c78861998-09-28 16:28:04 +0000103 if len(args) == 0:
104 initialcolor = 'grey50'
105 elif len(args) == 1:
106 initialcolor = args[0]
107 else:
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000108 usage(1)
109
110 for opt, arg in opts:
111 if opt in ('-h', '--help'):
112 usage(0)
Barry Warsaw04c78861998-09-28 16:28:04 +0000113 elif opt in ('-d', '--database'):
114 RGB_TXT.insert(0, arg)
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000115
116 # create the windows and go
117 for f in RGB_TXT:
118 try:
119 colordb = ColorDB.get_colordb(f)
Barry Warsaw14e2caf1998-10-06 15:21:04 +0000120 if colordb:
121 break
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000122 except IOError:
123 pass
124 else:
Barry Warsaw14e2caf1998-10-06 15:21:04 +0000125 usage(1, 'No color database file found, see the -d option.')
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000126
Barry Warsawe7f4a471998-10-06 19:50:33 +0000127 # get the initial color as components
128 red, green, blue = initial_color(initialcolor, colordb)
Barry Warsaweab81a91998-02-11 18:56:13 +0000129
Barry Warsaw63c9e981998-09-28 23:41:53 +0000130 # create all output widgets
Barry Warsaw4ab5d851998-10-01 16:47:06 +0000131 s = Switchboard(colordb)
132
133 # create the application window decorations
134 app = PyncheWidget(__version__, s)
135 parent = app.parent()
136
Barry Warsaw63c9e981998-09-28 23:41:53 +0000137 s.add_view(StripViewer(s, parent))
Barry Warsaw4ab5d851998-10-01 16:47:06 +0000138 s.add_view(ChipViewer(s, parent))
139 s.add_view(TypeinViewer(s, parent))
Barry Warsaw63c9e981998-09-28 23:41:53 +0000140 s.update_views(red, green, blue)
141
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000142 try:
Barry Warsaw63c9e981998-09-28 23:41:53 +0000143 app.start()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000144 except KeyboardInterrupt:
145 pass
146
147
148
149if __name__ == '__main__':
150 main()