Package muntjac :: Package addon :: Package colorpicker :: Module color_picker_select
[hide private]
[frames] | no frames]

Source Code for Module muntjac.addon.colorpicker.color_picker_select

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  from colorsys import hsv_to_rgb 
 17   
 18  from muntjac.addon.colorpicker.color import Color 
 19   
 20  from muntjac.ui.custom_component import CustomComponent 
 21  from muntjac.ui.vertical_layout import VerticalLayout 
 22  from muntjac.ui.select import Select 
 23   
 24  from muntjac.data.property import IValueChangeListener 
 25   
 26  from muntjac.addon.colorpicker.color_picker_grid import ColorPickerGrid 
 27  from muntjac.addon.colorpicker.color_selector import IColorSelector 
28 29 30 -class ColorPickerSelect(CustomComponent, IColorSelector, IValueChangeListener):
31 """The Class ColorPickerSelect. 32 33 @author: John Ahlroos / ITMill Oy LTd 2010 34 @author: Richard Lincoln 35 """ 36
37 - def __init__(self):
38 """Instantiates a new color picker select. 39 40 @param rows 41 the rows 42 @param columns 43 the columns 44 """ 45 super(ColorPickerSelect, self).__init__() 46 47 layout = VerticalLayout() 48 self.setCompositionRoot(layout) 49 50 self.setStyleName('colorselect') 51 self.setWidth('220px') 52 53 self._range = Select() 54 self._range.setImmediate(True) 55 self._range.setImmediate(True) 56 self._range.setNullSelectionAllowed(False) 57 self._range.setNewItemsAllowed(False) 58 self._range.setWidth('220px') 59 self._range.addListener(self, IValueChangeListener) 60 61 for Id in ColorRangePropertyId.values(): 62 self._range.addItem(Id) 63 64 layout.addComponent(self._range) 65 66 self._grid = ColorPickerGrid(self.createAllColors(14, 10)) 67 self._grid.setWidth('220px') 68 self._grid.setHeight('270px') 69 70 layout.addComponent(self._grid) 71 72 self._range.select(ColorRangePropertyId.ALL)
73 74
75 - def createAllColors(self, rows, columns):
76 """Creates the all colors. 77 78 @param rows: 79 the rows 80 @param columns: 81 the columns 82 83 @return: the color[][] 84 """ 85 colors = [([None] * columns) for _ in range(rows)] 86 87 for row in range(rows): 88 for col in range(columns): 89 90 # Create the color grid by varying the saturation and value 91 if row < rows - 1: 92 # Calculate new hue value 93 # The last row should have the black&white gradient 94 hue = col / columns 95 saturation = 1.0 96 value = 1.0 97 98 # For the upper half use value=1 and variable saturation 99 if row < rows / 2: 100 saturation = (row + 1.0) / rows / 2.0 101 else: 102 value = 1.0 - ((row - (rows / 2.0)) / rows / 2.0) 103 104 colors[row][col] = \ 105 Color(*hsv_to_rgb(hue, saturation, value)) 106 else: 107 hue = 0.0 108 saturation = 0.0 109 value = 1.0 - (col / columns) 110 111 colors[row][col] = \ 112 Color(*hsv_to_rgb(hue, saturation, value)) 113 114 return colors
115 116
117 - def createColor(self, color, rows, columns):
118 """Creates the color. 119 120 @param color: 121 the color 122 @param rows: 123 the rows 124 @param columns: 125 the columns 126 127 @return: the color[][] 128 """ 129 colors = [([None] * columns) for _ in range(rows)] 130 hsv = color.getHSV() 131 132 hue = hsv[0] 133 saturation = 1.0 134 value = 1.0 135 136 for row in range(rows): 137 for col in range(columns): 138 139 index = (row * columns) + col 140 saturation = 1.0 141 value = 1.0 142 143 if index <= (rows * columns) / 2: 144 saturation = index / (rows * columns) / 2.0 145 else: 146 index -= (rows * columns) / 2 147 value = 1.0 - (index / (rows * columns) / 2.0) 148 149 colors[row][col] = Color(*hsv_to_rgb(hue, saturation, value)) 150 151 return colors
152 153
154 - def addListener(self, listener, iface=None):
155 self._grid.addListener(listener, iface)
156 157
158 - def removeListener(self, listener, iface=None):
159 self._grid.removeListener(listener, iface)
160 161
162 - def getColor(self):
163 return self._grid.getColor()
164 165
166 - def setColor(self, color):
167 self._grid.getColor()
168 169
170 - def valueChange(self, event):
171 if self._grid is None: 172 return 173 174 if event.getProperty().getValue() == ColorRangePropertyId.ALL: 175 self._grid.setColorGrid(self.createAllColors(14, 10)) 176 177 elif event.getProperty().getValue() == ColorRangePropertyId.RED: 178 self._grid.setColorGrid(self.createColor(Color(255, 0, 0), 14, 10)) 179 180 elif event.getProperty().getValue() == ColorRangePropertyId.GREEN: 181 self._grid.setColorGrid(self.createColor(Color(0, 255, 0), 14, 10)) 182 183 elif event.getProperty().getValue() == ColorRangePropertyId.BLUE: 184 self._grid.setColorGrid(self.createColor(Color(0, 0, 255), 14, 10))
185
186 187 -class ColorRangePropertyId(object):
188 """The Enum ColorRangePropertyId.""" 189 190 ALL = None 191 RED = None 192 GREEN = None 193 BLUE = None 194
195 - def __init__(self, caption):
196 """Instantiates a new color range property id. 197 198 @param caption: 199 the caption 200 """ 201 self._caption = caption
202
203 - def __str__(self):
204 return self._caption
205 206 @classmethod
207 - def values(cls):
208 return [cls.ALL, cls.RED, cls.GREEN, cls.BLUE]
209 210 211 ColorRangePropertyId.ALL = ColorRangePropertyId('All colors') 212 ColorRangePropertyId.RED = ColorRangePropertyId('Red colors') 213 ColorRangePropertyId.GREEN = ColorRangePropertyId('Green colors') 214 ColorRangePropertyId.BLUE = ColorRangePropertyId('Blue colors') 215