blob: 78139f8f2da9502b29f4381f49332eab9cc45506 [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
Georg Brandlbf76ce12010-08-02 22:08:58 +000016from tkinter import *
Barry Warsawdc492e61998-09-29 15:44:20 +000017import ColorDB
Barry Warsaw7080a7f1998-01-29 23:48:55 +000018
Barry Warsawdc492e61998-09-29 15:44:20 +000019
20class ChipWidget:
Barry Warsaw37400e81998-02-11 18:54:23 +000021 _WIDTH = 150
22 _HEIGHT = 80
Barry Warsaw7080a7f1998-01-29 23:48:55 +000023
Barry Warsawdc492e61998-09-29 15:44:20 +000024 def __init__(self,
Barry Warsawca07ba01998-10-22 03:25:59 +000025 master = None,
Barry Warsawdc492e61998-09-29 15:44:20 +000026 width = _WIDTH,
27 height = _HEIGHT,
28 text = 'Color',
29 initialcolor = 'blue',
30 presscmd = None,
31 releasecmd = None):
32 # create the text label
Barry Warsawca07ba01998-10-22 03:25:59 +000033 self.__label = Label(master, text=text)
Barry Warsawdc492e61998-09-29 15:44:20 +000034 self.__label.grid(row=0, column=0)
35 # create the color chip, implemented as a frame
Barry Warsawca07ba01998-10-22 03:25:59 +000036 self.__chip = Frame(master, relief=RAISED, borderwidth=2,
Barry Warsawdc492e61998-09-29 15:44:20 +000037 width=width,
38 height=height,
39 background=initialcolor)
40 self.__chip.grid(row=1, column=0)
Barry Warsawe47e9772001-08-23 16:14:45 +000041 # create the color name
42 self.__namevar = StringVar()
43 self.__namevar.set(initialcolor)
44 self.__name = Entry(master, textvariable=self.__namevar,
45 relief=FLAT, justify=CENTER, state=DISABLED,
46 font=self.__label['font'])
Barry Warsawdc492e61998-09-29 15:44:20 +000047 self.__name.grid(row=2, column=0)
Barry Warsawe47e9772001-08-23 16:14:45 +000048 # create the message area
49 self.__msgvar = StringVar()
50 self.__name = Entry(master, textvariable=self.__msgvar,
51 relief=FLAT, justify=CENTER, state=DISABLED,
52 font=self.__label['font'])
53 self.__name.grid(row=3, column=0)
Barry Warsawdc492e61998-09-29 15:44:20 +000054 # set bindings
55 if presscmd:
56 self.__chip.bind('<ButtonPress-1>', presscmd)
57 if releasecmd:
58 self.__chip.bind('<ButtonRelease-1>', releasecmd)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000059
Barry Warsawe47e9772001-08-23 16:14:45 +000060 def set_color(self, color):
Barry Warsawdc492e61998-09-29 15:44:20 +000061 self.__chip.config(background=color)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000062
Barry Warsawdc492e61998-09-29 15:44:20 +000063 def get_color(self):
64 return self.__chip['background']
Barry Warsaw7080a7f1998-01-29 23:48:55 +000065
Barry Warsawe47e9772001-08-23 16:14:45 +000066 def set_name(self, colorname):
67 self.__namevar.set(colorname)
68
69 def set_message(self, message):
70 self.__msgvar.set(message)
71
Barry Warsawdc492e61998-09-29 15:44:20 +000072 def press(self):
73 self.__chip.configure(relief=SUNKEN)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000074
Barry Warsawdc492e61998-09-29 15:44:20 +000075 def release(self):
76 self.__chip.configure(relief=RAISED)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000077
Barry Warsaw7080a7f1998-01-29 23:48:55 +000078
79
Barry Warsawdc492e61998-09-29 15:44:20 +000080class ChipViewer:
Barry Warsawca07ba01998-10-22 03:25:59 +000081 def __init__(self, switchboard, master=None):
Barry Warsawdc492e61998-09-29 15:44:20 +000082 self.__sb = switchboard
Barry Warsawca07ba01998-10-22 03:25:59 +000083 self.__frame = Frame(master, relief=RAISED, borderwidth=1)
Barry Warsaw615a66a1998-11-18 03:15:19 +000084 self.__frame.grid(row=3, column=0, ipadx=5, sticky='NSEW')
Barry Warsawdc492e61998-09-29 15:44:20 +000085 # create the chip that will display the currently selected color
86 # exactly
87 self.__sframe = Frame(self.__frame)
88 self.__sframe.grid(row=0, column=0)
89 self.__selected = ChipWidget(self.__sframe, text='Selected')
90 # create the chip that will display the nearest real X11 color
91 # database color name
92 self.__nframe = Frame(self.__frame)
93 self.__nframe.grid(row=0, column=1)
94 self.__nearest = ChipWidget(self.__nframe, text='Nearest',
95 presscmd = self.__buttonpress,
96 releasecmd = self.__buttonrelease)
Barry Warsaw7080a7f1998-01-29 23:48:55 +000097
Barry Warsawdc492e61998-09-29 15:44:20 +000098 def update_yourself(self, red, green, blue):
Barry Warsaw0604d721999-04-26 23:17:16 +000099 # Selected always shows the #rrggbb name of the color, nearest always
Barry Warsaw28030462001-07-10 21:37:28 +0000100 # shows the name of the nearest color in the database. BAW: should
Barry Warsaw0604d721999-04-26 23:17:16 +0000101 # an exact match be indicated in some way?
102 #
Tim Peters182b5ac2004-07-18 06:16:08 +0000103 # Always use the #rrggbb style to actually set the color, since we may
Barry Warsaw0604d721999-04-26 23:17:16 +0000104 # not be using X color names (e.g. "web-safe" names)
105 colordb = self.__sb.colordb()
Barry Warsawdc492e61998-09-29 15:44:20 +0000106 rgbtuple = (red, green, blue)
Barry Warsaw0604d721999-04-26 23:17:16 +0000107 rrggbb = ColorDB.triplet_to_rrggbb(rgbtuple)
108 # find the nearest
109 nearest = colordb.nearest(red, green, blue)
110 nearest_tuple = colordb.find_byname(nearest)
111 nearest_rrggbb = ColorDB.triplet_to_rrggbb(nearest_tuple)
112 self.__selected.set_color(rrggbb)
Barry Warsawe47e9772001-08-23 16:14:45 +0000113 self.__nearest.set_color(nearest_rrggbb)
114 # set the name and messages areas
115 self.__selected.set_name(rrggbb)
116 if rrggbb == nearest_rrggbb:
117 self.__selected.set_message(nearest)
118 else:
119 self.__selected.set_message('')
120 self.__nearest.set_name(nearest_rrggbb)
121 self.__nearest.set_message(nearest)
Barry Warsawdc492e61998-09-29 15:44:20 +0000122
123 def __buttonpress(self, event=None):
124 self.__nearest.press()
125
126 def __buttonrelease(self, event=None):
127 self.__nearest.release()
Barry Warsaw0604d721999-04-26 23:17:16 +0000128 rrggbb = self.__nearest.get_color()
129 red, green, blue = ColorDB.rrggbb_to_triplet(rrggbb)
Barry Warsawdc492e61998-09-29 15:44:20 +0000130 self.__sb.update_views(red, green, blue)