1 | n/a | """Chip viewer and widget. |
---|
2 | n/a | |
---|
3 | n/a | In the lower left corner of the main Pynche window, you will see two |
---|
4 | n/a | ChipWidgets, one for the selected color and one for the nearest color. The |
---|
5 | n/a | selected color is the actual RGB value expressed as an X11 #COLOR name. The |
---|
6 | n/a | nearest color is the named color from the X11 database that is closest to the |
---|
7 | n/a | selected color in 3D space. There may be other colors equally close, but the |
---|
8 | n/a | nearest one is the first one found. |
---|
9 | n/a | |
---|
10 | n/a | Clicking on the nearest color chip selects that named color. |
---|
11 | n/a | |
---|
12 | n/a | The ChipViewer class includes the entire lower left quandrant; i.e. both the |
---|
13 | n/a | selected and nearest ChipWidgets. |
---|
14 | n/a | """ |
---|
15 | n/a | |
---|
16 | n/a | from tkinter import * |
---|
17 | n/a | import ColorDB |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | class ChipWidget: |
---|
21 | n/a | _WIDTH = 150 |
---|
22 | n/a | _HEIGHT = 80 |
---|
23 | n/a | |
---|
24 | n/a | def __init__(self, |
---|
25 | n/a | master = None, |
---|
26 | n/a | width = _WIDTH, |
---|
27 | n/a | height = _HEIGHT, |
---|
28 | n/a | text = 'Color', |
---|
29 | n/a | initialcolor = 'blue', |
---|
30 | n/a | presscmd = None, |
---|
31 | n/a | releasecmd = None): |
---|
32 | n/a | # create the text label |
---|
33 | n/a | self.__label = Label(master, text=text) |
---|
34 | n/a | self.__label.grid(row=0, column=0) |
---|
35 | n/a | # create the color chip, implemented as a frame |
---|
36 | n/a | self.__chip = Frame(master, relief=RAISED, borderwidth=2, |
---|
37 | n/a | width=width, |
---|
38 | n/a | height=height, |
---|
39 | n/a | background=initialcolor) |
---|
40 | n/a | self.__chip.grid(row=1, column=0) |
---|
41 | n/a | # create the color name |
---|
42 | n/a | self.__namevar = StringVar() |
---|
43 | n/a | self.__namevar.set(initialcolor) |
---|
44 | n/a | self.__name = Entry(master, textvariable=self.__namevar, |
---|
45 | n/a | relief=FLAT, justify=CENTER, state=DISABLED, |
---|
46 | n/a | font=self.__label['font']) |
---|
47 | n/a | self.__name.grid(row=2, column=0) |
---|
48 | n/a | # create the message area |
---|
49 | n/a | self.__msgvar = StringVar() |
---|
50 | n/a | self.__name = Entry(master, textvariable=self.__msgvar, |
---|
51 | n/a | relief=FLAT, justify=CENTER, state=DISABLED, |
---|
52 | n/a | font=self.__label['font']) |
---|
53 | n/a | self.__name.grid(row=3, column=0) |
---|
54 | n/a | # set bindings |
---|
55 | n/a | if presscmd: |
---|
56 | n/a | self.__chip.bind('<ButtonPress-1>', presscmd) |
---|
57 | n/a | if releasecmd: |
---|
58 | n/a | self.__chip.bind('<ButtonRelease-1>', releasecmd) |
---|
59 | n/a | |
---|
60 | n/a | def set_color(self, color): |
---|
61 | n/a | self.__chip.config(background=color) |
---|
62 | n/a | |
---|
63 | n/a | def get_color(self): |
---|
64 | n/a | return self.__chip['background'] |
---|
65 | n/a | |
---|
66 | n/a | def set_name(self, colorname): |
---|
67 | n/a | self.__namevar.set(colorname) |
---|
68 | n/a | |
---|
69 | n/a | def set_message(self, message): |
---|
70 | n/a | self.__msgvar.set(message) |
---|
71 | n/a | |
---|
72 | n/a | def press(self): |
---|
73 | n/a | self.__chip.configure(relief=SUNKEN) |
---|
74 | n/a | |
---|
75 | n/a | def release(self): |
---|
76 | n/a | self.__chip.configure(relief=RAISED) |
---|
77 | n/a | |
---|
78 | n/a | |
---|
79 | n/a | |
---|
80 | n/a | class ChipViewer: |
---|
81 | n/a | def __init__(self, switchboard, master=None): |
---|
82 | n/a | self.__sb = switchboard |
---|
83 | n/a | self.__frame = Frame(master, relief=RAISED, borderwidth=1) |
---|
84 | n/a | self.__frame.grid(row=3, column=0, ipadx=5, sticky='NSEW') |
---|
85 | n/a | # create the chip that will display the currently selected color |
---|
86 | n/a | # exactly |
---|
87 | n/a | self.__sframe = Frame(self.__frame) |
---|
88 | n/a | self.__sframe.grid(row=0, column=0) |
---|
89 | n/a | self.__selected = ChipWidget(self.__sframe, text='Selected') |
---|
90 | n/a | # create the chip that will display the nearest real X11 color |
---|
91 | n/a | # database color name |
---|
92 | n/a | self.__nframe = Frame(self.__frame) |
---|
93 | n/a | self.__nframe.grid(row=0, column=1) |
---|
94 | n/a | self.__nearest = ChipWidget(self.__nframe, text='Nearest', |
---|
95 | n/a | presscmd = self.__buttonpress, |
---|
96 | n/a | releasecmd = self.__buttonrelease) |
---|
97 | n/a | |
---|
98 | n/a | def update_yourself(self, red, green, blue): |
---|
99 | n/a | # Selected always shows the #rrggbb name of the color, nearest always |
---|
100 | n/a | # shows the name of the nearest color in the database. BAW: should |
---|
101 | n/a | # an exact match be indicated in some way? |
---|
102 | n/a | # |
---|
103 | n/a | # Always use the #rrggbb style to actually set the color, since we may |
---|
104 | n/a | # not be using X color names (e.g. "web-safe" names) |
---|
105 | n/a | colordb = self.__sb.colordb() |
---|
106 | n/a | rgbtuple = (red, green, blue) |
---|
107 | n/a | rrggbb = ColorDB.triplet_to_rrggbb(rgbtuple) |
---|
108 | n/a | # find the nearest |
---|
109 | n/a | nearest = colordb.nearest(red, green, blue) |
---|
110 | n/a | nearest_tuple = colordb.find_byname(nearest) |
---|
111 | n/a | nearest_rrggbb = ColorDB.triplet_to_rrggbb(nearest_tuple) |
---|
112 | n/a | self.__selected.set_color(rrggbb) |
---|
113 | n/a | self.__nearest.set_color(nearest_rrggbb) |
---|
114 | n/a | # set the name and messages areas |
---|
115 | n/a | self.__selected.set_name(rrggbb) |
---|
116 | n/a | if rrggbb == nearest_rrggbb: |
---|
117 | n/a | self.__selected.set_message(nearest) |
---|
118 | n/a | else: |
---|
119 | n/a | self.__selected.set_message('') |
---|
120 | n/a | self.__nearest.set_name(nearest_rrggbb) |
---|
121 | n/a | self.__nearest.set_message(nearest) |
---|
122 | n/a | |
---|
123 | n/a | def __buttonpress(self, event=None): |
---|
124 | n/a | self.__nearest.press() |
---|
125 | n/a | |
---|
126 | n/a | def __buttonrelease(self, event=None): |
---|
127 | n/a | self.__nearest.release() |
---|
128 | n/a | rrggbb = self.__nearest.get_color() |
---|
129 | n/a | red, green, blue = ColorDB.rrggbb_to_triplet(rrggbb) |
---|
130 | n/a | self.__sb.update_views(red, green, blue) |
---|