blob: 44398d7a541a6e5114b85dd6d410532becf1ee82 [file] [log] [blame]
Barry Warsawfefbf791998-01-31 23:39:01 +00001"""Main Pynche (Pythonically Natural Color and Hue Editor) widget.
2"""
3
4from Tkinter import *
5import Pmw
6import ColorDB
7from ChipWidget import ChipWidget
Barry Warsawa6f591a1998-02-11 17:19:54 +00008from TypeinWidget import TypeinWidget
Barry Warsaw29fe4dd1998-02-12 19:52:31 +00009from StripWidget import StripWidget
Barry Warsawfefbf791998-01-31 23:39:01 +000010
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000011
12def constant(numchips):
13 step = 255.0 / (numchips - 1)
14 start = 0.0
15 seq = []
16 while numchips > 0:
17 seq.append(int(start))
18 start = start + step
19 numchips = numchips - 1
20 return seq
21
Barry Warsaw79a78791998-02-13 21:28:14 +000022# red variations, green+blue = cyan constant
23def constant_cyan_generator(numchips, rgbtuple):
24 red, green, blue = rgbtuple
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000025 seq = constant(numchips)
Barry Warsaw79a78791998-02-13 21:28:14 +000026 return map(None, seq, [green] * numchips, [blue] * numchips)
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000027
Barry Warsaw79a78791998-02-13 21:28:14 +000028# green variations, red+blue = magenta constant
29def constant_magenta_generator(numchips, rgbtuple):
30 red, green, blue = rgbtuple
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000031 seq = constant(numchips)
Barry Warsaw79a78791998-02-13 21:28:14 +000032 return map(None, [red] * numchips, seq, [blue] * numchips)
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000033
Barry Warsaw79a78791998-02-13 21:28:14 +000034# blue variations, red+green = yellow constant
35def constant_yellow_generator(numchips, rgbtuple):
36 red, green, blue = rgbtuple
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000037 seq = constant(numchips)
Barry Warsaw79a78791998-02-13 21:28:14 +000038 return map(None, [red] * numchips, [green] * numchips, seq)
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000039
40
41
Barry Warsawfefbf791998-01-31 23:39:01 +000042class PyncheWidget(Pmw.MegaWidget):
43 def __init__(self, colordb, parent=None, **kw):
44 self.__colordb = colordb
45
Barry Warsawa6f591a1998-02-11 17:19:54 +000046 options = (('color', (128, 128, 128), self.__set_color),
47 ('delegate', None, None),
48 )
49 self.defineoptions(kw, options)
Barry Warsawfefbf791998-01-31 23:39:01 +000050
51 # initialize base class -- after defining options
52 Pmw.MegaWidget.__init__(self, parent)
53 interiorarg = (self.interior(),)
54
55 # create color selectors
Barry Warsaw93737b21998-02-17 03:09:19 +000056 group = Pmw.Group(parent, tag_text='Variations')
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000057 group.pack(side=TOP, expand=YES, fill=BOTH)
58 self.__reds = StripWidget(group.interior(),
Barry Warsaw79a78791998-02-13 21:28:14 +000059 generator=constant_cyan_generator,
Barry Warsaw93737b21998-02-17 03:09:19 +000060 axis=0, label='Red Variations')
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000061 self.__reds.pack()
62 self.__blues = StripWidget(group.interior(),
Barry Warsaw79a78791998-02-13 21:28:14 +000063 generator=constant_magenta_generator,
Barry Warsaw93737b21998-02-17 03:09:19 +000064 axis=1, label='Green Variations')
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000065 self.__blues.pack()
66 self.__greens = StripWidget(group.interior(),
Barry Warsaw79a78791998-02-13 21:28:14 +000067 generator=constant_yellow_generator,
Barry Warsaw93737b21998-02-17 03:09:19 +000068 axis=2, label='Blue Variations')
Barry Warsaw29fe4dd1998-02-12 19:52:31 +000069 self.__greens.pack()
Barry Warsawfefbf791998-01-31 23:39:01 +000070
71 # create chip window
72 group = Pmw.Group(parent, tag_text='Current Color')
Barry Warsaw93737b21998-02-17 03:09:19 +000073 group.pack(side=LEFT, expand=YES, fill=BOTH)
Barry Warsawfefbf791998-01-31 23:39:01 +000074 self.__selected = ChipWidget(group.interior(),
75 label_text='Selected')
76 self.__selected.grid()
77 self.__nearest = ChipWidget(group.interior(),
78 label_text='Nearest')
79 self.__nearest.grid(row=0, column=1)
Barry Warsaw93737b21998-02-17 03:09:19 +000080 chip = self.__nearest.component('chip')
81 chip.bind('<ButtonRelease-1>', self.__set_color_to_chip)
Barry Warsawfefbf791998-01-31 23:39:01 +000082
83 # create the options window
Barry Warsawa6f591a1998-02-11 17:19:54 +000084 self.__typein = TypeinWidget(group.interior())
Barry Warsaw93737b21998-02-17 03:09:19 +000085 self.__typein.grid(row=0, column=2)
Barry Warsawfefbf791998-01-31 23:39:01 +000086
87 # Check keywords and initialize options
88 self.initialiseoptions(PyncheWidget)
89
Barry Warsawa6f591a1998-02-11 17:19:54 +000090 self.__typein.configure(delegate=self)
Barry Warsaw93737b21998-02-17 03:09:19 +000091 self.__reds.configure(delegate=self)
92 self.__greens.configure(delegate=self)
93 self.__blues.configure(delegate=self)
Barry Warsawa6f591a1998-02-11 17:19:54 +000094
95 #
96 # PUBLIC INTERFACE
97 #
98
99
100 def set_color(self, obj, rgbtuple):
Barry Warsawa6f591a1998-02-11 17:19:54 +0000101 nearest = self.__colordb.nearest(rgbtuple)
Barry Warsaw55d3ffa1998-02-11 18:55:50 +0000102 red, green, blue = self.__colordb.find_byname(nearest)
Barry Warsawa6f591a1998-02-11 17:19:54 +0000103 # for an exact match, use the color name
104 if (red, green, blue) == rgbtuple:
105 self.__selected.configure(color=nearest)
106 # otherwise, use the #rrggbb name
107 else:
108 rrggbb = ColorDB.triplet_to_rrggbb(rgbtuple)
109 self.__selected.configure(color=rrggbb)
Barry Warsaw29fe4dd1998-02-12 19:52:31 +0000110
111 # configure all subwidgets
Barry Warsawa6f591a1998-02-11 17:19:54 +0000112 self.__nearest.configure(color=nearest)
113 self.__typein.configure(color=rgbtuple)
Barry Warsaw29fe4dd1998-02-12 19:52:31 +0000114 self.__reds.configure(color=rgbtuple)
115 self.__greens.configure(color=rgbtuple)
116 self.__blues.configure(color=rgbtuple)
Barry Warsawa6f591a1998-02-11 17:19:54 +0000117 delegate = self['delegate']
118 if delegate:
119 delegate.set_color(self, rgbtuple)
120
121 #
122 # PRIVATE INTERFACE
123 #
Barry Warsawfefbf791998-01-31 23:39:01 +0000124
125 def __set_color(self):
Barry Warsawa6f591a1998-02-11 17:19:54 +0000126 self.set_color(self, self['color'])
Barry Warsaw93737b21998-02-17 03:09:19 +0000127
128 def __set_color_to_chip(self, event=None):
129 color = self.__nearest['color']
130 rgbtuple = self.__colordb.find_byname(color)
131 self.set_color(self, rgbtuple)