3 Copyright (C) 2002 Michael Gilfix <mgilfix@eecs.tufts.edu>
5 This file is part of wxPyColourChooser.
7 This version of wxPyColourChooser is open source; you can redistribute it
8 and/or modify it under the licensed terms.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 from wxPython
.wx
import *
17 class PyColourBox(wxPanel
):
18 """A Colour Selection Box
20 The Colour selection box implements button like behavior but contains
21 a solid-filled, coloured sub-box. Placing the colour in a sub-box allows
22 for filling in the main panel's background for a high-lighting effect.
24 def __init__(self
, parent
, id, colour
=(0, 0, 0), size
=(25, 20)):
25 """Creates a new colour box instance and initializes the colour
27 wxPanel
.__init
__(self
, parent
, id,
28 size
=wxSize(size
[0], size
[1]))
30 self
.colour_box
= wxPanel(self
, -1, style
=wxSIMPLE_BORDER
)
32 sizer
= wxGridSizer(1, 1)
33 sizer
.Add(self
.colour_box
, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_CENTER_HORIZONTAL
)
34 sizer
.SetItemMinSize(self
.colour_box
, size
[0] - 5, size
[1] - 5)
35 self
.SetAutoLayout(True)
39 self
.real_bg
= self
.GetBackgroundColour()
40 self
.SetColourTuple(colour
)
42 def GetColourBox(self
):
43 """Returns a reference to the internal box object containing the
44 color. This function is useful for setting up event handlers for
46 return self
.colour_box
49 """Returns a wxColour object indicating the box's current colour."""
50 return self
.colour_box
.GetBackgroundColour()
52 def SetColour(self
, colour
):
53 """Accepts a wxColour object and sets the box's current color."""
54 self
.colour_box
.SetBackgroundColour(colour
)
55 self
.colour_box
.Refresh()
57 def SetColourTuple(self
, colour
):
58 """Sets the box's current couple to the given tuple."""
60 self
.colour_box
.SetBackgroundColour(wxColour(*self
.colour
))
64 self
.colour_box
.Update()
66 def SetHighlight(self
, val
):
67 """Accepts a boolean 'val' toggling the box's highlighting."""
68 # XXX This code has been disabled for now until I can figure out
69 # how to get this to work reliably across all platforms.
71 #A wxColourPtr is returned in windows, making this difficult
72 red
=(self
.real_bg
.Red() - 45) % 255
73 green
=(self
.real_bg
.Green() - 45) % 255
74 blue
=(self
.real_bg
.Blue() - 45) % 255
75 new_colour
= wxColour(red
, green
, blue
)
76 self
.SetBackgroundColour(new_colour
)
78 self
.SetBackgroundColour(self
.real_bg
)