1 """Chip viewer and widget.
3 In the lower left corner of the main Pynche window, you will see two
4 ChipWidgets, one for the selected color and one for the nearest color. The
5 selected color is the actual RGB value expressed as an X11 #COLOR name. The
6 nearest color is the named color from the X11 database that is closest to the
7 selected color in 3D space. There may be other colors equally close, but the
8 nearest one is the first one found.
10 Clicking on the nearest color chip selects that named color.
12 The ChipViewer class includes the entire lower left quandrant; i.e. both the
13 selected and nearest ChipWidgets.
16 from types
import StringType
30 initialcolor
= 'blue',
33 # create the text label
34 self
.__label
= Label(master
, text
=text
)
35 self
.__label
.grid(row
=0, column
=0)
36 # create the color chip, implemented as a frame
37 self
.__chip
= Frame(master
, relief
=RAISED
, borderwidth
=2,
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(master
, text
=initialcolor
)
44 self
.__name
.grid(row
=2, column
=0)
48 self
.__chip
.bind('<ButtonPress-1>', presscmd
)
50 self
.__chip
.bind('<ButtonRelease-1>', releasecmd
)
52 def set_color(self
, color
, colorname
=None):
53 self
.__chip
.config(background
=color
)
54 self
.__name
.config(text
=colorname
or color
)
57 return self
.__chip
['background']
60 self
.__chip
.configure(relief
=SUNKEN
)
63 self
.__chip
.configure(relief
=RAISED
)
68 def __init__(self
, switchboard
, master
=None):
69 self
.__sb
= switchboard
70 self
.__frame
= Frame(master
, relief
=RAISED
, borderwidth
=1)
71 self
.__frame
.grid(row
=3, column
=0, ipadx
=5, sticky
='NSEW')
72 # create the chip that will display the currently selected color
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
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
)
85 def update_yourself(self
, red
, green
, blue
):
86 # Selected always shows the #rrggbb name of the color, nearest always
87 # shows the name of the nearest color in the database. TBD: should
88 # an exact match be indicated in some way?
90 # Always use the #rrggbb style to actually set the color, since we may
91 # not be using X color names (e.g. "web-safe" names)
92 colordb
= self
.__sb
.colordb()
93 rgbtuple
= (red
, green
, blue
)
94 rrggbb
= ColorDB
.triplet_to_rrggbb(rgbtuple
)
96 nearest
= colordb
.nearest(red
, green
, blue
)
97 nearest_tuple
= colordb
.find_byname(nearest
)
98 nearest_rrggbb
= ColorDB
.triplet_to_rrggbb(nearest_tuple
)
99 self
.__selected
.set_color(rrggbb
)
100 self
.__nearest
.set_color(nearest_rrggbb
, nearest
)
102 def __buttonpress(self
, event
=None):
103 self
.__nearest
.press()
105 def __buttonrelease(self
, event
=None):
106 self
.__nearest
.release()
107 rrggbb
= self
.__nearest
.get_color()
108 red
, green
, blue
= ColorDB
.rrggbb_to_triplet(rrggbb
)
109 self
.__sb
.update_views(red
, green
, blue
)