Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 1 | from types import StringType |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 2 | from Tkinter import * |
Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 3 | import ColorDB |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 5 | |
| 6 | class ChipWidget: |
Barry Warsaw | 37400e8 | 1998-02-11 18:54:23 +0000 | [diff] [blame] | 7 | _WIDTH = 150 |
| 8 | _HEIGHT = 80 |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 9 | |
Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 10 | def __init__(self, |
| 11 | parent = None, |
| 12 | width = _WIDTH, |
| 13 | height = _HEIGHT, |
| 14 | text = 'Color', |
| 15 | initialcolor = 'blue', |
| 16 | presscmd = None, |
| 17 | releasecmd = None): |
| 18 | # create the text label |
| 19 | self.__label = Label(parent, text=text) |
| 20 | self.__label.grid(row=0, column=0) |
| 21 | # create the color chip, implemented as a frame |
| 22 | self.__chip = Frame(parent, relief=RAISED, borderwidth=2, |
| 23 | width=width, |
| 24 | height=height, |
| 25 | background=initialcolor) |
| 26 | self.__chip.grid(row=1, column=0) |
| 27 | # create the color name, ctor argument must be a string |
| 28 | self.__name = Label(parent, text=initialcolor) |
| 29 | self.__name.grid(row=2, column=0) |
| 30 | # |
| 31 | # set bindings |
| 32 | if presscmd: |
| 33 | self.__chip.bind('<ButtonPress-1>', presscmd) |
| 34 | if releasecmd: |
| 35 | self.__chip.bind('<ButtonRelease-1>', releasecmd) |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 36 | |
Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 37 | def set_color(self, color): |
| 38 | self.__chip.config(background=color) |
| 39 | self.__name.config(text=color) |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 40 | |
Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 41 | def get_color(self): |
| 42 | return self.__chip['background'] |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 43 | |
Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 44 | def press(self): |
| 45 | self.__chip.configure(relief=SUNKEN) |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 46 | |
Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 47 | def release(self): |
| 48 | self.__chip.configure(relief=RAISED) |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 49 | |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 50 | |
| 51 | |
Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 52 | class ChipViewer: |
| 53 | def __init__(self, switchboard, parent=None): |
| 54 | self.__sb = switchboard |
| 55 | self.__frame = Frame(parent) |
| 56 | self.__frame.pack() |
| 57 | # create the chip that will display the currently selected color |
| 58 | # exactly |
| 59 | self.__sframe = Frame(self.__frame) |
| 60 | self.__sframe.grid(row=0, column=0) |
| 61 | self.__selected = ChipWidget(self.__sframe, text='Selected') |
| 62 | # create the chip that will display the nearest real X11 color |
| 63 | # database color name |
| 64 | self.__nframe = Frame(self.__frame) |
| 65 | self.__nframe.grid(row=0, column=1) |
| 66 | self.__nearest = ChipWidget(self.__nframe, text='Nearest', |
| 67 | presscmd = self.__buttonpress, |
| 68 | releasecmd = self.__buttonrelease) |
Barry Warsaw | 7080a7f | 1998-01-29 23:48:55 +0000 | [diff] [blame] | 69 | |
Barry Warsaw | dc492e6 | 1998-09-29 15:44:20 +0000 | [diff] [blame] | 70 | def update_yourself(self, red, green, blue): |
| 71 | # TBD: should exactname default to X11 color name if their is an exact |
| 72 | # match for the rgb triplet? Part of me says it's nice to see both |
| 73 | # names for the color, the other part says that it's better to |
| 74 | # feedback the exact match. |
| 75 | rgbtuple = (red, green, blue) |
| 76 | try: |
| 77 | allcolors = self.__sb.colordb().find_byrgb(rgbtuple) |
| 78 | exactname = allcolors[0] |
| 79 | except ColorDB.BadColor: |
| 80 | exactname = ColorDB.triplet_to_rrggbb(rgbtuple) |
| 81 | nearest = self.__sb.colordb().nearest(red, green, blue) |
| 82 | self.__selected.set_color(exactname) |
| 83 | self.__nearest.set_color(nearest) |
| 84 | |
| 85 | def __buttonpress(self, event=None): |
| 86 | self.__nearest.press() |
| 87 | |
| 88 | def __buttonrelease(self, event=None): |
| 89 | self.__nearest.release() |
| 90 | colorname = self.__nearest.get_color() |
| 91 | red, green, blue = self.__sb.colordb().find_byname(colorname) |
| 92 | self.__sb.update_views(red, green, blue) |