]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | """ |
2 | wxPyColourChooser | |
3 | Copyright (C) 2002 Michael Gilfix | |
1fded56b | 4 | |
d14a1e28 | 5 | This file is part of wxPyColourChooser. |
1fded56b | 6 | |
d14a1e28 RD |
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. | |
10 | ||
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. | |
13 | ||
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. | |
17 | """ | |
18 | ||
b881fc78 RD |
19 | # 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
20 | # | |
21 | # o 2.5 compatability update. | |
22 | # | |
23 | ||
24 | import wx | |
25 | ||
26 | import canvas | |
27 | import colorsys | |
d14a1e28 RD |
28 | |
29 | class PyColourSlider(canvas.Canvas): | |
30 | """A Pure-Python Colour Slider | |
31 | ||
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 | |
34 | spectrum. | |
35 | ||
36 | This class is best accompanying by a wxSlider that allows the user | |
37 | to select a particular colour shade. | |
38 | """ | |
39 | ||
40 | HEIGHT = 172 | |
41 | WIDTH = 12 | |
42 | ||
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 | |
47 | # drawing function | |
48 | self.SetBaseColour(colour) | |
49 | ||
b881fc78 | 50 | canvas.Canvas.__init__(self, parent, id, size=(self.WIDTH, self.HEIGHT)) |
d14a1e28 RD |
51 | |
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 | |
56 | ||
57 | def GetBaseColour(self): | |
58 | """Return the current colour used as a colour base for filling out | |
59 | the slider.""" | |
60 | return self.base_colour | |
61 | ||
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 | |
65 | unpredictable.""" | |
7722248d | 66 | return self.buffer.GetPixel((0, pos)) |
d14a1e28 RD |
67 | |
68 | def DrawBuffer(self): | |
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: | |
72 | return | |
73 | ||
74 | target_red = self.base_colour.Red() | |
75 | target_green = self.base_colour.Green() | |
76 | target_blue = self.base_colour.Blue() | |
77 | ||
78 | h,s,v = colorsys.rgb_to_hsv(target_red / 255.0, target_green / 255.0, | |
79 | target_blue / 255.0) | |
80 | v = 1.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)] | |
b881fc78 RD |
84 | colour = wx.Colour(int(r), int(g), int(b)) |
85 | self.buffer.SetPen(wx.Pen(colour, 1, wx.SOLID)) | |
7722248d | 86 | self.buffer.DrawRectangle((0, y_pos), (15, 1)) |
d14a1e28 | 87 | v = v - vstep |