]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/lib/colourchooser/pycolourslider.py
Lots of demo tweaks for API updates, bug fixes and new images for the
[wxWidgets.git] / wxPython / wx / lib / colourchooser / pycolourslider.py
CommitLineData
d14a1e28
RD
1"""
2wxPyColourChooser
3Copyright (C) 2002 Michael Gilfix
1fded56b 4
d14a1e28 5This file is part of wxPyColourChooser.
1fded56b 6
d14a1e28
RD
7You should have received a file COPYING containing license terms
8along with this program; if not, write to Michael Gilfix
9(mgilfix@eecs.tufts.edu) for a copy.
10
11This version of wxPyColourChooser is open source; you can redistribute it and/or
12modify it under the terms listed in the file COPYING.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17"""
18
19import canvas
20import colorsys
21from wxPython.wx import *
22
23class PyColourSlider(canvas.Canvas):
24 """A Pure-Python Colour Slider
25
26 The colour slider displays transitions from value 0 to value 1 in
27 HSV, allowing the user to select a colour within the transition
28 spectrum.
29
30 This class is best accompanying by a wxSlider that allows the user
31 to select a particular colour shade.
32 """
33
34 HEIGHT = 172
35 WIDTH = 12
36
37 def __init__(self, parent, id, colour=None):
38 """Creates a blank slider instance. A colour must be set before the
39 slider will be filled in."""
40 # Set the base colour first since our base class calls the buffer
41 # drawing function
42 self.SetBaseColour(colour)
43
44 canvas.Canvas.__init__(self, parent, id,
45 size=wxSize(self.WIDTH, self.HEIGHT))
46
47 def SetBaseColour(self, colour):
48 """Sets the base, or target colour, to use as the central colour
49 when calculating colour transitions."""
50 self.base_colour = colour
51
52 def GetBaseColour(self):
53 """Return the current colour used as a colour base for filling out
54 the slider."""
55 return self.base_colour
56
57 def GetValue(self, pos):
58 """Returns the colour value for a position on the slider. The position
59 must be within the valid height of the slider, or results can be
60 unpredictable."""
61 return self.buffer.GetPixel(0, pos)
62
63 def DrawBuffer(self):
64 """Actual implementation of the widget's drawing. We simply draw
65 from value 0.0 to value 1.0 in HSV."""
66 if self.base_colour is None:
67 return
68
69 target_red = self.base_colour.Red()
70 target_green = self.base_colour.Green()
71 target_blue = self.base_colour.Blue()
72
73 h,s,v = colorsys.rgb_to_hsv(target_red / 255.0, target_green / 255.0,
74 target_blue / 255.0)
75 v = 1.0
76 vstep = 1.0 / self.HEIGHT
77 for y_pos in range(0, self.HEIGHT):
78 r,g,b = [c * 255.0 for c in colorsys.hsv_to_rgb(h,s,v)]
79 colour = wxColour(int(r), int(g), int(b))
80 self.buffer.SetPen(wxPen(colour, 1, wxSOLID))
81 self.buffer.DrawRectangle(0, y_pos, 15, 1)
82 v = v - vstep