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

Source Code for Module muntjac.addon.colorpicker.color

 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   
17  import colorsys 
18   
19 -class Color(object):
20 21 BLACK = None 22 WHITE = None 23 24 RED = None 25 GREEN = None 26 BLUE = None 27
28 - def __init__(self, r, g, b, a=1.0):
29 self._r = self._convert(r) 30 self._g = self._convert(g) 31 self._b = self._convert(b) 32 self._a = self._convert(a)
33 34
35 - def _convert(self, value):
36 if isinstance(value, float): 37 return int(value * 255) 38 else: 39 return value
40
41 - def getRed(self):
42 return self._r
43
44 - def getGreen(self):
45 return self._g
46
47 - def getBlue(self):
48 return self._b
49
50 - def getAlpha(self):
51 return self._a
52
53 - def __str__(self):
54 return 'rgb(%d,%d,%d)' % (self._r, self._g, self._b)
55
56 - def getHSV(self):
57 return colorsys.rgb_to_hsv( 58 self._r / 255.0, 59 self._g / 255.0, 60 self._b / 255.0)
61 62 Color.BLACK = Color(0.0, 0.0, 0.0) 63 Color.WHITE = Color(1.0, 1.0, 1.0) 64 Color.RED = Color(1.0, 0.0, 0.0) 65 Color.GREEN = Color(0.0, 1.0, 0.0) 66 Color.BLUE = Color(0.0, 0.0, 1.0) 67