annotate bGrease/color.py @ 46:bf506f739322

Removed layer as attribute of BaseBehaviour and added it as a parameter to the attachToLayer method.
author KarstenBock@gmx.net
date Wed, 07 Sep 2011 13:54:02 +0200
parents ff3e395abf91
children a6bbb732b27b
rev   line source
5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
1
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
2 class RGBA(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
3 """Four channel color representation.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
4
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
5 RGBA colors are floating point color representations with color channel
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
6 values between (0..1). Colors may be initialized from 3 or 4 floating
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
7 point numbers or a hex string::
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
8
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
9 RGBA(1.0, 1.0, 1.0) # Alpha defaults to 1.0
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
10 RGBA(1.0, 1.0, 0, 0.5)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
11 RGBA("#333")
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
12 RGBA("#7F7F7F")
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
13
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14 Individual color channels can be accessed by attribute name, or the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15 color object can be treated as a sequence of 4 floats.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
16 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
17
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
18 def __init__(self, r_or_colorstr, g=None, b=None, a=None):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19 if isinstance(r_or_colorstr, str):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20 assert g is b is a is None, "Ambiguous color arguments"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21 self.r, self.g, self.b, self.a = self._parse_colorstr(r_or_colorstr)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 elif g is b is a is None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24 self.r, self.g, self.b, self.a = r_or_colorstr
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25 except ValueError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26 self.r, self.g, self.b = r_or_colorstr
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27 self.a = 1.0
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
29 self.r = r_or_colorstr
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
30 self.g = g
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
31 self.b = b
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32 self.a = a
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33 if self.a is None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
34 self.a = 1.0
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
35
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36 def _parse_colorstr(self, colorstr):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37 length = len(colorstr)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38 if not colorstr.startswith("#") or length not in (4, 5, 7, 9):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39 raise ValueError("Invalid color string: " + colorstr)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40 if length <= 5:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 parsed = [int(c*2, 16) / 255.0 for c in colorstr[1:]]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
43 parsed = [int(colorstr[i:i+2], 16) / 255.0 for i in range(1, length, 2)]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
44 if len(parsed) == 3:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
45 parsed.append(1.0)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
46 return parsed
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
47
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
48 def __len__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
49 return 4
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
50
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
51 def __getitem__(self, item):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
52 return (self.r, self.g, self.b, self.a)[item]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
53
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
54 def __iter__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
55 return iter((self.r, self.g, self.b, self.a))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
56
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
57 def __eq__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
58 return tuple(self) == tuple(other)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
59
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
60 def __repr__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
61 return "%s(%.2f, %.2f, %.2f, %.2f)" % (self.__class__.__name__,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
62 self.r, self.g, self.b, self.a)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
63
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
64