blob: bbf5533881b598ee1d797831b618d4e8934d3a3f [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
Barry Warsaw6bfd6551998-10-06 20:44:14 +000036import os
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000037import getopt
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000038import ColorDB
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000039from PyncheWidget import PyncheWidget
Barry Warsaw04c78861998-09-28 16:28:04 +000040from Switchboard import Switchboard
Barry Warsaw63c9e981998-09-28 23:41:53 +000041from StripViewer import StripViewer
Barry Warsaw4ab5d851998-10-01 16:47:06 +000042from ChipViewer import ChipViewer
43from TypeinViewer import TypeinViewer
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000044
45
46
47PROGRAM = sys.argv[0]
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',
Barry Warsaw6bfd6551998-10-06 20:44:14 +000053 # The X11R6.4 rgb.txt file
54 os.path.join(sys.path[0], 'rgb.txt'),
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000055 # add more here
56 ]
57
58
59
60def usage(status, msg=''):
Barry Warsaw14e2caf1998-10-06 15:21:04 +000061 print __doc__ % globals()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000062 if msg:
63 print msg
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000064 sys.exit(status)
65
66
67
Barry Warsawe7f4a471998-10-06 19:50:33 +000068def initial_color(s, colordb):
69 # function called on every color
70 def scan_color(s, colordb=colordb):
71 try:
72 r, g, b = colordb.find_byname(s)
73 except ColorDB.BadColor:
74 try:
75 r, g, b = ColorDB.rrggbb_to_triplet(s)
76 except ColorDB.BadColor:
77 return None, None, None
78 return r, g, b
79 #
80 # First try the passed in color
81 r, g, b = scan_color(s)
82 if r is None:
83 # try the same color with '#' prepended, since some shells require
84 # this to be escaped, which is a pain
85 r, g, b = scan_color('#' + s)
86 if r is None:
87 print 'Bad initial color, using gray50:', s
88 r, g, b = scan_color('gray50')
89 if r is None:
90 usage(1, 'Cannot find an initial color to use')
91 # does not return
92 return r, g, b
93
94
95
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000096def main():
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000097 try:
Barry Warsaw04c78861998-09-28 16:28:04 +000098 opts, args = getopt.getopt(
99 sys.argv[1:],
100 'hd:',
101 ['database=', 'help'])
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000102 except getopt.error, msg:
103 usage(1, msg)
104
Barry Warsaw04c78861998-09-28 16:28:04 +0000105 if len(args) == 0:
106 initialcolor = 'grey50'
107 elif len(args) == 1:
108 initialcolor = args[0]
109 else:
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000110 usage(1)
111
112 for opt, arg in opts:
113 if opt in ('-h', '--help'):
114 usage(0)
Barry Warsaw04c78861998-09-28 16:28:04 +0000115 elif opt in ('-d', '--database'):
116 RGB_TXT.insert(0, arg)
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000117
118 # create the windows and go
119 for f in RGB_TXT:
120 try:
121 colordb = ColorDB.get_colordb(f)
Barry Warsaw14e2caf1998-10-06 15:21:04 +0000122 if colordb:
123 break
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000124 except IOError:
125 pass
126 else:
Barry Warsaw14e2caf1998-10-06 15:21:04 +0000127 usage(1, 'No color database file found, see the -d option.')
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000128
Barry Warsawe7f4a471998-10-06 19:50:33 +0000129 # get the initial color as components
130 red, green, blue = initial_color(initialcolor, colordb)
Barry Warsaweab81a91998-02-11 18:56:13 +0000131
Barry Warsaw63c9e981998-09-28 23:41:53 +0000132 # create all output widgets
Barry Warsaw4ab5d851998-10-01 16:47:06 +0000133 s = Switchboard(colordb)
134
135 # create the application window decorations
136 app = PyncheWidget(__version__, s)
137 parent = app.parent()
138
Barry Warsaw63c9e981998-09-28 23:41:53 +0000139 s.add_view(StripViewer(s, parent))
Barry Warsaw4ab5d851998-10-01 16:47:06 +0000140 s.add_view(ChipViewer(s, parent))
141 s.add_view(TypeinViewer(s, parent))
Barry Warsaw63c9e981998-09-28 23:41:53 +0000142 s.update_views(red, green, blue)
143
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000144 try:
Barry Warsaw63c9e981998-09-28 23:41:53 +0000145 app.start()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000146 except KeyboardInterrupt:
147 pass
148
149
150
151if __name__ == '__main__':
152 main()