]>
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 PyColourChooser. 
   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 PyColourChooser 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. 
  23 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
  25 # o wxPyColorChooser -> PyColorChooser 
  26 # o wxPyColourChooser -> PyColourChooser 
  34 class PyColourSlider(canvas
.Canvas
): 
  35     """A Pure-Python Colour Slider 
  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 
  41     This class is best accompanying by a wxSlider that allows the user 
  42     to select a particular colour shade. 
  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 
  53         self
.SetBaseColour(colour
) 
  55         canvas
.Canvas
.__init
__(self
, parent
, id, size
=(self
.WIDTH
, self
.HEIGHT
)) 
  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
 
  62     def GetBaseColour(self
): 
  63         """Return the current colour used as a colour base for filling out 
  65         return self
.base_colour
 
  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 
  71         return self
.buffer.GetPixel(0, pos
) 
  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: 
  79         target_red 
= self
.base_colour
.Red() 
  80         target_green 
= self
.base_colour
.Green() 
  81         target_blue 
= self
.base_colour
.Blue() 
  83         h
,s
,v 
= colorsys
.rgb_to_hsv(target_red 
/ 255.0, target_green 
/ 255.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
)] 
  89             colour 
= wx
.Colour(int(r
), int(g
), int(b
)) 
  90             self
.buffer.SetPen(wx
.Pen(colour
, 1, wx
.SOLID
)) 
  91             self
.buffer.DrawRectangle(0, y_pos
, 15, 1)