From: Robin Dunn Date: Fri, 19 Jan 2007 19:51:08 +0000 (+0000) Subject: Added stepColour utility function X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/fd363e1abcc9902ddc5a0d63d974fa78df3f3e31 Added stepColour utility function git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44260 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/wxPython/tests/test_stepColours.py b/wxPython/tests/test_stepColours.py new file mode 100644 index 0000000000..52bb72fd29 --- /dev/null +++ b/wxPython/tests/test_stepColours.py @@ -0,0 +1,56 @@ +import wx +import wx.lib.colourselect as cs +import wx.lib.imageutils as iu + +class TestPanel(wx.Panel): + def __init__(self, *args, **kw): + wx.Panel.__init__(self, *args, **kw) + + self.colour = wx.NamedColour("purple") + + self.cpnl = wx.Panel(self, size=(100,100), style=wx.SIMPLE_BORDER) + self.slider = wx.Slider(self, + minValue=0, + value=100, + maxValue=200, + size=(300, -1), + style=wx.SL_HORIZONTAL | wx.SL_LABELS) + csel = cs.ColourSelect(self, colour=self.colour) + + sizer = wx.BoxSizer(wx.VERTICAL) + sizer.Add(self.cpnl, 0, wx.ALL, 20) + sizer.Add(self.slider, 0, wx.LEFT, 20) + sizer.Add(csel, 0, wx.ALL, 20) + self.SetSizer(sizer) + + self.slider.Bind(wx.EVT_SCROLL, self.OnSliderChanged) + self.Bind(cs.EVT_COLOURSELECT, self.OnColourSelect) + + self.UpdatePanel() + + + def UpdatePanel(self): + val = self.slider.GetValue() + + colour = iu.stepColour(self.colour, val) + + self.cpnl.SetBackgroundColour(colour) + self.cpnl.Refresh() + + + def OnSliderChanged(self, evt): + self.UpdatePanel() + + + def OnColourSelect(self, evt): + self.colour = evt.GetValue() + self.UpdatePanel() + + + +app = wx.App() +frm = wx.Frame(None, title="Stepping Colours") +pnl = TestPanel(frm) +frm.Show() +app.MainLoop() + diff --git a/wxPython/wx/lib/imageutils.py b/wxPython/wx/lib/imageutils.py index d2cd60e037..e894feb6aa 100644 --- a/wxPython/wx/lib/imageutils.py +++ b/wxPython/wx/lib/imageutils.py @@ -10,8 +10,7 @@ # Licence: wxWindows license #---------------------------------------------------------------------- -from __future__ import nested_scopes - +import wx def grayOut(anImage): """ @@ -43,3 +42,50 @@ def makeGray((r,g,b), factor, maskColor): return map(lambda x: int((230 - x) * factor) + x, (r,g,b)) else: return (r,g,b) + + + +def stepColour(c, step): + """ + stepColour is a utility function that simply darkens or lightens a + color, based on the specified step value. A step of 0 is + completely black and a step of 200 is totally white, and 100 + results in the same color as was passed in. + """ + def _blendColour(fg, bg, dstep): + result = bg + (dstep * (fg - bg)) + if result < 0: + result = 0 + if result > 255: + result = 255 + return result + + if step == 100: + return c + + r = c.Red() + g = c.Green() + b = c.Blue() + + # step is 0..200 where 0 is completely black + # and 200 is completely white and 100 is the same + # convert that to a range of -1.0 .. 1.0 + step = min(step, 200) + step = max(step, 0) + dstep = (step - 100.0)/100.0 + + if step > 100: + # blend with white + bg = 255.0 + dstep = 1.0 - dstep # 0 = transparent fg; 1 = opaque fg + else: + # blend with black + bg = 0.0 + dstep = 1.0 + dstep; # 0 = transparent fg; 1 = opaque fg + + r = _blendColour(r, bg, dstep) + g = _blendColour(g, bg, dstep) + b = _blendColour(b, bg, dstep) + + return wx.Colour(int(r), int(g), int(b)) +