blob: ef70e95c5a1bf4acc31e5a5f8c7f0649da893df9 [file] [log] [blame]
Barry Warsawddb9fc11998-10-02 15:58:50 +00001"""Chip viewer and widget.
2
3In the lower left corner of the main Pynche window, you will see two
4ChipWidgets, one for the selected color and one for the nearest color. The
5selected color is the actual RGB value expressed as an X11 #COLOR name. The
6nearest color is the named color from the X11 database that is closest to the
7selected color in 3D space. There may be other colors equally close, but the
8nearest one is the first one found.
9
10Clicking on the nearest color chip selects that named color.
11
12The ChipViewer class includes the entire lower left quandrant; i.e. both the
13selected and nearest ChipWidgets.
14"""
15
Barry Warsawdc492e61998-09-29 15:44:20 +000016from types import StringType
Barry Warsaw7080a7f1998-01-29 23:48:55 +000017from Tkinter import *
Barry Warsawdc492e61998-09-29 15:44:20 +000018import ColorDB
Barry Warsaw7080a7f1998-01-29 23:48:55 +000019
Barry Warsawdc492e61998-09-29 15:44:20 +000020
21class ChipWidget:
Barry Warsaw37400e81998-02-11 18:54:23 +000022 _WIDTH = 150
23 _HEIGHT = 80
Barry Warsaw7080a7f1998-01-29 23:48:55 +000024
Barry Warsawdc492e61998-09-29 15:44:20 +000025 def __init__(self,
26 parent = None,
27 width = _WIDTH,
28 height = _HEIGHT,
29 text = 'Color',
30 initialcolor = 'blue',
31 presscmd = None,
32 releasecmd = None):
33 # create the text label
34 self.__label = Label(parent, text=text)
35 self.__label.grid(row=0, column=0)
36 # create the color chip, implemented as a frame
37 self.__chip = Frame(parent, relief=RAISED, borderwidth=2,
38 width=width,
39 height=height,
40 background=initialcolor)
41 self.__chip.grid(row=1, column=0)
42 # create the color name, ctor argument must be a string
43 self.__name = Label(parent, text=initialcolor)
44 self.__name.grid(row=2, column=0)
45 #
46 # set bindings
47 if presscmd:
48 self.__chip.bind('<ButtonPress-1>', presscmd)
49 if releasecmd:
50 self.__chip.bind('<ButtonRelease-1>', releasecmd)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000051
Barry Warsawdc492e61998-09-29 15:44:20 +000052 def set_color(self, color):
53 self.__chip.config(background=color)
54 self.__name.config(text=color)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000055
Barry Warsawdc492e61998-09-29 15:44:20 +000056 def get_color(self):
57 return self.__chip['background']
Barry Warsaw7080a7f1998-01-29 23:48:55 +000058
Barry Warsawdc492e61998-09-29 15:44:20 +000059 def press(self):
60 self.__chip.configure(relief=SUNKEN)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000061
Barry Warsawdc492e61998-09-29 15:44:20 +000062 def release(self):
63 self.__chip.configure(relief=RAISED)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000064
Barry Warsaw7080a7f1998-01-29 23:48:55 +000065
66
Barry Warsawdc492e61998-09-29 15:44:20 +000067class ChipViewer:
68 def __init__(self, switchboard, parent=None):
69 self.__sb = switchboard
Barry Warsaw65f0b6b1998-10-01 14:26:04 +000070 self.__frame = Frame(parent) #, relief=GROOVE, borderwidth=2)
71 self.__frame.grid(row=3, column=0)
Barry Warsawdc492e61998-09-29 15:44:20 +000072 # create the chip that will display the currently selected color
73 # exactly
74 self.__sframe = Frame(self.__frame)
75 self.__sframe.grid(row=0, column=0)
76 self.__selected = ChipWidget(self.__sframe, text='Selected')
77 # create the chip that will display the nearest real X11 color
78 # database color name
79 self.__nframe = Frame(self.__frame)
80 self.__nframe.grid(row=0, column=1)
81 self.__nearest = ChipWidget(self.__nframe, text='Nearest',
82 presscmd = self.__buttonpress,
83 releasecmd = self.__buttonrelease)
Barry Warsaw65f0b6b1998-10-01 14:26:04 +000084 self.__div = Frame(self.__frame,
85 width=2,
86 borderwidth=2,
87 relief=RAISED)
88 self.__div.grid(row=0, column=2, sticky='NS', padx=5)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000089
Barry Warsawdc492e61998-09-29 15:44:20 +000090 def update_yourself(self, red, green, blue):
91 # TBD: should exactname default to X11 color name if their is an exact
92 # match for the rgb triplet? Part of me says it's nice to see both
93 # names for the color, the other part says that it's better to
94 # feedback the exact match.
95 rgbtuple = (red, green, blue)
96 try:
97 allcolors = self.__sb.colordb().find_byrgb(rgbtuple)
98 exactname = allcolors[0]
99 except ColorDB.BadColor:
100 exactname = ColorDB.triplet_to_rrggbb(rgbtuple)
101 nearest = self.__sb.colordb().nearest(red, green, blue)
102 self.__selected.set_color(exactname)
103 self.__nearest.set_color(nearest)
104
105 def __buttonpress(self, event=None):
106 self.__nearest.press()
107
108 def __buttonrelease(self, event=None):
109 self.__nearest.release()
110 colorname = self.__nearest.get_color()
111 red, green, blue = self.__sb.colordb().find_byname(colorname)
112 self.__sb.update_views(red, green, blue)