ยปCore Development>Code coverage>Tools/pynche/ChipViewer.py

Python code coverage for Tools/pynche/ChipViewer.py

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