blob: 7fcc877dee6ba371681e065f82981d240f48e34c [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 Warsawf7c1e5a1998-01-31 23:39:28 +000041
42
43
44PROGRAM = sys.argv[0]
45
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000046# Default locations of rgb.txt or other textual color database
47RGB_TXT = [
48 # Solaris OpenWindows
49 '/usr/openwin/lib/rgb.txt',
50 # add more here
51 ]
52
53
54
55def usage(status, msg=''):
56 if msg:
57 print msg
58 print __doc__ % globals()
59 sys.exit(status)
60
61
62
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000063def main():
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000064 try:
Barry Warsaw04c78861998-09-28 16:28:04 +000065 opts, args = getopt.getopt(
66 sys.argv[1:],
67 'hd:',
68 ['database=', 'help'])
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000069 except getopt.error, msg:
70 usage(1, msg)
71
Barry Warsaw04c78861998-09-28 16:28:04 +000072 if len(args) == 0:
73 initialcolor = 'grey50'
74 elif len(args) == 1:
75 initialcolor = args[0]
76 else:
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000077 usage(1)
78
79 for opt, arg in opts:
80 if opt in ('-h', '--help'):
81 usage(0)
Barry Warsaw04c78861998-09-28 16:28:04 +000082 elif opt in ('-d', '--database'):
83 RGB_TXT.insert(0, arg)
Barry Warsawf7c1e5a1998-01-31 23:39:28 +000084
85 # create the windows and go
86 for f in RGB_TXT:
87 try:
88 colordb = ColorDB.get_colordb(f)
89 break
90 except IOError:
91 pass
92 else:
93 raise IOError('No color database file found')
94
Barry Warsaw63c9e981998-09-28 23:41:53 +000095 # create the application window decorations
96 app = PyncheWidget(__version__)
97 parent = app.parent()
Barry Warsaweab81a91998-02-11 18:56:13 +000098
99 # get triplet for initial color
100 try:
101 red, green, blue = colordb.find_byname(initialcolor)
102 except ColorDB.BadColor:
103 # must be a #rrggbb style color
104 try:
105 red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor)
106 except ColorDB.BadColor:
Barry Warsaw04c78861998-09-28 16:28:04 +0000107 print 'Bad initial color, using default: %s' % initialcolor
108 initialcolor = 'grey50'
109 try:
110 red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor)
111 except ColorDB.BadColor:
112 usage(1, 'Cannot find an initial color to use')
Barry Warsaweab81a91998-02-11 18:56:13 +0000113
Barry Warsaw63c9e981998-09-28 23:41:53 +0000114 # create all output widgets
115 s = Switchboard()
116 s.add_view(StripViewer(s, parent))
117 s.update_views(red, green, blue)
118
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000119 try:
Barry Warsaw63c9e981998-09-28 23:41:53 +0000120 app.start()
Barry Warsawf7c1e5a1998-01-31 23:39:28 +0000121 except KeyboardInterrupt:
122 pass
123
124
125
126if __name__ == '__main__':
127 main()