]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/colourchooser/pycolourslider.py
3 Copyright (C) 2002 Michael Gilfix
5 This file is part of wxPyColourChooser.
7 You should have received a file COPYING containing license terms
8 along with this program; if not, write to Michael Gilfix
9 (mgilfix@eecs.tufts.edu) for a copy.
11 This version of wxPyColourChooser is open source; you can redistribute it and/or
12 modify it under the terms listed in the file COPYING.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 # 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net)
21 # o 2.5 compatability update.
29 class PyColourSlider(canvas
.Canvas
):
30 """A Pure-Python Colour Slider
32 The colour slider displays transitions from value 0 to value 1 in
33 HSV, allowing the user to select a colour within the transition
36 This class is best accompanying by a wxSlider that allows the user
37 to select a particular colour shade.
43 def __init__(self
, parent
, id, colour
=None):
44 """Creates a blank slider instance. A colour must be set before the
45 slider will be filled in."""
46 # Set the base colour first since our base class calls the buffer
48 self
.SetBaseColour(colour
)
50 canvas
.Canvas
.__init
__(self
, parent
, id, size
=(self
.WIDTH
, self
.HEIGHT
))
52 def SetBaseColour(self
, colour
):
53 """Sets the base, or target colour, to use as the central colour
54 when calculating colour transitions."""
55 self
.base_colour
= colour
57 def GetBaseColour(self
):
58 """Return the current colour used as a colour base for filling out
60 return self
.base_colour
62 def GetValue(self
, pos
):
63 """Returns the colour value for a position on the slider. The position
64 must be within the valid height of the slider, or results can be
66 return self
.buffer.GetPixel((0, pos
))
69 """Actual implementation of the widget's drawing. We simply draw
70 from value 0.0 to value 1.0 in HSV."""
71 if self
.base_colour
is None:
74 target_red
= self
.base_colour
.Red()
75 target_green
= self
.base_colour
.Green()
76 target_blue
= self
.base_colour
.Blue()
78 h
,s
,v
= colorsys
.rgb_to_hsv(target_red
/ 255.0, target_green
/ 255.0,
81 vstep
= 1.0 / self
.HEIGHT
82 for y_pos
in range(0, self
.HEIGHT
):
83 r
,g
,b
= [c
* 255.0 for c
in colorsys
.hsv_to_rgb(h
,s
,v
)]
84 colour
= wx
.Colour(int(r
), int(g
), int(b
))
85 self
.buffer.SetPen(wx
.Pen(colour
, 1, wx
.SOLID
))
86 self
.buffer.DrawRectangle((0, y_pos
), (15, 1))