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