]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/wx/lib/colourchooser/pypalette.py
1.
[wxWidgets.git] / wxPython / wx / lib / colourchooser / pypalette.py
... / ...
CommitLineData
1"""
2wxPyColourChooser
3Copyright (C) 2002 Michael Gilfix
4
5This file is part of wxPyColourChooser.
6
7You should have received a file COPYING containing license terms
8along with this program; if not, write to Michael Gilfix
9(mgilfix@eecs.tufts.edu) for a copy.
10
11This version of wxPyColourChooser is open source; you can redistribute it and/or
12modify it under the terms listed in the file COPYING.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY 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
23import cStringIO
24import zlib
25
26import wx
27
28import canvas
29import colorsys
30
31
32# Bitmap functions generated from img2py
33def getData():
34 return zlib.decompress(
35'x\xdaUV{<\x93{\x18\x1f:\xe3\xc8Xj\xa3a\xe6R\x9db*B.3\x93\xeb\xe9\x1c\xb7\
36\xe4\xb2r9\x8c\xe5\x12e\xc9\xda\x88\x9a\xb4\x83\xcet\xa1N\xae\x85\xe3\xb2\
37\xe1,\xb7\xc2\x8a\xd0\x99D\xc8t\xdc\x86r\x1f\x96\\\x16k:\xebC\xe78\xef\xe7\
38\xf3\xbe\xbf?\xde\xf7\xf9=\xcf\xf3\xbd<\xbf\xf7WgG;\x90,L\x16\x00\x00\x80\
39\x1c\xec\xad]\xc5+[|7\xc8\x00\xc5O\xccv\xddE\xf1\xb2\x8d`\xe5\xe0&#\xbebdpT\
40\x00`\xbb\x94\x835\xc6\x8d\x989\x97\xd5N\xca\xb3\x05\xdb\xdc\x9c[\xd0\xf4\
41\xd2\xe6\x7f\xb9\xd3\xaa\xe9\x85\xd3\xd5\xd9\xbe]\xa7\xf2\xf1\xf2\x1b\xef\
42\x1b\xe6\xb7\tF\xca\t`\xef\xf9\xb5\xa2\x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bk2G\xaf\xa2p\\\x8e\xd0\x8e\
71\t\nf\xce\xf0\x10}O\xc1\x95\x9e\x80\xa6\x91\x8d\xae0\xcf\xa0\xc7\x97-h\xc3\
72\x1f\xb8"l\x14\xcaz9\xffYl\x15.l|\xc4\x94\xdd&\x9c\x9f\xf8\xb8f7\x08cA\x1e\
73\x85\x11,\xb0\xba\xf1\x87639\xfbJ\xef~e\xf3\xdbK\xd4\xe7\xec\xa5\x92\x17\xf7\
74\x1aO\xe3*\xd5\xf3\xcbA\xef\xf4\xa4[\x1d\xaf\xd7>M-\xf1\xb9@\xea\x96x\xd9(\
75\x06Z\xec+J\xed\xe5\xd45\x95\xe1\xba\xeb\xf4\xa4\xa3i\xbb\x82}\xd0\xf6Lh\xe8\
76E4;0\x9aPk\x0emo~G\x04\xb6&u\xb31\x80\xdao\x01\xf5P\x1d\xd8\x05\x92qmV\xf6R\
77\x17\x89\x1a-\xf4\x15P*\xf9?|\xcea\xa9\x8f\x99~6\x92^\xf8\x03s\x11;v\xe2D\
78\xc4^\x1c>Q\xbfY\xd0n\xbaE\xc0b=\x91\x0c=[U\x86\xfb\x06\xb52\x92\x19M1uz<\
79\xb1\xa4r~4\x83E\xe2y\x08\x0f\nQ\x84\xe8\xfa\xfa\xea\x13\x0e\xd4\x95\x91\\\
80\x7f\xf6)\x08`\xb6\x89\xc5\x95^\xff\xe2\\\x03X\xe4\xbe\x88x\x8f\xe34\xb7\xe6\
81\xbe\t\xf8\\w\x9c\xd3\x1a\xee\x98\xeeW\x92\xad\x83\x99\xb6B\xcb\x8f\xbdD\x86\
82\xc6\xe3\xab\x1b\xb2\xdf\x08:9\xbc\x9e\xf3\xf9h\xd1\xec\xd98\xc8%\x0b\x87c\
83\xb8\xbc\x0f\xad\x89\xca\xa5\x94J\xa9\x88j\x1ddo\x91u\x84\xf3\xcd\xea\xc5\
84\xf6\x99\xab\xe0\xd7-\x92\xff:\xe6)T\x07\x0bd\x1b\xa9+9\xa4\x86\xec2F\xa1\
85\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7V\x1f\xde\x1b\x02\xb0!\x0c\xbeY&\xf6\xe9\xdd[u:0\x0f)(\xc5g\n\xd5\
114\xb6\xcc\xf0st\n\x113\x81Q\xcc\xef\xaa\x1b\x9a#\xad|\x12\x98\xd8\xf7"\xa2\
115\xa2\xd7\xdbwz+\x08\xb8\x0c\x9d&mZ\\-<\xbaG6j\x9cy@\x8ah\x10@\x8e\xd9\x12\
116\x9dK\x00\xf3\xabdo\x1f\x8b\x80\x9c\xf7i^#\xc1X\x7f-\x13R](\x17\xa0\x89p\x9c\
117L\x1a\xc5\x9c\x88U\xde\xc7\x9d\xd04\x04\x8en\x11\x8112\xbd\xf6J\x96wP\x06\
118\xcd\xa9x\x08\xf7\xc3\xfc\x0e\xceE\xef\xf9j\xd50\x19~\x02~\xf7C\x13\xae\xd8\
119\xea\x8e\xc9\x8c\xafpU\xc8\x8d\xaa\xe5\x88Q\xfan\x93\xf7\xf0(\xb0\x93\xf5NH\
120\x1f\xae\xc5\xf8\xaa\x97F4\x19;\x19\xe4=\x89\xe0\xae\x15\xc9\xb6\xfe\xe2\xce\
121\x1e\xca\xe6\x1a\n<\t\xa9].x\x03\xfd\x1c\x86Fd?\xbd\x17|z\x03\xa8\xafX[N"|\
122\x16\xa3#\x0e\x92\xf0h{^+K\x04/!\x8f\xac\xf4\xe4\xbbH\xa9.\x85q\xdd\x93\xc7\
123\xbb`\x96\xbb\xb5\xefQ\xdc\x9ch+G\xf8\xbf\xf6b\xdc\xfbww\xcf\xc7\x85\xf7\n@\
124\x8d[\xdc\x1b\x8e\xd5\x85\x1c\xf0@JG\x08\xc9;\n\xfb\x9dX\xc5\x8e\t\\\xb3g#\
125\xa0\xa2\xb7`\n\x96\x116?\xda\x83\xea\xa1\x7f.Y\x9f\xcb\xda_\x8c\xe9\x01s\
126\x0f\xf6\x03\xb7:\xa0\xc6\x94\xaat\xc4\x96r\x1c\x12\x06\x1dZ\xf7\x10V\xd5\
127\x088\x02N\xc6\xcc\x05y\xd7\xc0T\x07,c\xea\xb2\xcf\xc7=>y\x87M_\x9a\x86\x12\
128\xa5\x92\x83\n_"\x84\xff\x8b7\x95\xfeu\x02\x9c\xf7\xe4\xfacQyo\xda\xbb\t\xed\
129\xdeS\xd3\xb7\x04/j\xdb\x96\xae\xec\xd3\x01\xb8P\x8ap\x8c7\x88\xc2\xa8\xfd\
130\x1d\xd5\xd1=\xab$*\x8c\x9dd\xacu\x07\xe3\xa6X\xed\x1d\xb9eHd@\x8f\xb7\xd4V\
131\xdc\x95\x0f\xa91\xba\xe3s?\n\x12\xf2\x97\xefh\xf4\x1d\x89\x04\xccC)\x8f\x83\
132\xbf\x84\xd5\xe0A\xb7\xccC\xf9\xc3fGA\x92\xe4\x12\x89\x03\x14bb\xdfe\xd9\x7f\
133\x0f\x86\xc6R\xf9wC\x114\xe0\xdd\xae9\xc9ef\x92\xb6\x12\x1eU\'ZW\xa2\xe9\xa7\
1344\x15\xfdb\nr\x17\xf1\xe15IkA\xe5\x12aM[&\x93T\x16\xa5\x92x\xf8\xc3\xd4\xca\
135\xd8[\x96]wPNO\t!5\xaf&\xfarlLC\xdd\x00\xdd\x8e\x13qc\xea&]nAb\x8b1>)9\x047\
136\xc5\x8e\x1a\xd5\x84\x8b\x7f\x8f\x01\x0e6\x8e\xd6eV~W\xff\x01[x\x1b=' )
137
138def getBitmap():
139 return wx.BitmapFromImage(getImage())
140
141def getImage():
142 stream = cStringIO.StringIO(getData())
143 return wx.ImageFromStream(stream)
144
145class PyPalette(canvas.Canvas):
146 """The Pure-Python Palette
147
148 The PyPalette is a pure python implementation of a colour palette. The
149 palette implementation here imitates the palette layout used by MS
150 Windows and Adobe Photoshop.
151
152 The actual palette image has been embedded as an XPM for speed. The
153 actual reverse-engineered drawing algorithm is provided in the
154 GeneratePaletteBMP() method. The algorithm is tweakable by supplying
155 the granularity factor to improve speed at the cost of display
156 beauty. Since the generator isn't used in real time, no one will
157 likely care :) But if you need it for some sort of unforeseen realtime
158 application, it's there.
159 """
160
161 HORIZONTAL_STEP = 2
162 VERTICAL_STEP = 4
163
164 def __init__(self, parent, id):
165 """Creates a palette object."""
166 # Load the pre-generated palette XPM
167 wx.InitAllImageHandlers()
168 self.palette = getBitmap ()
169 canvas.Canvas.__init__ (self, parent, id, size=(200, 192))
170
171 def GetValue(self, x, y):
172 """Returns a colour value at a specific x, y coordinate pair. This
173 is useful for determining the colour found a specific mouse click
174 in an external event handler."""
175 return self.buffer.GetPixel((x, y))
176
177 def DrawBuffer(self):
178 """Draws the palette XPM into the memory buffer."""
179 #self.GeneratePaletteBMP ("foo.bmp")
180 self.buffer.DrawBitmap(self.palette, (0, 0), 0)
181
182 def HighlightPoint(self, x, y):
183 """Highlights an area of the palette with a little circle around
184 the coordinate point"""
185 colour = wx.Colour(0, 0, 0)
186 self.buffer.SetPen(wx.Pen(colour, 1, wx.SOLID))
187 self.buffer.SetBrush(wx.Brush(colour, wx.TRANSPARENT))
188 self.buffer.DrawCircle((x, y), 3)
189 self.Refresh()
190
191 def GeneratePaletteBMP(self, file_name, granularity=1):
192 """The actual palette drawing algorithm.
193
194 This used to be 100% reverse engineered by looking at the
195 values on the MS map, but has since been redone Correctly(tm)
196 according to the HSV (hue, saturation, value) colour model by
197 Charl P. Botha <http://cpbotha.net/>.
198
199 Speed is tweakable by changing the granularity factor, but
200 that affects how nice the output looks (makes the vertical
201 blocks bigger. This method was used to generate the embedded
202 XPM data."""
203 self.vertical_step = self.VERTICAL_STEP * granularity
204 width, height = self.GetSize ()
205
206 # simply iterate over hue (horizontal) and saturation (vertical)
207 value = 1.0
208 for y in range(0, height, self.vertical_step):
209 saturation = 1.0 - float(y) / float(height)
210 for x in range(0, width, self.HORIZONTAL_STEP):
211 hue = float(x) / float(width)
212 r,g,b = colorsys.hsv_to_rgb(hue, saturation, value)
213 colour = wx.Colour(int(r * 255.0), int(g * 255.0), int(b * 255.0))
214 self.buffer.SetPen(wx.Pen(colour, 1, wx.SOLID))
215 self.buffer.SetBrush(wx.Brush(colour, wx.SOLID))
216 self.buffer.DrawRectangle((x, y),
217 (self.HORIZONTAL_STEP, self.vertical_step))
218
219 # this code is now simpler (and works)
220 bitmap = self.buffer.GetBitmap()
221 image = wx.ImageFromBitmap(bitmap)
222 image.SaveFile (file_name, wx.BITMAP_TYPE_XPM)