1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 from StringIO import StringIO
17
18 from math import pi
19
20 from datetime import datetime as Date
21
22 from muntjac.addon.colorpicker.color import Color
23
24 from muntjac.application import Application
25
26 from muntjac.ui.check_box import CheckBox
27 from muntjac.ui.window import Window
28 from muntjac.ui.embedded import Embedded
29 from muntjac.ui.panel import Panel
30 from muntjac.ui.vertical_layout import VerticalLayout
31 from muntjac.ui.horizontal_layout import HorizontalLayout
32 from muntjac.ui.grid_layout import GridLayout
33 from muntjac.ui.button import IClickListener
34 from muntjac.ui.alignment import Alignment
35
36 from muntjac.terminal.stream_resource import IStreamSource, StreamResource
37
38 from muntjac.addon.colorpicker.color_picker \
39 import ColorPicker, ButtonStyle, IColorChangeListener
40
41 from muntjac.addon.canvas.canvas import Canvas
42
43
45 """Testing application for the ColorPicker.
46
47 @author: John Ahlroos / ITMill Oy Ltd 2010
48 @author: Richard Lincoln
49 """
50
51 _VERSION = '1.1.2'
52
54 super(ColorPickerApplication, self).__init__()
55
56
57 self._foregroundColor = Color.BLACK
58
59
60 self._backgroundColor = Color.WHITE
61
62
63 self._display = None
64
65 self._mainLayout = None
66
67 self._colorpicker1 = None
68 self._colorpicker2 = None
69 self._colorpicker3 = None
70 self._colorpicker4 = None
71 self._colorpicker5 = None
72 self._colorpicker6 = None
73
74 self._rgbVisible = True
75 self._hsvVisible = True
76 self._swaVisible = True
77 self._historyVisible = True
78 self._txtfieldVisible = True
79
80 self._rgbBox = CheckBox('RGB tab visible')
81 self._hsvBox = CheckBox('HSV tab visible')
82 self._swaBox = CheckBox('Swatches tab visible')
83 self._hisBox = CheckBox('History visible')
84 self._txtBox = CheckBox('CSS field visible')
85
86
131
132
134
135 main = Window()
136 main.setWidth('1000px')
137 self.setMainWindow(main)
138
139
140
141 self._display = Canvas()
142 self._display.setWidth('270px')
143 self._display.setHeight('270px')
144
145
146 self._mainLayout = mainLayout = HorizontalLayout()
147 mainLayout.setMargin(True)
148 mainLayout.setSpacing(True)
149 main.setContent(mainLayout)
150
151 layout = VerticalLayout()
152 layout.setWidth('450px')
153 layout.setSpacing(True)
154
155 optPanel = Panel('Customize the color picker popup window',
156 GridLayout(3, 2))
157 optPanel.getContent().setSizeFull()
158 optPanel.getContent().setMargin(True)
159 optPanel.getContent().setSpacing(True)
160
161 self._rgbBox.addListener(RgbClickListener(self), IClickListener)
162 self._rgbBox.setValue(self._rgbVisible)
163 self._rgbBox.setImmediate(True)
164 optPanel.getContent().addComponent(self._rgbBox)
165
166 self._hsvBox.addListener(HsvClickListener(self), IClickListener)
167 self._hsvBox.setValue(self._hsvVisible)
168 self._hsvBox.setImmediate(True)
169 optPanel.getContent().addComponent(self._hsvBox)
170
171 self._swaBox.addListener(SwaClickListener(self), IClickListener)
172 self._swaBox.setValue(self._swaVisible)
173 self._swaBox.setImmediate(True)
174 optPanel.getContent().addComponent(self._swaBox)
175
176 self._hisBox.addListener(HisClickListener(self), IClickListener)
177 self._hisBox.setValue(self._historyVisible)
178 self._hisBox.setImmediate(True)
179 optPanel.getContent().addComponent(self._hisBox)
180
181 self._txtBox.addListener(TxtClickListener(self), IClickListener)
182 self._txtBox.setValue(self._txtfieldVisible)
183 self._txtBox.setImmediate(True)
184 optPanel.getContent().addComponent(self._txtBox)
185
186 layout.addComponent(optPanel)
187
188 panel1 = Panel(
189 'Button like colorpicker with current color and CSS code',
190 HorizontalLayout())
191 panel1.getContent().setSizeFull()
192 panel1.getContent().setMargin(True)
193
194 self._colorpicker1 = ColorPicker('Foreground', self._foregroundColor)
195 self._colorpicker1.setWidth('100px')
196 self._colorpicker1.addListener(self)
197 panel1.addComponent(self._colorpicker1)
198 panel1.getContent().setComponentAlignment(self._colorpicker1,
199 Alignment.MIDDLE_CENTER)
200
201 self._colorpicker2 = ColorPicker('Background', self._backgroundColor)
202 self._colorpicker2.addListener(self)
203 self._colorpicker2.setWidth('100px')
204 panel1.addComponent(self._colorpicker2)
205 panel1.getContent().setComponentAlignment(self._colorpicker2,
206 Alignment.MIDDLE_CENTER)
207 layout.addComponent(panel1)
208
209 panel2 = Panel(
210 'Button like colorpicker with current color and custom caption',
211 HorizontalLayout())
212 panel2.getContent().setSizeFull()
213 panel2.getContent().setMargin(True)
214 self._colorpicker3 = ColorPicker('Foreground', self._foregroundColor)
215 self._colorpicker3.addListener(self)
216 self._colorpicker3.setWidth('120px')
217 self._colorpicker3.setButtonCaption('Foreground')
218 panel2.addComponent(self._colorpicker3)
219 panel2.getContent().setComponentAlignment(self._colorpicker3,
220 Alignment.MIDDLE_CENTER)
221
222 self._colorpicker4 = ColorPicker('Background', self._backgroundColor)
223 self._colorpicker4.addListener(self)
224 self._colorpicker4.setWidth('120px')
225 self._colorpicker4.setButtonCaption('Background')
226 panel2.addComponent(self._colorpicker4)
227 panel2.getContent().setComponentAlignment(self._colorpicker4,
228 Alignment.MIDDLE_CENTER)
229 layout.addComponent(panel2)
230
231 panel3 = Panel(
232 'Color area color picker with caption',
233 HorizontalLayout())
234 panel3.getContent().setSizeFull()
235 panel3.getContent().setMargin(True)
236
237 self._colorpicker5 = ColorPicker('Foreground', self._foregroundColor)
238 self._colorpicker5.setCaption('Foreground')
239 self._colorpicker5.addListener(self)
240 self._colorpicker5.setButtonStyle(ButtonStyle.BUTTON_AREA)
241 panel3.addComponent(self._colorpicker5)
242 panel3.getContent().setComponentAlignment(self._colorpicker5,
243 Alignment.MIDDLE_CENTER)
244
245 self._colorpicker6 = ColorPicker('Background', self._backgroundColor)
246 self._colorpicker6.setCaption('Background')
247 self._colorpicker6.addListener(self)
248 self._colorpicker6.setButtonStyle(ButtonStyle.BUTTON_AREA)
249 panel3.addComponent(self._colorpicker6)
250 panel3.getContent().setComponentAlignment(self._colorpicker6,
251 Alignment.MIDDLE_CENTER)
252 layout.addComponent(panel3)
253
254 mainLayout.addComponent(layout)
255 mainLayout.addComponent(self._display)
256
257 self.updateDisplay(self._foregroundColor, self._backgroundColor)
258
259
289
290
327
328
331
332
337
338
344
345
351
352
358
359
365
366
372
373
375 """This class is used to represent the preview of the color selection."""
376
378 """Instantiates a new my image source.
379
380 @param fg:
381 the foreground color
382 @param bg:
383 the background color
384 """
385 self._imagebuffer = None
386
387 self._fgColor = fg
388 self._bgColor = bg
389
390
392 from PIL import Image, ImageDraw
393
394
395 image = Image.new("RGB", (270, 270))
396 drawable = ImageDraw.Draw(image)
397 drawable.rectangle([0, 0, 270, 270], fill=str(Color.BLUE))
398 drawable.rectangle([1, 1, 268, 268], fill=str(self._bgColor))
399 drawable.ellipse([25, 25, 245, 245], fill=str(self._fgColor))
400 drawable.text((50, 100),
401 'r=' + str(self._fgColor.getRed()) +
402 ',g=' + str(self._fgColor.getGreen()) +
403 ',b=' + str(self._fgColor.getBlue()), fill=str(Color.BLACK))
404 drawable.text((5, 15),
405 'r=' + str(self._bgColor.getRed()) +
406 ',g=' + str(self._bgColor.getGreen()) +
407 ',b=' + str(self._bgColor.getBlue()), fill=str(Color.BLACK))
408
409 del drawable
410
411 try:
412
413 self._imagebuffer = StringIO()
414 image.save(self._imagebuffer, 'PNG')
415 return self._imagebuffer
416 except IOError:
417 return None
418
419
420 if __name__ == '__main__':
421 from muntjac.main import muntjac
422 muntjac(ColorPickerApplication, nogui=True, forever=True, debug=True)
423