Package muntjac :: Package addon :: Package invient :: Module gradient
[hide private]
[frames] | no frames]

Source Code for Module muntjac.addon.invient.gradient

  1  # @INVIENT_COPYRIGHT@ 
  2  #  
  3  # Licensed under the Apache License, Version 2.0 (the "License");  
  4  # you may not use this file except in compliance with the License.  
  5  # You may obtain a copy of the License at  
  6  #  
  7  #     http://www.apache.org/licenses/LICENSE-2.0  
  8  #  
  9  # Unless required by applicable law or agreed to in writing, software  
 10  # distributed under the License is distributed on an "AS IS" BASIS,  
 11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 12  # See the License for the specific language governing permissions and  
 13  # limitations under the License. 
 14   
 15  from muntjac.addon.invient.paint import IPaint 
 16   
 17  try: 
 18      from cStringIO import StringIO 
 19  except ImportError: 
 20      from StringIO import StringIO 
21 22 23 -class Unit(object):
24 25 NUMBER = None 26 PERCENT = None 27
28 - def __init__(self, name, symbol):
29 self._name = name 30 self._symbol = symbol
31
32 - def getName(self):
33 return self._name
34
35 - def getSymbol(self):
36 return self._symbol
37 38 @classmethod
39 - def values(cls):
40 return [cls.NUMBER, cls.PERCENT]
41 42 Unit.NUMBER = Unit('number', '') 43 Unit.PERCENT = Unit('percent', '%')
44 45 46 -class IColorStop(object):
47 """Represents a stop-value and a color. The stop-value should be in range 48 (0.0-1.0). The color of the gradient at each stop is the color specified 49 for that stop. Between each such stop, the colors and the alpha component 50 will be linearly interpolated over the RGBA. 51 52 @author: Invient 53 @author: Richard Lincoln 54 """ 55
56 - def getStopAt(self):
57 raise NotImplementedError
58
59 - def getStopAtUnit(self):
60 raise NotImplementedError
61
62 - def getColor(self):
63 raise NotImplementedError
64
65 66 -class IGradient(IPaint):
67 """The Gradient defines a way to fill a shape with a linear color gradient 68 pattern. 69 70 @author: Invient 71 @author: Richard Lincoln 72 """ 73
74 - def getxStart(self):
75 """Returns the x-coordinate of a point at which linear gradient 76 starts. 77 @return: the x-coordinate of a point at which linear gradient 78 starts. 79 """ 80 raise NotImplementedError
81
82 - def getxStartUnit(self):
83 """Returns the unit of x-coordinate of a point at which linear gradient starts. 84 @return: the unit of x-coordinate of a point at which linear gradient starts. 85 """ 86 raise NotImplementedError
87
88 - def getyStart(self):
89 """Returns the y-coordinate of a point at which linear gradient starts. 90 @return: the y-coordinate of a point at which linear gradient starts. 91 """ 92 raise NotImplementedError
93
94 - def getyStartUnit(self):
95 """Returns the unit of y-coordinate of a point at which linear gradient starts. 96 @return: the unit of y-coordinate of a point at which linear gradient starts. 97 """ 98 raise NotImplementedError
99
100 - def getxEnd(self):
101 """Returns the x-coordinate of a point at which linear gradient 102 ends. 103 @return: the x-coordinate of a point at which linear gradient 104 ends. 105 """ 106 raise NotImplementedError
107
108 - def getxEndUnit(self):
109 """Returns the unit of x-coordinate of a point at which linear gradient ends. 110 @return: the unit of x-coordinate of a point at which linear gradient ends. 111 """ 112 raise NotImplementedError
113
114 - def getyEnd(self):
115 """Returns the x-coordinate of a point at which linear gradient ends. 116 @return: the x-coordinate of a point at which linear gradient ends. 117 """ 118 raise NotImplementedError
119
120 - def getyEndUnit(self):
121 """Returns the unit of y-coordinate of a point at which linear gradient ends. 122 @return: the unit of y-coordinate of a point at which linear gradient ends. 123 """ 124 raise NotImplementedError
125
126 - def getColorStops(self):
127 """Returns a list of colorstops associated with this gradient. 128 @return: a list of colorstops associated with this gradient. 129 @see: L{IColorStop} 130 """ 131 raise NotImplementedError
132
133 134 -class LinearGradient(IGradient):
135 """Represents linear gradient where points of a linear gradient specify a 136 line. For more details on gradient, refer to CSS 3 gradient 137 documentation. 138 139 @author: Invient 140 @author: Richard Lincoln 141 """ 142
143 - def __init__(self, xStart, xStartUnit_or_yStart, yStart_or_xEnd, 144 yStartUnit_or_yEnd, xEnd_or_colorStops, xEndUnit=None, 145 yEnd=None, yEndUnit=None, colorStops=None):
146 """Creates a LinearGradient with the specified xStart, xEnd, yStart 147 and yEnd values with default {@link Unit} value number. 148 149 @param xStart: 150 the x-coordinate of a point at which linear gradient 151 starts. 152 @param xStartUnit_or_yStart: 153 the unit for the xStart value. It can have one of the two 154 values Unit.NUMBER or Unit.PERCENT. If it is null then the 155 default value is Unit.NUMBER. Or the y-coordinate of a point 156 at which linear gradient starts. 157 @param yStart_or_xEnd: 158 the y-coordinate of a point at which linear gradient 159 starts or the x-coordinate of a point at which linear 160 gradient ends. 161 @param yStartUnit_or_yEnd: 162 the unit for the yStart value. It can have one of the two 163 values Unit.NUMBER or Unit.PERCENT. If it is null then the 164 default value is Unit.NUMBER. Or the y-coordinate of a point 165 at which linear gradient ends. 166 @param xEnd_or_colorStops: 167 the x-coordinate of a point at which linear gradient ends or 168 the list of colorstops for the linear gradient. 169 @param xEndUnit: 170 the unit for the xEnd value. It can have one of the two 171 values Unit.NUMBER or Unit.PERCENT. If it is null then the 172 default value is Unit.NUMBER. 173 @param yEnd: 174 the y-coordinate of a point at which linear gradient ends. 175 @param yEndUnit 176 the unit for the yEnd value. It can have one of the two 177 values Unit.NUMBER or Unit.PERCENT. If it is null then the 178 default value is Unit.NUMBER. 179 @param colorStops: 180 the list of colorstops for the linear gradient. 181 """ 182 self._xStart = 0 183 self._xStartUnit = Unit.NUMBER 184 self._yStart = 0 185 self._yStartUnit = Unit.NUMBER 186 self._xEnd = 0 187 self._xEndUnit = Unit.NUMBER 188 self._yEnd = 0 189 self._yEndUnit = Unit.NUMBER 190 self._colorStops = list() 191 192 xStartUnit = yStartUnit = None 193 if xEndUnit is None: 194 xStart, yStart, xEnd, yEnd, colorStops = (xStart, 195 xStartUnit_or_yStart, yStart_or_xEnd, yStartUnit_or_yEnd, 196 xEnd_or_colorStops) 197 else: 198 xStartUnit, yStart, yStartUnit, xEnd, xEndUnit, yEnd, yEndUnit, \ 199 colorStops = (xStartUnit_or_yStart, yStart_or_xEnd, 200 yStartUnit_or_yEnd, xEnd_or_colorStops, xEndUnit, yEnd, 201 yEndUnit, colorStops) 202 203 super(LinearGradient, self).__init__() 204 205 self._xStart = xStart 206 if xStartUnit is not None: 207 self._xStartUnit = xStartUnit 208 209 self._yStart = yStart 210 if yStartUnit is not None: 211 self._yStartUnit = yStartUnit 212 213 self._xEnd = xEnd 214 if xEndUnit is not None: 215 self._xEndUnit = xEndUnit 216 217 self._yEnd = yEnd 218 if yEndUnit is not None: 219 self._yEndUnit = yEndUnit 220 221 if colorStops is not None: 222 self._colorStops = colorStops
223
224 - def getxStart(self):
225 return self._xStart
226
227 - def getxEnd(self):
228 return self._xEnd
229
230 - def getyStart(self):
231 return self._yStart
232
233 - def getyEnd(self):
234 return self._yEnd
235
236 - def getxStartUnit(self):
237 return self._xStartUnit
238
239 - def getyStartUnit(self):
240 return self._yStartUnit
241
242 - def getxEndUnit(self):
243 return self._xEndUnit
244
245 - def getyEndUnit(self):
246 return self._yEndUnit
247
248 - def getColorStops(self):
249 return self._colorStops
250
251 - def getString(self):
252 """@return: Returns string representation of this LinearGradient""" 253 sb = StringIO() 254 # The prefix "JSOBJ:" indicates that the string is a JavaScript 255 # object 256 x1 = '\'' + str(self._xStart) + self._xStartUnit.getSymbol() + '\'' 257 y1 = '\'' + str(self._yStart) + self._yStartUnit.getSymbol() + '\'' 258 x2 = '\'' + str(self._xEnd) + self._xEndUnit.getSymbol() + '\'' 259 y2 = '\'' + str(self._yEnd) + self._yEndUnit.getSymbol() + '\'' 260 sb.write('JSOBJ:{') 261 sb.write(' linearGradient: [' + x1 + ',' + y1 + ',' + x2 + ',' + y2 + '],') 262 sb.write(' stops: [') 263 count = 0 264 for colorStop in self._colorStops: 265 if colorStop.getColor() is not None: 266 stopAt = ('\'' + str(colorStop.getStopAt()) 267 + colorStop.getStopAtUnit().getSymbol() + '\'') 268 stopColor = '\'' + colorStop.getColor().getString() + '\'' 269 if count > 0: 270 sb.write(',') 271 sb.write('[' + stopAt + ', ' + stopColor + ']') 272 count += 1 273 sb.write(' ]') 274 sb.write('}') 275 return sb.getvalue()
276
277 278 -class LinearColorStop(IColorStop):
279 """Represents stop-value and color for the L{LinearGradient} 280 281 @author: Invient 282 @author: Richard Lincoln 283 """ 284
285 - def __init__(self, stopAt, stopAtUnit_or_color, color=None):
286 self._stopAt = None 287 self._stopAtUnit = Unit.NUMBER 288 self._color = None 289 290 if color == None: 291 color = stopAtUnit_or_color 292 super(LinearColorStop, self).__init__() 293 self._stopAt = stopAt 294 self._color = color 295 else: 296 stopAtUnit = stopAtUnit_or_color 297 super(LinearColorStop, self).__init__() 298 self._stopAt = stopAt 299 if stopAtUnit is not None: 300 self._stopAtUnit = stopAtUnit 301 self._color = color
302
303 - def getStopAt(self):
304 return self._stopAt
305
306 - def getStopAtUnit(self):
307 return self._stopAtUnit
308
309 - def getColor(self):
310 return self._color
311