1998-10-02 12:58:50 -03:00
|
|
|
|
"""Chip viewer and widget.
|
|
|
|
|
|
|
|
|
|
In the lower left corner of the main Pynche window, you will see two
|
|
|
|
|
ChipWidgets, one for the selected color and one for the nearest color. The
|
|
|
|
|
selected color is the actual RGB value expressed as an X11 #COLOR name. The
|
|
|
|
|
nearest color is the named color from the X11 database that is closest to the
|
|
|
|
|
selected color in 3D space. There may be other colors equally close, but the
|
|
|
|
|
nearest one is the first one found.
|
|
|
|
|
|
|
|
|
|
Clicking on the nearest color chip selects that named color.
|
|
|
|
|
|
|
|
|
|
The ChipViewer class includes the entire lower left quandrant; i.e. both the
|
|
|
|
|
selected and nearest ChipWidgets.
|
|
|
|
|
"""
|
|
|
|
|
|
1998-09-29 12:44:20 -03:00
|
|
|
|
from types import StringType
|
1998-01-29 19:48:55 -04:00
|
|
|
|
from Tkinter import *
|
1998-09-29 12:44:20 -03:00
|
|
|
|
import ColorDB
|
1998-01-29 19:48:55 -04:00
|
|
|
|
|
1998-09-29 12:44:20 -03:00
|
|
|
|
|
|
|
|
|
class ChipWidget:
|
1998-02-11 14:54:23 -04:00
|
|
|
|
_WIDTH = 150
|
|
|
|
|
_HEIGHT = 80
|
1998-01-29 19:48:55 -04:00
|
|
|
|
|
1998-09-29 12:44:20 -03:00
|
|
|
|
def __init__(self,
|
Many changes to support a second mode of operation. Pynche can now be
run either as a standalone application (by running pynche or
pynche.pyw), or as a modal dialog inside another application. This
can be done by importing pyColorChooser and running askcolor(). The
API for this is the same as the tkColorChooser.askcolor() API, namely:
When `Okay' is hit, askcolor() returns ((r, g, b), "name"). When
`Cancel' is hit, askcolor() returns (None, None).
Note the following differences:
1. pyColorChooser.askcolor() takes an optional keyword `master'
which if set tells Pynche to run as a modal dialog. `master'
is a Tkinter parent window. Without the `master' keyword
Pynche runs standalone.
2. in pyColorChooser.askcolor() will return a Tk/X11 color name as
"name" if there is an exact match, otherwise it will return a
color spec, e.g. "#rrggbb". tkColorChooser can't return a
color name.
There are also some UI differences when running standalone vs. modal.
When modal, there is no "File" menu, but instead there are "Okay" and
"Cancel" buttons.
The implementation of all this is a bit of a hack, but it seems to
work moderately well. I'm not guaranteeing the pyColorChooser.Chooser
class has the same semantics as the tkColorChooser.Chooser class.
1998-10-22 00:25:59 -03:00
|
|
|
|
master = None,
|
1998-09-29 12:44:20 -03:00
|
|
|
|
width = _WIDTH,
|
|
|
|
|
height = _HEIGHT,
|
|
|
|
|
text = 'Color',
|
|
|
|
|
initialcolor = 'blue',
|
|
|
|
|
presscmd = None,
|
|
|
|
|
releasecmd = None):
|
|
|
|
|
# create the text label
|
Many changes to support a second mode of operation. Pynche can now be
run either as a standalone application (by running pynche or
pynche.pyw), or as a modal dialog inside another application. This
can be done by importing pyColorChooser and running askcolor(). The
API for this is the same as the tkColorChooser.askcolor() API, namely:
When `Okay' is hit, askcolor() returns ((r, g, b), "name"). When
`Cancel' is hit, askcolor() returns (None, None).
Note the following differences:
1. pyColorChooser.askcolor() takes an optional keyword `master'
which if set tells Pynche to run as a modal dialog. `master'
is a Tkinter parent window. Without the `master' keyword
Pynche runs standalone.
2. in pyColorChooser.askcolor() will return a Tk/X11 color name as
"name" if there is an exact match, otherwise it will return a
color spec, e.g. "#rrggbb". tkColorChooser can't return a
color name.
There are also some UI differences when running standalone vs. modal.
When modal, there is no "File" menu, but instead there are "Okay" and
"Cancel" buttons.
The implementation of all this is a bit of a hack, but it seems to
work moderately well. I'm not guaranteeing the pyColorChooser.Chooser
class has the same semantics as the tkColorChooser.Chooser class.
1998-10-22 00:25:59 -03:00
|
|
|
|
self.__label = Label(master, text=text)
|
1998-09-29 12:44:20 -03:00
|
|
|
|
self.__label.grid(row=0, column=0)
|
|
|
|
|
# create the color chip, implemented as a frame
|
Many changes to support a second mode of operation. Pynche can now be
run either as a standalone application (by running pynche or
pynche.pyw), or as a modal dialog inside another application. This
can be done by importing pyColorChooser and running askcolor(). The
API for this is the same as the tkColorChooser.askcolor() API, namely:
When `Okay' is hit, askcolor() returns ((r, g, b), "name"). When
`Cancel' is hit, askcolor() returns (None, None).
Note the following differences:
1. pyColorChooser.askcolor() takes an optional keyword `master'
which if set tells Pynche to run as a modal dialog. `master'
is a Tkinter parent window. Without the `master' keyword
Pynche runs standalone.
2. in pyColorChooser.askcolor() will return a Tk/X11 color name as
"name" if there is an exact match, otherwise it will return a
color spec, e.g. "#rrggbb". tkColorChooser can't return a
color name.
There are also some UI differences when running standalone vs. modal.
When modal, there is no "File" menu, but instead there are "Okay" and
"Cancel" buttons.
The implementation of all this is a bit of a hack, but it seems to
work moderately well. I'm not guaranteeing the pyColorChooser.Chooser
class has the same semantics as the tkColorChooser.Chooser class.
1998-10-22 00:25:59 -03:00
|
|
|
|
self.__chip = Frame(master, relief=RAISED, borderwidth=2,
|
1998-09-29 12:44:20 -03:00
|
|
|
|
width=width,
|
|
|
|
|
height=height,
|
|
|
|
|
background=initialcolor)
|
|
|
|
|
self.__chip.grid(row=1, column=0)
|
|
|
|
|
# create the color name, ctor argument must be a string
|
Many changes to support a second mode of operation. Pynche can now be
run either as a standalone application (by running pynche or
pynche.pyw), or as a modal dialog inside another application. This
can be done by importing pyColorChooser and running askcolor(). The
API for this is the same as the tkColorChooser.askcolor() API, namely:
When `Okay' is hit, askcolor() returns ((r, g, b), "name"). When
`Cancel' is hit, askcolor() returns (None, None).
Note the following differences:
1. pyColorChooser.askcolor() takes an optional keyword `master'
which if set tells Pynche to run as a modal dialog. `master'
is a Tkinter parent window. Without the `master' keyword
Pynche runs standalone.
2. in pyColorChooser.askcolor() will return a Tk/X11 color name as
"name" if there is an exact match, otherwise it will return a
color spec, e.g. "#rrggbb". tkColorChooser can't return a
color name.
There are also some UI differences when running standalone vs. modal.
When modal, there is no "File" menu, but instead there are "Okay" and
"Cancel" buttons.
The implementation of all this is a bit of a hack, but it seems to
work moderately well. I'm not guaranteeing the pyColorChooser.Chooser
class has the same semantics as the tkColorChooser.Chooser class.
1998-10-22 00:25:59 -03:00
|
|
|
|
self.__name = Label(master, text=initialcolor)
|
1998-09-29 12:44:20 -03:00
|
|
|
|
self.__name.grid(row=2, column=0)
|
|
|
|
|
#
|
|
|
|
|
# set bindings
|
|
|
|
|
if presscmd:
|
|
|
|
|
self.__chip.bind('<ButtonPress-1>', presscmd)
|
|
|
|
|
if releasecmd:
|
|
|
|
|
self.__chip.bind('<ButtonRelease-1>', releasecmd)
|
|
|
|
|
|
1999-04-26 20:17:16 -03:00
|
|
|
|
def set_color(self, color, colorname=None):
|
1998-09-29 12:44:20 -03:00
|
|
|
|
self.__chip.config(background=color)
|
1999-04-26 20:17:16 -03:00
|
|
|
|
self.__name.config(text=colorname or color)
|
1998-09-29 12:44:20 -03:00
|
|
|
|
|
|
|
|
|
def get_color(self):
|
|
|
|
|
return self.__chip['background']
|
|
|
|
|
|
|
|
|
|
def press(self):
|
|
|
|
|
self.__chip.configure(relief=SUNKEN)
|
|
|
|
|
|
|
|
|
|
def release(self):
|
|
|
|
|
self.__chip.configure(relief=RAISED)
|
1998-01-29 19:48:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1998-09-29 12:44:20 -03:00
|
|
|
|
class ChipViewer:
|
Many changes to support a second mode of operation. Pynche can now be
run either as a standalone application (by running pynche or
pynche.pyw), or as a modal dialog inside another application. This
can be done by importing pyColorChooser and running askcolor(). The
API for this is the same as the tkColorChooser.askcolor() API, namely:
When `Okay' is hit, askcolor() returns ((r, g, b), "name"). When
`Cancel' is hit, askcolor() returns (None, None).
Note the following differences:
1. pyColorChooser.askcolor() takes an optional keyword `master'
which if set tells Pynche to run as a modal dialog. `master'
is a Tkinter parent window. Without the `master' keyword
Pynche runs standalone.
2. in pyColorChooser.askcolor() will return a Tk/X11 color name as
"name" if there is an exact match, otherwise it will return a
color spec, e.g. "#rrggbb". tkColorChooser can't return a
color name.
There are also some UI differences when running standalone vs. modal.
When modal, there is no "File" menu, but instead there are "Okay" and
"Cancel" buttons.
The implementation of all this is a bit of a hack, but it seems to
work moderately well. I'm not guaranteeing the pyColorChooser.Chooser
class has the same semantics as the tkColorChooser.Chooser class.
1998-10-22 00:25:59 -03:00
|
|
|
|
def __init__(self, switchboard, master=None):
|
1998-09-29 12:44:20 -03:00
|
|
|
|
self.__sb = switchboard
|
Many changes to support a second mode of operation. Pynche can now be
run either as a standalone application (by running pynche or
pynche.pyw), or as a modal dialog inside another application. This
can be done by importing pyColorChooser and running askcolor(). The
API for this is the same as the tkColorChooser.askcolor() API, namely:
When `Okay' is hit, askcolor() returns ((r, g, b), "name"). When
`Cancel' is hit, askcolor() returns (None, None).
Note the following differences:
1. pyColorChooser.askcolor() takes an optional keyword `master'
which if set tells Pynche to run as a modal dialog. `master'
is a Tkinter parent window. Without the `master' keyword
Pynche runs standalone.
2. in pyColorChooser.askcolor() will return a Tk/X11 color name as
"name" if there is an exact match, otherwise it will return a
color spec, e.g. "#rrggbb". tkColorChooser can't return a
color name.
There are also some UI differences when running standalone vs. modal.
When modal, there is no "File" menu, but instead there are "Okay" and
"Cancel" buttons.
The implementation of all this is a bit of a hack, but it seems to
work moderately well. I'm not guaranteeing the pyColorChooser.Chooser
class has the same semantics as the tkColorChooser.Chooser class.
1998-10-22 00:25:59 -03:00
|
|
|
|
self.__frame = Frame(master, relief=RAISED, borderwidth=1)
|
1998-11-17 23:15:19 -04:00
|
|
|
|
self.__frame.grid(row=3, column=0, ipadx=5, sticky='NSEW')
|
1998-09-29 12:44:20 -03:00
|
|
|
|
# create the chip that will display the currently selected color
|
|
|
|
|
# exactly
|
|
|
|
|
self.__sframe = Frame(self.__frame)
|
|
|
|
|
self.__sframe.grid(row=0, column=0)
|
|
|
|
|
self.__selected = ChipWidget(self.__sframe, text='Selected')
|
|
|
|
|
# create the chip that will display the nearest real X11 color
|
|
|
|
|
# database color name
|
|
|
|
|
self.__nframe = Frame(self.__frame)
|
|
|
|
|
self.__nframe.grid(row=0, column=1)
|
|
|
|
|
self.__nearest = ChipWidget(self.__nframe, text='Nearest',
|
|
|
|
|
presscmd = self.__buttonpress,
|
|
|
|
|
releasecmd = self.__buttonrelease)
|
|
|
|
|
|
|
|
|
|
def update_yourself(self, red, green, blue):
|
1999-04-26 20:17:16 -03:00
|
|
|
|
# Selected always shows the #rrggbb name of the color, nearest always
|
|
|
|
|
# shows the name of the nearest color in the database. TBD: should
|
|
|
|
|
# an exact match be indicated in some way?
|
|
|
|
|
#
|
|
|
|
|
# Always use the #rrggbb style to actually set the color, since we may
|
|
|
|
|
# not be using X color names (e.g. "web-safe" names)
|
|
|
|
|
colordb = self.__sb.colordb()
|
1998-09-29 12:44:20 -03:00
|
|
|
|
rgbtuple = (red, green, blue)
|
1999-04-26 20:17:16 -03:00
|
|
|
|
rrggbb = ColorDB.triplet_to_rrggbb(rgbtuple)
|
|
|
|
|
# find the nearest
|
|
|
|
|
nearest = colordb.nearest(red, green, blue)
|
|
|
|
|
nearest_tuple = colordb.find_byname(nearest)
|
|
|
|
|
nearest_rrggbb = ColorDB.triplet_to_rrggbb(nearest_tuple)
|
|
|
|
|
self.__selected.set_color(rrggbb)
|
|
|
|
|
self.__nearest.set_color(nearest_rrggbb, nearest)
|
1998-09-29 12:44:20 -03:00
|
|
|
|
|
|
|
|
|
def __buttonpress(self, event=None):
|
|
|
|
|
self.__nearest.press()
|
|
|
|
|
|
|
|
|
|
def __buttonrelease(self, event=None):
|
|
|
|
|
self.__nearest.release()
|
1999-04-26 20:17:16 -03:00
|
|
|
|
rrggbb = self.__nearest.get_color()
|
|
|
|
|
red, green, blue = ColorDB.rrggbb_to_triplet(rrggbb)
|
1998-09-29 12:44:20 -03:00
|
|
|
|
self.__sb.update_views(red, green, blue)
|