blob: b4dfa857c5c28da53550512aba92e84c59a952b7 [file] [log] [blame]
Barry Warsawdc492e61998-09-29 15:44:20 +00001from types import StringType
Barry Warsaw7080a7f1998-01-29 23:48:55 +00002from Tkinter import *
Barry Warsawdc492e61998-09-29 15:44:20 +00003import ColorDB
Barry Warsaw7080a7f1998-01-29 23:48:55 +00004
Barry Warsawdc492e61998-09-29 15:44:20 +00005
6class ChipWidget:
Barry Warsaw37400e81998-02-11 18:54:23 +00007 _WIDTH = 150
8 _HEIGHT = 80
Barry Warsaw7080a7f1998-01-29 23:48:55 +00009
Barry Warsawdc492e61998-09-29 15:44:20 +000010 def __init__(self,
11 parent = None,
12 width = _WIDTH,
13 height = _HEIGHT,
14 text = 'Color',
15 initialcolor = 'blue',
16 presscmd = None,
17 releasecmd = None):
18 # create the text label
19 self.__label = Label(parent, text=text)
20 self.__label.grid(row=0, column=0)
21 # create the color chip, implemented as a frame
22 self.__chip = Frame(parent, relief=RAISED, borderwidth=2,
23 width=width,
24 height=height,
25 background=initialcolor)
26 self.__chip.grid(row=1, column=0)
27 # create the color name, ctor argument must be a string
28 self.__name = Label(parent, text=initialcolor)
29 self.__name.grid(row=2, column=0)
30 #
31 # set bindings
32 if presscmd:
33 self.__chip.bind('<ButtonPress-1>', presscmd)
34 if releasecmd:
35 self.__chip.bind('<ButtonRelease-1>', releasecmd)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000036
Barry Warsawdc492e61998-09-29 15:44:20 +000037 def set_color(self, color):
38 self.__chip.config(background=color)
39 self.__name.config(text=color)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000040
Barry Warsawdc492e61998-09-29 15:44:20 +000041 def get_color(self):
42 return self.__chip['background']
Barry Warsaw7080a7f1998-01-29 23:48:55 +000043
Barry Warsawdc492e61998-09-29 15:44:20 +000044 def press(self):
45 self.__chip.configure(relief=SUNKEN)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000046
Barry Warsawdc492e61998-09-29 15:44:20 +000047 def release(self):
48 self.__chip.configure(relief=RAISED)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000049
Barry Warsaw7080a7f1998-01-29 23:48:55 +000050
51
Barry Warsawdc492e61998-09-29 15:44:20 +000052class ChipViewer:
53 def __init__(self, switchboard, parent=None):
54 self.__sb = switchboard
55 self.__frame = Frame(parent)
56 self.__frame.pack()
57 # create the chip that will display the currently selected color
58 # exactly
59 self.__sframe = Frame(self.__frame)
60 self.__sframe.grid(row=0, column=0)
61 self.__selected = ChipWidget(self.__sframe, text='Selected')
62 # create the chip that will display the nearest real X11 color
63 # database color name
64 self.__nframe = Frame(self.__frame)
65 self.__nframe.grid(row=0, column=1)
66 self.__nearest = ChipWidget(self.__nframe, text='Nearest',
67 presscmd = self.__buttonpress,
68 releasecmd = self.__buttonrelease)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000069
Barry Warsawdc492e61998-09-29 15:44:20 +000070 def update_yourself(self, red, green, blue):
71 # TBD: should exactname default to X11 color name if their is an exact
72 # match for the rgb triplet? Part of me says it's nice to see both
73 # names for the color, the other part says that it's better to
74 # feedback the exact match.
75 rgbtuple = (red, green, blue)
76 try:
77 allcolors = self.__sb.colordb().find_byrgb(rgbtuple)
78 exactname = allcolors[0]
79 except ColorDB.BadColor:
80 exactname = ColorDB.triplet_to_rrggbb(rgbtuple)
81 nearest = self.__sb.colordb().nearest(red, green, blue)
82 self.__selected.set_color(exactname)
83 self.__nearest.set_color(nearest)
84
85 def __buttonpress(self, event=None):
86 self.__nearest.press()
87
88 def __buttonrelease(self, event=None):
89 self.__nearest.release()
90 colorname = self.__nearest.get_color()
91 red, green, blue = self.__sb.colordb().find_byname(colorname)
92 self.__sb.update_views(red, green, blue)