]>
Commit | Line | Data |
---|---|---|
d14a1e28 | 1 | """ |
d4b73b1b | 2 | PyColourChooser |
d14a1e28 | 3 | Copyright (C) 2002 Michael Gilfix |
1fded56b | 4 | |
d4b73b1b | 5 | This file is part of PyColourChooser. |
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 | ||
d4b73b1b | 11 | This version of PyColourChooser is open source; you can redistribute it and/or |
d14a1e28 RD |
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 | # | |
d4b73b1b RD |
23 | # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
24 | # | |
25 | # o wxPyColorChooser -> PyColorChooser | |
26 | # o wxPyColourChooser -> PyColourChooser | |
27 | # | |
b881fc78 RD |
28 | |
29 | import wx | |
30 | ||
31 | import canvas | |
32 | import colorsys | |
d14a1e28 RD |
33 | |
34 | class PyColourSlider(canvas.Canvas): | |
35 | """A Pure-Python Colour Slider | |
36 | ||
37 | The colour slider displays transitions from value 0 to value 1 in | |
38 | HSV, allowing the user to select a colour within the transition | |
39 | spectrum. | |
40 | ||
41 | This class is best accompanying by a wxSlider that allows the user | |
42 | to select a particular colour shade. | |
43 | """ | |
44 | ||
45 | HEIGHT = 172 | |
46 | WIDTH = 12 | |
47 | ||
48 | def __init__(self, parent, id, colour=None): | |
49 | """Creates a blank slider instance. A colour must be set before the | |
50 | slider will be filled in.""" | |
51 | # Set the base colour first since our base class calls the buffer | |
52 | # drawing function | |
53 | self.SetBaseColour(colour) | |
54 | ||
b881fc78 | 55 | canvas.Canvas.__init__(self, parent, id, size=(self.WIDTH, self.HEIGHT)) |
d14a1e28 RD |
56 | |
57 | def SetBaseColour(self, colour): | |
58 | """Sets the base, or target colour, to use as the central colour | |
59 | when calculating colour transitions.""" | |
60 | self.base_colour = colour | |
61 | ||
62 | def GetBaseColour(self): | |
63 | """Return the current colour used as a colour base for filling out | |
64 | the slider.""" | |
65 | return self.base_colour | |
66 | ||
67 | def GetValue(self, pos): | |
68 | """Returns the colour value for a position on the slider. The position | |
69 | must be within the valid height of the slider, or results can be | |
70 | unpredictable.""" | |
d7403ad2 | 71 | return self.buffer.GetPixel(0, pos) |
d14a1e28 RD |
72 | |
73 | def DrawBuffer(self): | |
74 | """Actual implementation of the widget's drawing. We simply draw | |
75 | from value 0.0 to value 1.0 in HSV.""" | |
76 | if self.base_colour is None: | |
77 | return | |
78 | ||
79 | target_red = self.base_colour.Red() | |
80 | target_green = self.base_colour.Green() | |
81 | target_blue = self.base_colour.Blue() | |
82 | ||
83 | h,s,v = colorsys.rgb_to_hsv(target_red / 255.0, target_green / 255.0, | |
84 | target_blue / 255.0) | |
85 | v = 1.0 | |
86 | vstep = 1.0 / self.HEIGHT | |
87 | for y_pos in range(0, self.HEIGHT): | |
88 | r,g,b = [c * 255.0 for c in colorsys.hsv_to_rgb(h,s,v)] | |
b881fc78 RD |
89 | colour = wx.Colour(int(r), int(g), int(b)) |
90 | self.buffer.SetPen(wx.Pen(colour, 1, wx.SOLID)) | |
d7403ad2 | 91 | self.buffer.DrawRectangle(0, y_pos, 15, 1) |
d14a1e28 | 92 | v = v - vstep |