Package lepl :: Package cairo :: Module colour
[hide private]
[frames] | no frames]

Source Code for Module lepl.cairo.colour

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  Provide some short-hand colour definitions for cairo. 
 32   
 33  As far as I can tell, cairo uses pre-multiplied alpha - 
 34  http://en.wikipedia.org/wiki/Alpha_compositing 
 35  ''' 
 36   
 37   
 38  from collections import namedtuple 
 39  from operator import itemgetter 
40 41 42 -def _replace(index):
43 def setter(self, value): 44 args = list(self) 45 args[index] = value 46 return Colour(*args)
47 return setter 48
49 50 # pylint: disable-msg=E1002 51 # it is new=style 52 -class Colour(namedtuple('BaseColour', 'r g b a')):
53
54 - def __new__(cls, r, g, b, a=1.0):
55 a = min(1, max(0, a)) 56 r = min(a, max(0, r)) 57 g = min(a, max(0, g)) 58 b = min(a, max(0, b)) 59 return super(Colour, cls).__new__(cls, r, g, b, a)
60 61 r = property(itemgetter(0), _replace(0)) 62 g = property(itemgetter(1), _replace(1)) 63 b = property(itemgetter(2), _replace(2)) 64 a = property(itemgetter(3), _replace(3)) 65 66 @staticmethod
67 - def from_rgb(rgb):
68 return Colour(rgb[0], rgb[1], rgb[2], 1.0)
69
70 - def rgb(self):
71 ''' 72 Convert to an RGB triplet (alpha is not removed). 73 ''' 74 return (self.r, self.g, self.b)
75
76 - def __mul__(self, factor):
77 ''' 78 Scale the colour by some factor. 79 80 If factor is a single value, it is applied only to the RGB components; 81 if it is a pair then the second value is applied to alpha (and 82 folded in to RGB too). So 0.5 would reduce RGB by half; (1, 0.5) 83 would reduce alpha by half (and scale RGB correspondingly); (0.5, 0.5) 84 would scale RGB *and* reduce alpha (so pre-multiplied RGB would be 85 numerically scaled by 0.25). 86 87 After scaling, values are clipped within (0, 1). 88 ''' 89 r, g, b, a = self 90 try: 91 frgb, fa = factor 92 frgb *= fa # pre-multiply 93 r *= frgb 94 g *= frgb 95 b *= frgb 96 a *= fa 97 except TypeError: 98 r *= factor 99 g *= factor 100 b *= factor 101 return Colour(r, g, b, a)
102
103 - def opaque(self):
104 ''' 105 The same colour, but with alpha forced to 1. 106 ''' 107 return Colour(self.r / self.a, self.g / self.a, self.b / self.a)
108
109 - def __add__(self, colour):
110 ''' 111 Overlay colours following normal composition (right on top). 112 ''' 113 fa = 1 - colour.a 114 r = colour.r + self.r * fa 115 g = colour.g + self.g * fa 116 b = colour.b + self.b * fa 117 a = colour.a + self.a * fa 118 return Colour(r, g, b, a)
119 120 121 BLACK = Colour(0, 0, 0) 122 RED = Colour(1, 0, 0) 123 GREEN = Colour(0, 1, 0) 124 BLUE = Colour(0, 0, 1) 125 YELLOW = Colour(1, 1, 0) 126 CYAN = Colour(0, 1, 1) 127 MAGENTA = Colour(1, 0, 1) 128 WHITE = Colour(1, 1, 1) 129