]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/colourchooser/pypalette.py
Track and translate typenames where possible
[wxWidgets.git] / wxPython / wx / lib / colourchooser / pypalette.py
1 """
2 PyColourChooser
3 Copyright (C) 2002 Michael Gilfix
4
5 This file is part of PyColourChooser.
6
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 PyColourChooser 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 # 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net)
19 #
20 # o 2.5 compatability update.
21 #
22 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
23 #
24 # o wxPyColorChooser -> PyColorChooser
25 # o wxPyColourChooser -> PyColourChooser
26 # o Commented out wx.InitAllImageHandlers() (see comments at that
27 # point for explanation
28 #
29
30 import cStringIO
31 import zlib
32
33 import wx
34
35 import canvas
36 import colorsys
37
38
39 # Bitmap functions generated from img2py
40 def getData():
41 return zlib.decompress(
42 'x\xdaUV{<\x93{\x18\x1f:\xe3\xc8Xj\xa3a\xe6R\x9db*B.3\x93\xeb\xe9\x1c\xb7\
43 \xe4\xb2r9\x8c\xe5\x12e\xc9\xda\x88\x9a\xb4\x83\xcet\xa1N\xae\x85\xe3\xb2\
44 \xe1,\xb7\xc2\x8a\xd0\x99D\xc8t\xdc\x86r\x1f\x96\\\x16k:\xebC\xe78\xef\xe7\
45 \xf3\xbe\xbf?\xde\xf7\xf9=\xcf\xf3\xbd<\xbf\xf7WgG;\x90,L\x16\x00\x00\x80\
46 \x1c\xec\xad]\xc5+[|7\xc8\x00\xc5O\xccv\xddE\xf1\xb2\x8d`\xe5\xe0&#\xbebdpT\
47 \x00`\xbb\x94\x835\xc6\x8d\x989\x97\xd5N\xca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\xfd u:\xe7:g\x02\xc1r\x926\x9b\x7f\xe2\xf9\xe3\xed\xf1qU\
104 \xbe\xbf\xe8\x91\t\x0c0\xfb-\xe5%d\xef\x19v\x966\xec\xaaB\xe2`N[\x8c\xda\x98\
105 \xf4\xb4\x83\x13\xcc\x8a\x83\x81\xa3\x91%\xdb\xad\xab\xff\x87\xe1\xba\xda\
106 \xb5\xdb\xf0\x17\xfd\xf4F\x18KTNH\xf5J\xbc\x97\xdfB:\xa7\x96\xdf/k\x1c\xeaF_\
107 \x8c\xfc\xdfap\x1e\x99\xae8\x94b\xa1t<\xb54.3c\xd9\xe8y=u9FM;D\xa6\xc0\xea~\
108 \x0f"O\xee\x81\xdc\xa3\xb2\x1a\xa0\xa7\x02\xb9\x7f>\xfdg\x974\xc8\x0b\xbaU6P\
109 \xe7\x14\xd8\xd5 \x90\xbc\x0b\xf0\xb7\xc4\x7f\x08\xfaPl\xf5\xa7\x96\xac\xc2\
110 \x0f*\x05\xf3\x83\xe8\xce\xa7\xc4\x8c\xdbX\xa4U\x9b\xeeW\xe9\xf1\xbf\xa4Q#\
111 \xcbDQ\x18h\x02\xca\'\xca\xee),1"\x8d\xfb<\x15\xafl\xb8\xb3z\x18\x18\xaf\xb6\
112 I$\xa2\xbc\xe5\xe5J\xbe\x00\r\x08&<\x0eK\x98\x0e.[\xd1\xea`\xa9\xe2\x96\xae-\
113 d9%\xc0 \x85\xc5{c_\x03x\xaf\x8f\x98P= 0\x8e\xff\xaa\xf5>7\xfeO\x7f\x1b\xcbm\
114 \xb1W\xa7\t\x9b\xe1w\x02\xc5\xb5\x9fM/\x8d\xab\xe4u\r\x06\xa0\xd6\xc9\xb5\
115 \xf2\xb7J\x01\xda<\r\x9f\xd1\x06\x03\xea>\xab\x9d\xe6\xde\xb4\xbb\xb1\xc6\
116 \xa3EP\x1e\x17\x16\xf2\x1c\xa7\x823\xa7\xcc~\xd1\xdb\xb2\xcb\xbd\xe1\xdb\xf0\
117 W(,\xe9XD)3I\xc4\x15z_\x98\x8b\xb2v\xdb\x13\xd0\xb8\xf3U"\xb713\xaf\xa0\x1dC\
118 j\x0b\xb0\xf9\xfd\xef\x0ex\xd7l\xa5\xc1\xf7Z\xd2\x12\x1f\xbe\r\x87Shjm\xe3\
119 \x1c\x92\xbc\xc7^\x9e\xe5\x84\xf9\xb8\xcb\x88+\x12\xb4M\xee\xb0\xbb\xcd\xc4\
120 \x9c\xc7V\x1f\xde\x1b\x02\xb0!\x0c\xbeY&\xf6\xe9\xdd[u:0\x0f)(\xc5g\n\xd5\
121 \xb6\xcc\xf0st\n\x113\x81Q\xcc\xef\xaa\x1b\x9a#\xad|\x12\x98\xd8\xf7"\xa2\
122 \xa2\xd7\xdbwz+\x08\xb8\x0c\x9d&mZ\\-<\xbaG6j\x9cy@\x8ah\x10@\x8e\xd9\x12\
123 \x9dK\x00\xf3\xabdo\x1f\x8b\x80\x9c\xf7i^#\xc1X\x7f-\x13R](\x17\xa0\x89p\x9c\
124 L\x1a\xc5\x9c\x88U\xde\xc7\x9d\xd04\x04\x8en\x11\x8112\xbd\xf6J\x96wP\x06\
125 \xcd\xa9x\x08\xf7\xc3\xfc\x0e\xceE\xef\xf9j\xd50\x19~\x02~\xf7C\x13\xae\xd8\
126 \xea\x8e\xc9\x8c\xafpU\xc8\x8d\xaa\xe5\x88Q\xfan\x93\xf7\xf0(\xb0\x93\xf5NH\
127 \x1f\xae\xc5\xf8\xaa\x97F4\x19;\x19\xe4=\x89\xe0\xae\x15\xc9\xb6\xfe\xe2\xce\
128 \x1e\xca\xe6\x1a\n<\t\xa9].x\x03\xfd\x1c\x86Fd?\xbd\x17|z\x03\xa8\xafX[N"|\
129 \x16\xa3#\x0e\x92\xf0h{^+K\x04/!\x8f\xac\xf4\xe4\xbbH\xa9.\x85q\xdd\x93\xc7\
130 \xbb`\x96\xbb\xb5\xefQ\xdc\x9ch+G\xf8\xbf\xf6b\xdc\xfbww\xcf\xc7\x85\xf7\n@\
131 \x8d[\xdc\x1b\x8e\xd5\x85\x1c\xf0@JG\x08\xc9;\n\xfb\x9dX\xc5\x8e\t\\\xb3g#\
132 \xa0\xa2\xb7`\n\x96\x116?\xda\x83\xea\xa1\x7f.Y\x9f\xcb\xda_\x8c\xe9\x01s\
133 \x0f\xf6\x03\xb7:\xa0\xc6\x94\xaat\xc4\x96r\x1c\x12\x06\x1dZ\xf7\x10V\xd5\
134 \x088\x02N\xc6\xcc\x05y\xd7\xc0T\x07,c\xea\xb2\xcf\xc7=>y\x87M_\x9a\x86\x12\
135 \xa5\x92\x83\n_"\x84\xff\x8b7\x95\xfeu\x02\x9c\xf7\xe4\xfacQyo\xda\xbb\t\xed\
136 \xdeS\xd3\xb7\x04/j\xdb\x96\xae\xec\xd3\x01\xb8P\x8ap\x8c7\x88\xc2\xa8\xfd\
137 \x1d\xd5\xd1=\xab$*\x8c\x9dd\xacu\x07\xe3\xa6X\xed\x1d\xb9eHd@\x8f\xb7\xd4V\
138 \xdc\x95\x0f\xa91\xba\xe3s?\n\x12\xf2\x97\xefh\xf4\x1d\x89\x04\xccC)\x8f\x83\
139 \xbf\x84\xd5\xe0A\xb7\xccC\xf9\xc3fGA\x92\xe4\x12\x89\x03\x14bb\xdfe\xd9\x7f\
140 \x0f\x86\xc6R\xf9wC\x114\xe0\xdd\xae9\xc9ef\x92\xb6\x12\x1eU\'ZW\xa2\xe9\xa7\
141 4\x15\xfdb\nr\x17\xf1\xe15IkA\xe5\x12aM[&\x93T\x16\xa5\x92x\xf8\xc3\xd4\xca\
142 \xd8[\x96]wPNO\t!5\xaf&\xfarlLC\xdd\x00\xdd\x8e\x13qc\xea&]nAb\x8b1>)9\x047\
143 \xc5\x8e\x1a\xd5\x84\x8b\x7f\x8f\x01\x0e6\x8e\xd6eV~W\xff\x01[x\x1b=' )
144
145 def getBitmap():
146 return wx.BitmapFromImage(getImage())
147
148 def getImage():
149 stream = cStringIO.StringIO(getData())
150 return wx.ImageFromStream(stream)
151
152 class PyPalette(canvas.Canvas):
153 """The Pure-Python Palette
154
155 The PyPalette is a pure python implementation of a colour palette. The
156 palette implementation here imitates the palette layout used by MS
157 Windows and Adobe Photoshop.
158
159 The actual palette image has been embedded as an XPM for speed. The
160 actual reverse-engineered drawing algorithm is provided in the
161 GeneratePaletteBMP() method. The algorithm is tweakable by supplying
162 the granularity factor to improve speed at the cost of display
163 beauty. Since the generator isn't used in real time, no one will
164 likely care :) But if you need it for some sort of unforeseen realtime
165 application, it's there.
166 """
167
168 HORIZONTAL_STEP = 2
169 VERTICAL_STEP = 4
170
171 def __init__(self, parent, id):
172 """Creates a palette object."""
173 # Load the pre-generated palette XPM
174
175 # Leaving this in causes warning messages in some cases.
176 # It is the responsibility of the app to init the image
177 # handlers, IAW RD
178 #wx.InitAllImageHandlers()
179
180 self.palette = getBitmap ()
181 canvas.Canvas.__init__ (self, parent, id, size=(200, 192))
182
183 def GetValue(self, x, y):
184 """Returns a colour value at a specific x, y coordinate pair. This
185 is useful for determining the colour found a specific mouse click
186 in an external event handler."""
187 return self.buffer.GetPixel((x, y))
188
189 def DrawBuffer(self):
190 """Draws the palette XPM into the memory buffer."""
191 #self.GeneratePaletteBMP ("foo.bmp")
192 self.buffer.DrawBitmap(self.palette, (0, 0), 0)
193
194 def HighlightPoint(self, x, y):
195 """Highlights an area of the palette with a little circle around
196 the coordinate point"""
197 colour = wx.Colour(0, 0, 0)
198 self.buffer.SetPen(wx.Pen(colour, 1, wx.SOLID))
199 self.buffer.SetBrush(wx.Brush(colour, wx.TRANSPARENT))
200 self.buffer.DrawCircle((x, y), 3)
201 self.Refresh()
202
203 def GeneratePaletteBMP(self, file_name, granularity=1):
204 """The actual palette drawing algorithm.
205
206 This used to be 100% reverse engineered by looking at the
207 values on the MS map, but has since been redone Correctly(tm)
208 according to the HSV (hue, saturation, value) colour model by
209 Charl P. Botha <http://cpbotha.net/>.
210
211 Speed is tweakable by changing the granularity factor, but
212 that affects how nice the output looks (makes the vertical
213 blocks bigger. This method was used to generate the embedded
214 XPM data."""
215 self.vertical_step = self.VERTICAL_STEP * granularity
216 width, height = self.GetSize ()
217
218 # simply iterate over hue (horizontal) and saturation (vertical)
219 value = 1.0
220 for y in range(0, height, self.vertical_step):
221 saturation = 1.0 - float(y) / float(height)
222 for x in range(0, width, self.HORIZONTAL_STEP):
223 hue = float(x) / float(width)
224 r,g,b = colorsys.hsv_to_rgb(hue, saturation, value)
225 colour = wx.Colour(int(r * 255.0), int(g * 255.0), int(b * 255.0))
226 self.buffer.SetPen(wx.Pen(colour, 1, wx.SOLID))
227 self.buffer.SetBrush(wx.Brush(colour, wx.SOLID))
228 self.buffer.DrawRectangle((x, y),
229 (self.HORIZONTAL_STEP, self.vertical_step))
230
231 # this code is now simpler (and works)
232 bitmap = self.buffer.GetBitmap()
233 image = wx.ImageFromBitmap(bitmap)
234 image.SaveFile (file_name, wx.BITMAP_TYPE_XPM)