summaryrefslogtreecommitdiffstats
path: root/Tools/pynche/Main.py
blob: e95c071f6f49c22b24354ec3da965ea07c2d2dfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#! /usr/bin/env python

"""Pynche: The PYthon Natural Color and Hue Editor.

Pynche is based largely on a similar color editor I wrote years ago for the
Sunview window system.  That editor was called ICE: the Interactive Color
Editor.  I'd always wanted to port the editor to X but didn't feel like
hacking X and C code to do it.  Fast forward many years, to where Python +
Tkinter + Pmw provides such a nice programming environment, with enough
power, that I finally buckled down and implemented it.  I changed the name
because these days, too many other systems have the acronym `ICE'.

This program currently requires Python 1.5 with Tkinter.  It also requires at
least Pmw 0.6.1.  It has only been tested on Solaris 2.6.  Feedback is greatly 
appreciated.  Send email to bwarsaw@python.org

Usage: %(PROGRAM)s [-c color] [-h]

Where:
    --color color
    -c color
        initial color, as an X color name or #RRGGBB format

    --help
    -h
        print this message

"""

__version__ = '1.0'

import sys
import getopt
import Pmw
import ColorDB
from Tkinter import *
from PyncheWidget import PyncheWidget



PROGRAM = sys.argv[0]

# Milliseconds between interrupt checks
KEEPALIVE_TIMER = 500

RGBCOLOR = 1
HSICOLOR = 2
NAMEDCOLOR = 3

# Default locations of rgb.txt or other textual color database
RGB_TXT = [
    # Solaris OpenWindows
    '/usr/openwin/lib/rgb.txt',
    # add more here
    ]



def usage(status, msg=''):
    if msg:
	print msg
    print __doc__ % globals()
    sys.exit(status)



app = None

def keepalive():
    # Exercise the Python interpreter regularly so keybard interrupts get
    # through.
    app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive)


def main():
    global app

    initialcolor = 'grey50'
    try:
	opts, args = getopt.getopt(sys.argv[1:],
				   'hc:',
				   ['color=', 'help'])
    except getopt.error, msg:
	usage(1, msg)

    if args:
	usage(1)

    for opt, arg in opts:
	if opt in ('-h', '--help'):
	    usage(0)
	elif opt in ('-c', '--color'):
	    initialcolor = arg

    # create the windows and go
    for f in RGB_TXT:
	try:
	    colordb = ColorDB.get_colordb(f)
	    break
	except IOError:
	    pass
    else:
	raise IOError('No color database file found')

    app = Pmw.initialise(fontScheme='pmw1')
    app.title('Pynche %s' % __version__)
    app.tk.createtimerhandler(KEEPALIVE_TIMER, keepalive)

    # get triplet for initial color
    try:
	red, green, blue = colordb.find_byname(initialcolor)
    except ColorDB.BadColor:
	# must be a #rrggbb style color
	try:
	    red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor)
	except ColorDB.BadColor:
	    usage(1, 'Bad initial color: %s' % initialcolor)

    p = PyncheWidget(colordb, app, color=(red, green, blue))
    try:
	keepalive()
	app.mainloop()
    except KeyboardInterrupt:
	pass



if __name__ == '__main__':
    main()