annotate bGrease/color.py @ 173:3abd31885f0f

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