blob: 4877adc6b037beedd0d40ef951b691adf2a4e1fa [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
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000038from PyncheWidget import PyncheWidget
Barry Warsaw04c78861998-09-28 16:28:04 +000039from Switchboard import Switchboard
Barry Warsaw63c9e981998-09-28 23:41:53 +000040from StripViewer import StripViewer
Barry Warsaw4ab5d851998-10-01 16:47:06 +000041from ChipViewer import ChipViewer
42from TypeinViewer import TypeinViewer
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000043
44
45
46PROGRAM = sys.argv[0]
47
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000048# Default locations of rgb.txt or other textual color database
49RGB_TXT = [
50 # Solaris OpenWindows
51 '/usr/openwin/lib/rgb.txt',
52 # add more here
53 ]
54
55
56
57def usage(status, msg=''):
Barry Warsaw14e2caf1998-10-06 15:21:04 +000058 print __doc__ % globals()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000059 if msg:
60 print msg
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000061 sys.exit(status)
62
63
64
Barry Warsawe7f4a471998-10-06 19:50:33 +000065def initial_color(s, colordb):
66 # function called on every color
67 def scan_color(s, colordb=colordb):
68 try:
69 r, g, b = colordb.find_byname(s)
70 except ColorDB.BadColor:
71 try:
72 r, g, b = ColorDB.rrggbb_to_triplet(s)
73 except ColorDB.BadColor:
74 return None, None, None
75 return r, g, b
76 #
77 # First try the passed in color
78 r, g, b = scan_color(s)
79 if r is None:
80 # try the same color with '#' prepended, since some shells require
81 # this to be escaped, which is a pain
82 r, g, b = scan_color('#' + s)
83 if r is None:
84 print 'Bad initial color, using gray50:', s
85 r, g, b = scan_color('gray50')
86 if r is None:
87 usage(1, 'Cannot find an initial color to use')
88 # does not return
89 return r, g, b
90
91
92
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000093def main():
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000094 try:
Barry Warsaw04c78861998-09-28 16:28:04 +000095 opts, args = getopt.getopt(
96 sys.argv[1:],
97 'hd:',
98 ['database=', 'help'])
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000099 except getopt.error, msg:
100 usage(1, msg)
101
Barry Warsaw04c78861998-09-28 16:28:04 +0000102 if len(args) == 0:
103 initialcolor = 'grey50'
104 elif len(args) == 1:
105 initialcolor = args[0]
106 else:
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000107 usage(1)
108
109 for opt, arg in opts:
110 if opt in ('-h', '--help'):
111 usage(0)
Barry Warsaw04c78861998-09-28 16:28:04 +0000112 elif opt in ('-d', '--database'):
113 RGB_TXT.insert(0, arg)
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000114
115 # create the windows and go
116 for f in RGB_TXT:
117 try:
118 colordb = ColorDB.get_colordb(f)
Barry Warsaw14e2caf1998-10-06 15:21:04 +0000119 if colordb:
120 break
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000121 except IOError:
122 pass
123 else:
Barry Warsaw14e2caf1998-10-06 15:21:04 +0000124 usage(1, 'No color database file found, see the -d option.')
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000125
Barry Warsawe7f4a471998-10-06 19:50:33 +0000126 # get the initial color as components
127 red, green, blue = initial_color(initialcolor, colordb)
Barry Warsaweab81a91998-02-11 18:56:13 +0000128
Barry Warsaw63c9e981998-09-28 23:41:53 +0000129 # create all output widgets
Barry Warsaw4ab5d851998-10-01 16:47:06 +0000130 s = Switchboard(colordb)
131
132 # create the application window decorations
133 app = PyncheWidget(__version__, s)
134 parent = app.parent()
135
Barry Warsaw63c9e981998-09-28 23:41:53 +0000136 s.add_view(StripViewer(s, parent))
Barry Warsaw4ab5d851998-10-01 16:47:06 +0000137 s.add_view(ChipViewer(s, parent))
138 s.add_view(TypeinViewer(s, parent))
Barry Warsaw63c9e981998-09-28 23:41:53 +0000139 s.update_views(red, green, blue)
140
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000141 try:
Barry Warsaw63c9e981998-09-28 23:41:53 +0000142 app.start()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000143 except KeyboardInterrupt:
144 pass
145
146
147
148if __name__ == '__main__':
149 main()