-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_class.py
76 lines (59 loc) · 2.66 KB
/
color_class.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from random import randrange as rd
class Color:
def __init__(self, *args, color_range=(256, 256, 256)):
if args:
self.r, self.g, self.b = args
else:
if isinstance(color_range, Color):
color_range = color_range.tuple()
self.r, self.g, self.b = rd(0, color_range[0]), rd(0, color_range[1]), rd(0, color_range[2])
def __str__(self):
return f'Color({self.r}, {self.g}, {self.b})'
def __add__(self, other):
if isinstance(other, Color):
return Color(self.r + other.r, self.g + other.g, self.b + other.b)
elif isinstance(other, tuple):
return Color(self.r + other[0], self.g + other[1], self.b + other[2])
return Color(self.r + other, self.g + other, self.b + other)
def __sub__(self, other):
if isinstance(other, Color):
return Color(self.r - other.r, self.g - other.g, self.b - other.b)
elif isinstance(other, tuple):
return Color(self.r - other[0], self.g - other[1], self.b - other[2])
return Color(self.r - other, self.g - other, self.b - other)
def __mul__(self, other):
return Color(self.r * other, self.g * other, self.b * other)
def __mod__(self, other):
if isinstance(other, Color):
return Color(self.r % other.r, self.g % other.g, self.b % other.b)
elif isinstance(other, tuple):
return Color(self.r % other[0], self.g % other[1], self.b % other[2])
return Color(self.r % other, self.g % other, self.b % other)
def __imod__(self, other):
return self % other
def __floordiv__(self, other):
if isinstance(other, Color):
return Color(self.r // other.r, self.g // other.g, self.b // other.b)
elif isinstance(other, tuple):
return Color(self.r // other[0], self.g // other[1], self.b // other[2])
return Color(self.r // other, self.g // other, self.b // other)
def __ifloordiv__(self, other):
return self // other
def __iadd__(self, other):
return self + other
def __isub__(self, other):
return self - other
def __imul__(self, other):
return self * other
def __lt__(self, other):
if isinstance(other, Color):
return self.r < other.r and self.g < other.g and self.b < other.b
elif isinstance(other, tuple):
return self.r < other[0] and self.g < other[1] and self.b < other[2]
return self.r < other and self.g < other and self.b < other
def tuple(self):
return self.r, self.g, self.b
if __name__ == '__main__':
c1 = Color(20, 30, 57)
c2 = Color(10, 20, 1)
print(c1 < 12)