blob: 05412cec48bb9d139d1eb38fe6f59dd95060d203 [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,
Barry Warsawca07ba01998-10-22 03:25:59 +000026 master = None,
Barry Warsawdc492e61998-09-29 15:44:20 +000027 width = _WIDTH,
28 height = _HEIGHT,
29 text = 'Color',
30 initialcolor = 'blue',
31 presscmd = None,
32 releasecmd = None):
33 # create the text label
Barry Warsawca07ba01998-10-22 03:25:59 +000034 self.__label = Label(master, text=text)
Barry Warsawdc492e61998-09-29 15:44:20 +000035 self.__label.grid(row=0, column=0)
36 # create the color chip, implemented as a frame
Barry Warsawca07ba01998-10-22 03:25:59 +000037 self.__chip = Frame(master, relief=RAISED, borderwidth=2,
Barry Warsawdc492e61998-09-29 15:44:20 +000038 width=width,
39 height=height,
40 background=initialcolor)
41 self.__chip.grid(row=1, column=0)
Barry Warsawe47e9772001-08-23 16:14:45 +000042 # create the color name
43 self.__namevar = StringVar()
44 self.__namevar.set(initialcolor)
45 self.__name = Entry(master, textvariable=self.__namevar,
46 relief=FLAT, justify=CENTER, state=DISABLED,
47 font=self.__label['font'])
Barry Warsawdc492e61998-09-29 15:44:20 +000048 self.__name.grid(row=2, column=0)
Barry Warsawe47e9772001-08-23 16:14:45 +000049 # create the message area
50 self.__msgvar = StringVar()
51 self.__name = Entry(master, textvariable=self.__msgvar,
52 relief=FLAT, justify=CENTER, state=DISABLED,
53 font=self.__label['font'])
54 self.__name.grid(row=3, column=0)
Barry Warsawdc492e61998-09-29 15:44:20 +000055 # set bindings
56 if presscmd:
57 self.__chip.bind('<ButtonPress-1>', presscmd)
58 if releasecmd:
59 self.__chip.bind('<ButtonRelease-1>', releasecmd)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000060
Barry Warsawe47e9772001-08-23 16:14:45 +000061 def set_color(self, color):
Barry Warsawdc492e61998-09-29 15:44:20 +000062 self.__chip.config(background=color)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000063
Barry Warsawdc492e61998-09-29 15:44:20 +000064 def get_color(self):
65 return self.__chip['background']
Barry Warsaw7080a7f1998-01-29 23:48:55 +000066
Barry Warsawe47e9772001-08-23 16:14:45 +000067 def set_name(self, colorname):
68 self.__namevar.set(colorname)
69
70 def set_message(self, message):
71 self.__msgvar.set(message)
72
Barry Warsawdc492e61998-09-29 15:44:20 +000073 def press(self):
74 self.__chip.configure(relief=SUNKEN)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000075
Barry Warsawdc492e61998-09-29 15:44:20 +000076 def release(self):
77 self.__chip.configure(relief=RAISED)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000078
Barry Warsaw7080a7f1998-01-29 23:48:55 +000079
80
Barry Warsawdc492e61998-09-29 15:44:20 +000081class ChipViewer:
Barry Warsawca07ba01998-10-22 03:25:59 +000082 def __init__(self, switchboard, master=None):
Barry Warsawdc492e61998-09-29 15:44:20 +000083 self.__sb = switchboard
Barry Warsawca07ba01998-10-22 03:25:59 +000084 self.__frame = Frame(master, relief=RAISED, borderwidth=1)
Barry Warsaw615a66a1998-11-18 03:15:19 +000085 self.__frame.grid(row=3, column=0, ipadx=5, sticky='NSEW')
Barry Warsawdc492e61998-09-29 15:44:20 +000086 # create the chip that will display the currently selected color
87 # exactly
88 self.__sframe = Frame(self.__frame)
89 self.__sframe.grid(row=0, column=0)
90 self.__selected = ChipWidget(self.__sframe, text='Selected')
91 # create the chip that will display the nearest real X11 color
92 # database color name
93 self.__nframe = Frame(self.__frame)
94 self.__nframe.grid(row=0, column=1)
95 self.__nearest = ChipWidget(self.__nframe, text='Nearest',
96 presscmd = self.__buttonpress,
97 releasecmd = self.__buttonrelease)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000098
Barry Warsawdc492e61998-09-29 15:44:20 +000099 def update_yourself(self, red, green, blue):
Barry Warsaw0604d721999-04-26 23:17:16 +0000100 # Selected always shows the #rrggbb name of the color, nearest always
Barry Warsaw28030462001-07-10 21:37:28 +0000101 # shows the name of the nearest color in the database. BAW: should
Barry Warsaw0604d721999-04-26 23:17:16 +0000102 # an exact match be indicated in some way?
103 #
Tim Peters182b5ac2004-07-18 06:16:08 +0000104 # Always use the #rrggbb style to actually set the color, since we may
Barry Warsaw0604d721999-04-26 23:17:16 +0000105 # not be using X color names (e.g. "web-safe" names)
106 colordb = self.__sb.colordb()
Barry Warsawdc492e61998-09-29 15:44:20 +0000107 rgbtuple = (red, green, blue)
Barry Warsaw0604d721999-04-26 23:17:16 +0000108 rrggbb = ColorDB.triplet_to_rrggbb(rgbtuple)
109 # find the nearest
110 nearest = colordb.nearest(red, green, blue)
111 nearest_tuple = colordb.find_byname(nearest)
112 nearest_rrggbb = ColorDB.triplet_to_rrggbb(nearest_tuple)
113 self.__selected.set_color(rrggbb)
Barry Warsawe47e9772001-08-23 16:14:45 +0000114 self.__nearest.set_color(nearest_rrggbb)
115 # set the name and messages areas
116 self.__selected.set_name(rrggbb)
117 if rrggbb == nearest_rrggbb:
118 self.__selected.set_message(nearest)
119 else:
120 self.__selected.set_message('')
121 self.__nearest.set_name(nearest_rrggbb)
122 self.__nearest.set_message(nearest)
Barry Warsawdc492e61998-09-29 15:44:20 +0000123
124 def __buttonpress(self, event=None):
125 self.__nearest.press()
126
127 def __buttonrelease(self, event=None):
128 self.__nearest.release()
Barry Warsaw0604d721999-04-26 23:17:16 +0000129 rrggbb = self.__nearest.get_color()
130 red, green, blue = ColorDB.rrggbb_to_triplet(rrggbb)
Barry Warsawdc492e61998-09-29 15:44:20 +0000131 self.__sb.update_views(red, green, blue)