53e828389e0367f9920b4be768ccc31fa063fb7c
1 #----------------------------------------------------------------------------
2 # Name: ColourSelect.py
3 # Purpose: Colour Box Selection Control
5 # Author: Lorne White, Lorne.White@telusplanet.net
7 # Created: Feb 25, 2001
8 # Licence: wxWindows license
9 #----------------------------------------------------------------------------
11 from wxPython
.wx
import *
13 # creates a colour wxButton with selectable color
14 # button click provides a colour selection box
15 # button colour will change to new colour
16 # GetColour method to get the selected colour
19 # call back to function if changes made
21 # Cliff Wells, logiplexsoftware@earthlink.net:
22 # - Made ColourSelect into "is a button" rather than "has a button"
23 # - Added label parameter and logic to adjust the label colour according to the background
26 # - Rearranged arguments to more closely follow wx conventions
27 # - Simplified some of the code
30 class ColourSelect(wxButton
):
31 def __init__(self
, parent
, id, label
= "", bcolour
=(0, 0, 0),
32 pos
= wxDefaultPosition
, size
= wxDefaultSize
,
34 wxButton
.__init
__(self
, parent
, id, label
, pos
=pos
, size
=size
)
35 self
.SetColour(bcolour
)
36 self
.callback
= callback
37 EVT_BUTTON(parent
, self
.GetId(), self
.OnClick
)
40 return self
.set_colour
43 return self
.set_colour
45 def SetColour(self
, bcolour
):
46 self
.set_colour_val
= wxColor(bcolour
[0], bcolour
[1], bcolour
[2])
47 self
.SetBackgroundColour(self
.set_colour_val
)
48 avg
= reduce(lambda a
, b
: a
+ b
, bcolour
) / 3
49 fcolour
= avg
> 128 and (0, 0, 0) or (255, 255, 255)
50 self
.SetForegroundColour(apply(wxColour
, fcolour
))
51 self
.set_colour
= bcolour
53 def SetValue(self
, bcolour
):
54 self
.SetColour(bcolour
)
57 if self
.callback
is not None:
60 def OnClick(self
, event
):
62 data
.SetChooseFull(true
)
63 data
.SetColour(self
.set_colour_val
)
64 dlg
= wxColourDialog(self
.GetParent(), data
)
65 changed
= dlg
.ShowModal() == wxID_OK
67 data
= dlg
.GetColourData()
68 self
.SetColour(data
.GetColour().Get())
72 self
.OnChange() # moved after dlg.Destroy, since who knows what the callback will do...