forked from dodoru/rasterizer.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.py
55 lines (40 loc) · 1.14 KB
/
color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from __future__ import division
import operator
__author__ = 'gua'
class Color(object):
def __init__(self, r, g, b, a):
self.r = r
self.g = g
self.b = b
self.a = a
def _operation(self, other, op):
r = op(self.r, other.r)
g = op(self.g, other.g)
b = op(self.b, other.b)
a = op(self.a, other.a)
return Color(r, g, b, a)
def __add__(self, other):
return self._operation(other, operator.add)
def __sub__(self, other):
return self._operation(other, operator.sub)
def __mul__(self, value):
r, g, b, a = [i * value for i in [self.r, self.g, self.b, self.a]]
return Color(r, g, b, a)
def uint32(self):
r = int(self.r * 255)
g = int(self.g * 255)
b = int(self.b * 255)
a = int(self.a * 255)
return a << 24 | b << 16 | g << 8 | r
@staticmethod
def red():
return Color(1, 0, 0, 1)
@staticmethod
def green():
return Color(0, 1, 0, 1)
@staticmethod
def blue():
return Color(0, 0, 1, 1)
@staticmethod
def cyan():
return Color(0, 1, 1, 1)