]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/imageutils.py
fixed wxVsnprintf() to write as much as it can if the output buffer is too short
[wxWidgets.git] / wxPython / wx / lib / imageutils.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.imageutils
3 # Purpose: A collection of functions for simple image manipulations
4 #
5 # Author: Robb Shecter
6 #
7 # Created: 7-Nov-2002
8 # RCS-ID: $Id$
9 # Copyright: (c) 2002 by
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------
12
13 import wx
14
15 def grayOut(anImage):
16 """
17 Convert the given image (in place) to a grayed-out
18 version, appropriate for a 'disabled' appearance.
19 """
20 factor = 0.7 # 0 < f < 1. Higher is grayer.
21 if anImage.HasMask():
22 maskColor = (anImage.GetMaskRed(), anImage.GetMaskGreen(), anImage.GetMaskBlue())
23 else:
24 maskColor = None
25 data = map(ord, list(anImage.GetData()))
26
27 for i in range(0, len(data), 3):
28 pixel = (data[i], data[i+1], data[i+2])
29 pixel = makeGray(pixel, factor, maskColor)
30 for x in range(3):
31 data[i+x] = pixel[x]
32 anImage.SetData(''.join(map(chr, data)))
33
34
35 def makeGray((r,g,b), factor, maskColor):
36 """
37 Make a pixel grayed-out. If the pixel
38 matches the maskColor, it won't be
39 changed.
40 """
41 if (r,g,b) != maskColor:
42 return map(lambda x: int((230 - x) * factor) + x, (r,g,b))
43 else:
44 return (r,g,b)
45
46
47
48 def stepColour(c, step):
49 """
50 stepColour is a utility function that simply darkens or lightens a
51 color, based on the specified step value. A step of 0 is
52 completely black and a step of 200 is totally white, and 100
53 results in the same color as was passed in.
54 """
55 def _blendColour(fg, bg, dstep):
56 result = bg + (dstep * (fg - bg))
57 if result < 0:
58 result = 0
59 if result > 255:
60 result = 255
61 return result
62
63 if step == 100:
64 return c
65
66 r = c.Red()
67 g = c.Green()
68 b = c.Blue()
69
70 # step is 0..200 where 0 is completely black
71 # and 200 is completely white and 100 is the same
72 # convert that to a range of -1.0 .. 1.0
73 step = min(step, 200)
74 step = max(step, 0)
75 dstep = (step - 100.0)/100.0
76
77 if step > 100:
78 # blend with white
79 bg = 255.0
80 dstep = 1.0 - dstep # 0 = transparent fg; 1 = opaque fg
81 else:
82 # blend with black
83 bg = 0.0
84 dstep = 1.0 + dstep; # 0 = transparent fg; 1 = opaque fg
85
86 r = _blendColour(r, bg, dstep)
87 g = _blendColour(g, bg, dstep)
88 b = _blendColour(b, bg, dstep)
89
90 return wx.Colour(int(r), int(g), int(b))
91