e2D 1.4.15__py3-none-any.whl → 1.4.17__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- e2D/__init__.py +8 -4
- e2D/__init__.pyi +4 -0
- e2D/colors.py +466 -0
- e2D/def_colors.py +1732 -0
- e2D/envs.py +23 -21
- e2D/plots.py +1 -1
- e2D/utils.py +10 -9
- {e2D-1.4.15.dist-info → e2D-1.4.17.dist-info}/METADATA +2 -2
- e2D-1.4.17.dist-info/RECORD +13 -0
- {e2D-1.4.15.dist-info → e2D-1.4.17.dist-info}/WHEEL +1 -1
- e2D-1.4.15.dist-info/RECORD +0 -11
- {e2D-1.4.15.dist-info → e2D-1.4.17.dist-info}/LICENSE +0 -0
- {e2D-1.4.15.dist-info → e2D-1.4.17.dist-info}/top_level.txt +0 -0
e2D/__init__.py
CHANGED
|
@@ -56,6 +56,10 @@ class Vector2D:
|
|
|
56
56
|
@property
|
|
57
57
|
def length_sqrd(self) -> float:
|
|
58
58
|
return self.x ** 2 + self.y ** 2
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def inverse(self) -> "Vector2D":
|
|
62
|
+
return self.mult(-1)
|
|
59
63
|
|
|
60
64
|
def floor(self, n=1) -> "Vector2D":
|
|
61
65
|
return self.__floor__(n)
|
|
@@ -468,10 +472,10 @@ V2two = Vector2D(2.0, 2.0)
|
|
|
468
472
|
V2pi = Vector2D(PI, PI)
|
|
469
473
|
V2inf = Vector2D(float("inf"), float("inf"))
|
|
470
474
|
|
|
471
|
-
V2neg_one =
|
|
472
|
-
V2neg_two =
|
|
473
|
-
V2neg_pi =
|
|
474
|
-
V2neg_inf =
|
|
475
|
+
V2neg_one = V2one.mult(-1)
|
|
476
|
+
V2neg_two = V2two.mult(-1)
|
|
477
|
+
V2neg_pi = V2pi.mult(-1)
|
|
478
|
+
V2neg_inf = V2inf.mult(-1)
|
|
475
479
|
|
|
476
480
|
V2up = Vector2D(0, 1)
|
|
477
481
|
V2right = Vector2D(1, 0)
|
e2D/__init__.pyi
CHANGED
e2D/colors.py
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
from colorsys import hsv_to_rgb as __hsv_to_rgb_def__, hls_to_rgb as __hls_to_rgb_def__, rgb_to_hls as __rgb_to_hls__, rgb_to_hsv as __rgb_to_hsv__
|
|
2
|
+
from typing import Any, Callable, Generator, Literal
|
|
3
|
+
from pygame.color import Color as __color_pygame__
|
|
4
|
+
from random import randint as __randint__
|
|
5
|
+
|
|
6
|
+
RGB_COLOR_MODE = "rgb"
|
|
7
|
+
RGBA_COLOR_MODE = "rgba"
|
|
8
|
+
BGR_COLOR_MODE = "bgr"
|
|
9
|
+
BGRA_COLOR_MODE = "bgra"
|
|
10
|
+
GRAY_COLOR_MODE = "g"
|
|
11
|
+
HSV_COLOR_MODE = "hsv"
|
|
12
|
+
HLS_COLOR_MODE = "hls"
|
|
13
|
+
# CMYK_COLOR_MODE = "cmyk"
|
|
14
|
+
# LAB_COLOR_MODE = "lab"
|
|
15
|
+
|
|
16
|
+
__LITERAL_COLOR_MODES__ = Literal["rgb","rgba","bgr","bgra","g","hsv","hls"] #,"cmyk","lab"]
|
|
17
|
+
|
|
18
|
+
def __hsv_to_rgb__(h:int|float, s:int|float, v:int|float) -> tuple[int|float, int|float, int|float]:
|
|
19
|
+
r,g,b = __hsv_to_rgb_def__(h, s, v)
|
|
20
|
+
return r*255, g*255, b*255
|
|
21
|
+
|
|
22
|
+
def __hls_to_rgb__(h:int|float, s:int|float, v:int|float) -> tuple[int|float, int|float, int|float]:
|
|
23
|
+
r,g,b = __hls_to_rgb_def__(h, s, v)
|
|
24
|
+
return r*255, g*255, b*255
|
|
25
|
+
|
|
26
|
+
__conversion_table__ :dict[__LITERAL_COLOR_MODES__, dict[__LITERAL_COLOR_MODES__, Callable]]= {
|
|
27
|
+
"rgb": {
|
|
28
|
+
"rgba": lambda r,g,b: (r, g, b, 255.0),
|
|
29
|
+
"bgr": lambda r,g,b: (b, g, r),
|
|
30
|
+
"bgra": lambda r,g,b: (b, g, r, 255.0),
|
|
31
|
+
"g": lambda r,g,b: (.2989*r+.587*g+.114*b,),
|
|
32
|
+
"hsv": lambda r,g,b: __rgb_to_hsv__(r/255.0, g/255.0, b/255.0),
|
|
33
|
+
"hls": lambda r,g,b: __rgb_to_hls__(r/255.0, g/255.0, b/255.0),
|
|
34
|
+
},
|
|
35
|
+
"rgba": {
|
|
36
|
+
"rgb": lambda r,g,b,a: (r, g, b),
|
|
37
|
+
"bgr": lambda r,g,b,a: (b, g, r),
|
|
38
|
+
"bgra": lambda r,g,b,a: (b, g, r, a),
|
|
39
|
+
"g": lambda r,g,b,a: (.2989*r+.587*g+.114*b,),
|
|
40
|
+
"hsv": lambda r,g,b,a: __rgb_to_hsv__(r/255.0, g/255.0, b/255.0),
|
|
41
|
+
"hls": lambda r,g,b,a: __rgb_to_hls__(r/255.0, g/255.0, b/255.0),
|
|
42
|
+
},
|
|
43
|
+
"bgr": {
|
|
44
|
+
"rgb": lambda b,g,r: (r, g, b),
|
|
45
|
+
"rgba": lambda b,g,r: (r, g, b, 255.0),
|
|
46
|
+
"bgra": lambda b,g,r: (b, g, r, 255.0),
|
|
47
|
+
"g": lambda b,g,r: (.2989*r+.587*g+.114*b,),
|
|
48
|
+
"hsv": lambda b,g,r: __rgb_to_hsv__(r/255.0, g/255.0, b/255.0),
|
|
49
|
+
"hls": lambda b,g,r: __rgb_to_hls__(r/255.0, g/255.0, b/255.0),
|
|
50
|
+
},
|
|
51
|
+
"bgra": {
|
|
52
|
+
"rgb": lambda b,g,r,a: (r, g, b),
|
|
53
|
+
"rgba": lambda b,g,r,a: (b, g, r, a),
|
|
54
|
+
"bgr": lambda b,g,r,a: (b, g, r),
|
|
55
|
+
"g": lambda b,g,r,a: (.2989*r+.587*g+.114*b,),
|
|
56
|
+
"hsv": lambda b,g,r,a: __rgb_to_hsv__(r/255.0, g/255.0, b/255.0),
|
|
57
|
+
"hls": lambda b,g,r,a: __rgb_to_hls__(r/255.0, g/255.0, b/255.0),
|
|
58
|
+
},
|
|
59
|
+
"g": {
|
|
60
|
+
"rgb": lambda g: (g, g, g),
|
|
61
|
+
"rgba": lambda g: (g, g, g, 255.0),
|
|
62
|
+
"bgr": lambda g: (g, g, g),
|
|
63
|
+
"bgra": lambda g: (g, g, g, 255.0),
|
|
64
|
+
"hsv": lambda g: __rgb_to_hsv__(g/255.0, g/255.0, g/255.0),
|
|
65
|
+
"hls": lambda g: __rgb_to_hls__(g/255.0, g/255.0, g/255.0),
|
|
66
|
+
},
|
|
67
|
+
"hsv": {
|
|
68
|
+
"rgb": lambda h,s,v: __hsv_to_rgb__(h,s,v),
|
|
69
|
+
"rgba": lambda h,s,v: (*__hsv_to_rgb__(h,s,v), 255.0),
|
|
70
|
+
"bgr": lambda h,s,v: __hsv_to_rgb__(h,s,v)[::-1],
|
|
71
|
+
"bgra": lambda h,s,v: (*__hsv_to_rgb__(h,s,v)[::-1], 255.0),
|
|
72
|
+
"g": lambda h,s,v: (sum(m*v for m,v in zip((.2989, .587, .114), __hsv_to_rgb__(h,s,v))),),
|
|
73
|
+
"hls": lambda h,s,v: __rgb_to_hls__(*__hsv_to_rgb_def__(h,s,v)),
|
|
74
|
+
},
|
|
75
|
+
"hls": {
|
|
76
|
+
"rgb": lambda h,l,s: __hls_to_rgb__(h,l,s),
|
|
77
|
+
"rgba": lambda h,l,s: (*__hls_to_rgb__(h,l,s), 255.0),
|
|
78
|
+
"bgr": lambda h,l,s: __hls_to_rgb__(h,l,s)[::-1],
|
|
79
|
+
"bgra": lambda h,l,s: (*__hls_to_rgb__(h,l,s)[::-1], 255.0),
|
|
80
|
+
"g": lambda h,l,s: (sum(m*v for m,v in zip((.2989, .587, .114), __hls_to_rgb__(h,l,s))),),
|
|
81
|
+
"hsv": lambda h,l,s: __rgb_to_hsv__(*__hls_to_rgb_def__(h,l,s)),
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
class Color:
|
|
86
|
+
def __init__(self, *values, mode:__LITERAL_COLOR_MODES__=RGB_COLOR_MODE) -> None:
|
|
87
|
+
self.__dict__ = dict(zip(mode, values))
|
|
88
|
+
self.mode :__LITERAL_COLOR_MODES__= mode
|
|
89
|
+
|
|
90
|
+
@classmethod
|
|
91
|
+
def new_rgb(cls, r:int|float, g:int|float, b:int|float) -> "Color":
|
|
92
|
+
return Color(r,g,b, mode=RGB_COLOR_MODE)
|
|
93
|
+
@classmethod
|
|
94
|
+
def new_rgba(cls, r:int|float, g:int|float, b:int|float, a:int|float) -> "Color":
|
|
95
|
+
return Color(r,g,b,a, mode=RGBA_COLOR_MODE)
|
|
96
|
+
@classmethod
|
|
97
|
+
def new_bgr(cls, b:int|float, g:int|float, r:int|float) -> "Color":
|
|
98
|
+
return Color(b,g,r, mode=BGR_COLOR_MODE)
|
|
99
|
+
@classmethod
|
|
100
|
+
def new_bgra(cls, b:int|float, g:int|float, r:int|float, a:int|float) -> "Color":
|
|
101
|
+
return Color(b,g,r,a, mode=BGRA_COLOR_MODE)
|
|
102
|
+
@classmethod
|
|
103
|
+
def new_g(cls, g) -> "Color":
|
|
104
|
+
return Color(g, mode=GRAY_COLOR_MODE)
|
|
105
|
+
@classmethod
|
|
106
|
+
def new_hsv(cls, h:int|float, s:int|float, v:int|float) -> "Color":
|
|
107
|
+
return Color(h,s,v, mode=HSV_COLOR_MODE)
|
|
108
|
+
@classmethod
|
|
109
|
+
def new_hls(cls, h:int|float, l:int|float, s:int|float) -> "Color":
|
|
110
|
+
return Color(h,l,s, mode=HLS_COLOR_MODE)
|
|
111
|
+
# @classmethod
|
|
112
|
+
# def new_cmyk(cls, c:int|float, m:int|float, y:int|float, k:int|float) -> Color:
|
|
113
|
+
# return Color(c,m,y,k, mode=CMYK_COLOR_MODE)
|
|
114
|
+
# @classmethod
|
|
115
|
+
# def new_lab(cls, l:int|float, a:int|float, b:int|float) -> Color:
|
|
116
|
+
# return Color(l,a,b, mode=LAB_COLOR_MODE)
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def values(self) -> tuple[int|float, ...]:
|
|
120
|
+
return tuple(self.__dict__.values())[:-1]
|
|
121
|
+
|
|
122
|
+
@property
|
|
123
|
+
def keys(self) -> tuple[str, ...]:
|
|
124
|
+
return tuple(self.__dict__.keys())[:-1]
|
|
125
|
+
|
|
126
|
+
@property
|
|
127
|
+
def items(self) -> tuple[tuple[str, int|float], ...]:
|
|
128
|
+
return tuple(self.__dict__.items())[:-1]
|
|
129
|
+
|
|
130
|
+
def copy(self) -> "Color":
|
|
131
|
+
return Color(*self.values, mode=self.mode)
|
|
132
|
+
|
|
133
|
+
def to_mode(self, mode:__LITERAL_COLOR_MODES__) -> "Color":
|
|
134
|
+
if mode == self.mode: return self.copy()
|
|
135
|
+
return Color(*__conversion_table__[self.mode][mode](*self.values), mode=mode)
|
|
136
|
+
|
|
137
|
+
def to_rgb(self) -> "Color":
|
|
138
|
+
if self.mode == RGB_COLOR_MODE: return self.copy()
|
|
139
|
+
return Color(*__conversion_table__[self.mode][RGB_COLOR_MODE](*self.values), mode=RGB_COLOR_MODE)
|
|
140
|
+
def to_rgba(self) -> "Color":
|
|
141
|
+
if self.mode == RGBA_COLOR_MODE: return self.copy()
|
|
142
|
+
return Color(*__conversion_table__[self.mode][RGBA_COLOR_MODE](*self.values), mode=RGBA_COLOR_MODE)
|
|
143
|
+
def to_bgr(self) -> "Color":
|
|
144
|
+
if self.mode == BGR_COLOR_MODE: return self.copy()
|
|
145
|
+
return Color(*__conversion_table__[self.mode][BGR_COLOR_MODE](*self.values), mode=BGR_COLOR_MODE)
|
|
146
|
+
def to_bgra(self) -> "Color":
|
|
147
|
+
if self.mode == BGRA_COLOR_MODE: return self.copy()
|
|
148
|
+
return Color(*__conversion_table__[self.mode][BGRA_COLOR_MODE](*self.values), mode=BGRA_COLOR_MODE)
|
|
149
|
+
def to_g(self) -> "Color":
|
|
150
|
+
if self.mode == GRAY_COLOR_MODE: return self.copy()
|
|
151
|
+
return Color(*__conversion_table__[self.mode][GRAY_COLOR_MODE](*self.values), mode=GRAY_COLOR_MODE)
|
|
152
|
+
def to_hsv(self) -> "Color":
|
|
153
|
+
if self.mode == HSV_COLOR_MODE: return self.copy()
|
|
154
|
+
return Color(*__conversion_table__[self.mode][HSV_COLOR_MODE](*self.values), mode=HSV_COLOR_MODE)
|
|
155
|
+
def to_hls(self) -> "Color":
|
|
156
|
+
if self.mode == HLS_COLOR_MODE: return self.copy()
|
|
157
|
+
return Color(*__conversion_table__[self.mode][HLS_COLOR_MODE](*self.values), mode=HLS_COLOR_MODE)
|
|
158
|
+
|
|
159
|
+
def __repr__(self) -> str:
|
|
160
|
+
return self.__str__()
|
|
161
|
+
|
|
162
|
+
def __str__(self) -> str:
|
|
163
|
+
return "Color(" + ", ".join(f"{k}:{v}" for k, v in self.items) + ")"
|
|
164
|
+
|
|
165
|
+
def __call__(self) -> __color_pygame__:
|
|
166
|
+
return __color_pygame__(*map(int, self.to_rgba().values))
|
|
167
|
+
|
|
168
|
+
# fast operations Vector2D.operation(both,x,y)
|
|
169
|
+
def add(self, all3=.0, r=.0, g=.0, b=.0) -> "Color":
|
|
170
|
+
c_color = self.to_rgb()
|
|
171
|
+
return Color(c_color.r + (r + all3), c_color.g + (g + all3), c_color.b + (b + all3)).to_mode(self.mode) # type: ignore
|
|
172
|
+
|
|
173
|
+
def sub(self, all3=.0, r=.0, g=.0, b=.0) -> "Color":
|
|
174
|
+
c_color = self.to_rgb()
|
|
175
|
+
return Color(c_color.r - (r + all3), c_color.g - (g + all3), c_color.b - (b + all3)).to_mode(self.mode) # type: ignore
|
|
176
|
+
|
|
177
|
+
def mult(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
178
|
+
c_color = self.to_rgb()
|
|
179
|
+
return Color(c_color.r * r * all3, c_color.g * g * all3, c_color.b * b * all3).to_mode(self.mode) # type: ignore
|
|
180
|
+
|
|
181
|
+
def pow(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
182
|
+
c_color = self.to_rgb()
|
|
183
|
+
return Color(c_color.r ** (r + all3), c_color.g ** (g + all3), c_color.b ** (b + all3)).to_mode(self.mode) # type: ignore
|
|
184
|
+
|
|
185
|
+
def mod(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
186
|
+
c_color = self.to_rgb()
|
|
187
|
+
return Color(c_color.r % (r + all3), c_color.g % (g + all3), c_color.b % (b + all3)).to_mode(self.mode) # type: ignore
|
|
188
|
+
|
|
189
|
+
def div(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
190
|
+
c_color = self.to_rgb()
|
|
191
|
+
return Color(c_color.r / r / all3, c_color.g / g / all3, c_color.b / b / all3).to_mode(self.mode) # type: ignore
|
|
192
|
+
|
|
193
|
+
def fdiv(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
194
|
+
c_color = self.to_rgb()
|
|
195
|
+
return Color(c_color.r // r // all3, c_color.g // g // all3, c_color.b // b // all3).to_mode(self.mode) # type: ignore
|
|
196
|
+
|
|
197
|
+
# fast inplace operations Vector2D.ioperation(both,x,y)
|
|
198
|
+
def set(self, both=.0, x=.0, y=.0) -> "Color":
|
|
199
|
+
self.x = x + both
|
|
200
|
+
self.y = y + both
|
|
201
|
+
return self
|
|
202
|
+
|
|
203
|
+
def iadd(self, all3=.0, r=.0, g=.0, b=.0) -> "Color":
|
|
204
|
+
c_color = self.to_rgb()
|
|
205
|
+
c_color.r += r + all3 # type: ignore
|
|
206
|
+
c_color.g += g + all3 # type: ignore
|
|
207
|
+
c_color.b += b + all3 # type: ignore
|
|
208
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
209
|
+
return self
|
|
210
|
+
|
|
211
|
+
def isub(self, all3=.0, r=.0, g=.0, b=.0) -> "Color":
|
|
212
|
+
c_color = self.to_rgb()
|
|
213
|
+
c_color.r -= r + all3 # type: ignore
|
|
214
|
+
c_color.g -= g + all3 # type: ignore
|
|
215
|
+
c_color.b -= b + all3 # type: ignore
|
|
216
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
217
|
+
return self
|
|
218
|
+
|
|
219
|
+
def imult(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
220
|
+
c_color = self.to_rgb()
|
|
221
|
+
c_color.r *= r * all3 # type: ignore
|
|
222
|
+
c_color.g *= g * all3 # type: ignore
|
|
223
|
+
c_color.b *= b * all3 # type: ignore
|
|
224
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
225
|
+
return self
|
|
226
|
+
|
|
227
|
+
def ipow(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
228
|
+
c_color = self.to_rgb()
|
|
229
|
+
c_color.r **= r + all3 # type: ignore
|
|
230
|
+
c_color.g **= g + all3 # type: ignore
|
|
231
|
+
c_color.b **= b + all3 # type: ignore
|
|
232
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
233
|
+
return self
|
|
234
|
+
|
|
235
|
+
def imod(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
236
|
+
c_color = self.to_rgb()
|
|
237
|
+
c_color.r %= r + all3 # type: ignore
|
|
238
|
+
c_color.g %= g + all3 # type: ignore
|
|
239
|
+
c_color.b %= b + all3 # type: ignore
|
|
240
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
241
|
+
return self
|
|
242
|
+
|
|
243
|
+
def idiv(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
244
|
+
c_color = self.to_rgb()
|
|
245
|
+
c_color.r /= r * all3 # type: ignore
|
|
246
|
+
c_color.g /= g * all3 # type: ignore
|
|
247
|
+
c_color.b /= b * all3 # type: ignore
|
|
248
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
249
|
+
return self
|
|
250
|
+
|
|
251
|
+
def ifdiv(self, all3=1.0, r=1.0, g=1.0, b=1.0) -> "Color":
|
|
252
|
+
c_color = self.to_rgb()
|
|
253
|
+
c_color.r //= r * all3 # type: ignore
|
|
254
|
+
c_color.g //= g * all3 # type: ignore
|
|
255
|
+
c_color.b //= b * all3 # type: ignore
|
|
256
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
257
|
+
return self
|
|
258
|
+
|
|
259
|
+
# normal operations Vector2D + a
|
|
260
|
+
def __add__(self, other) -> "Color":
|
|
261
|
+
other = Color.__normalize__(other)
|
|
262
|
+
c_color = self.to_rgb()
|
|
263
|
+
return Color(c_color.r + other.r, c_color.g + other.g, c_color.b + other.b).to_mode(self.mode) # type: ignore
|
|
264
|
+
|
|
265
|
+
def __sub__(self, other) -> "Color":
|
|
266
|
+
other = Color.__normalize__(other)
|
|
267
|
+
c_color = self.to_rgb()
|
|
268
|
+
return Color(c_color.r - other.r, c_color.g - other.g, c_color.b - other.b).to_mode(self.mode) # type: ignore
|
|
269
|
+
|
|
270
|
+
def __mul__(self, other) -> "Color":
|
|
271
|
+
other = Color.__normalize__(other)
|
|
272
|
+
c_color = self.to_rgb()
|
|
273
|
+
return Color(c_color.r * other.r, c_color.g * other.g, c_color.b * other.b).to_mode(self.mode) # type: ignore
|
|
274
|
+
|
|
275
|
+
def __mod__(self, other) -> "Color":
|
|
276
|
+
other = Color.__normalize__(other)
|
|
277
|
+
c_color = self.to_rgb()
|
|
278
|
+
return Color(c_color.r % other.r, c_color.g % other.g, c_color.b % other.b).to_mode(self.mode) # type: ignore
|
|
279
|
+
|
|
280
|
+
def __pow__(self, other) -> "Color":
|
|
281
|
+
other = Color.__normalize__(other)
|
|
282
|
+
c_color = self.to_rgb()
|
|
283
|
+
return Color(c_color.r ** other.r, c_color.g ** other.g, c_color.b ** other.b).to_mode(self.mode) # type: ignore
|
|
284
|
+
|
|
285
|
+
def __truediv__(self, other) -> "Color":
|
|
286
|
+
other = Color.__normalize__(other)
|
|
287
|
+
c_color = self.to_rgb()
|
|
288
|
+
return Color(c_color.r / other.r, c_color.g / other.g, c_color.b / other.b).to_mode(self.mode) # type: ignore
|
|
289
|
+
|
|
290
|
+
def __floordiv__(self, other) -> "Color":
|
|
291
|
+
other = Color.__normalize__(other)
|
|
292
|
+
c_color = self.to_rgb()
|
|
293
|
+
return Color(c_color.r // other.r, c_color.g // other.g, c_color.b // other.b).to_mode(self.mode) # type: ignore
|
|
294
|
+
|
|
295
|
+
# right operations a + Vector2D
|
|
296
|
+
def __radd__(self, other) -> "Color":
|
|
297
|
+
return self.__add__(other)
|
|
298
|
+
|
|
299
|
+
def __rsub__(self, other) -> "Color":
|
|
300
|
+
other = Color.__normalize__(other)
|
|
301
|
+
c_color = self.to_rgb()
|
|
302
|
+
return Color(other.r - c_color.r, other.g - c_color.g, other.b - c_color.b).to_mode(self.mode) # type: ignore
|
|
303
|
+
|
|
304
|
+
def __rmul__(self, other) -> "Color":
|
|
305
|
+
return self.__mul__(other)
|
|
306
|
+
|
|
307
|
+
def __rmod__(self, other) -> "Color":
|
|
308
|
+
other = Color.__normalize__(other)
|
|
309
|
+
c_color = self.to_rgb()
|
|
310
|
+
return Color(other.r % c_color.r, other.g % c_color.g, other.b % c_color.b).to_mode(self.mode) # type: ignore
|
|
311
|
+
|
|
312
|
+
def __rpow__(self, other) -> "Color":
|
|
313
|
+
other = Color.__normalize__(other)
|
|
314
|
+
c_color = self.to_rgb()
|
|
315
|
+
return Color(other.r ** c_color.r, other.g ** c_color.g, other.b ** c_color.b).to_mode(self.mode) # type: ignore
|
|
316
|
+
|
|
317
|
+
def __rtruediv__(self, other) -> "Color":
|
|
318
|
+
other = Color.__normalize__(other)
|
|
319
|
+
c_color = self.to_rgb()
|
|
320
|
+
return Color(other.r / c_color.r, other.g / c_color.g, other.b / c_color.b).to_mode(self.mode) # type: ignore
|
|
321
|
+
|
|
322
|
+
def __rfloordiv__(self, other) -> "Color":
|
|
323
|
+
other = Color.__normalize__(other)
|
|
324
|
+
c_color = self.to_rgb()
|
|
325
|
+
return Color(other.r // c_color.r, other.g // c_color.g, other.b // c_color.b).to_mode(self.mode) # type: ignore
|
|
326
|
+
|
|
327
|
+
# in-place operations Vector2D += a
|
|
328
|
+
def __iadd__(self, other) -> "Color":
|
|
329
|
+
other = Color.__normalize__(other)
|
|
330
|
+
c_color = self.to_rgb()
|
|
331
|
+
c_color.r += other.r # type: ignore
|
|
332
|
+
c_color.g += other.g # type: ignore
|
|
333
|
+
c_color.b += other.b # type: ignore
|
|
334
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
335
|
+
return self
|
|
336
|
+
|
|
337
|
+
def __isub__(self, other) -> "Color":
|
|
338
|
+
other = Color.__normalize__(other)
|
|
339
|
+
c_color = self.to_rgb()
|
|
340
|
+
c_color.r -= other.r # type: ignore
|
|
341
|
+
c_color.g -= other.g # type: ignore
|
|
342
|
+
c_color.b -= other.b # type: ignore
|
|
343
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
344
|
+
return self
|
|
345
|
+
|
|
346
|
+
def __imul__(self, other) -> "Color":
|
|
347
|
+
other = Color.__normalize__(other)
|
|
348
|
+
c_color = self.to_rgb()
|
|
349
|
+
c_color.r *= other.r # type: ignore
|
|
350
|
+
c_color.g *= other.g # type: ignore
|
|
351
|
+
c_color.b *= other.b # type: ignore
|
|
352
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
353
|
+
return self
|
|
354
|
+
|
|
355
|
+
def __itruediv__(self, other) -> "Color":
|
|
356
|
+
other = Color.__normalize__(other)
|
|
357
|
+
c_color = self.to_rgb()
|
|
358
|
+
c_color.r **= other.r # type: ignore
|
|
359
|
+
c_color.g **= other.g # type: ignore
|
|
360
|
+
c_color.b **= other.b # type: ignore
|
|
361
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
362
|
+
return self
|
|
363
|
+
|
|
364
|
+
def __imod__(self, other) -> "Color":
|
|
365
|
+
other = Color.__normalize__(other)
|
|
366
|
+
c_color = self.to_rgb()
|
|
367
|
+
c_color.r %= other.r # type: ignore
|
|
368
|
+
c_color.g %= other.g # type: ignore
|
|
369
|
+
c_color.b %= other.b # type: ignore
|
|
370
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
371
|
+
return self
|
|
372
|
+
|
|
373
|
+
def __ipow__(self, other) -> "Color":
|
|
374
|
+
other = Color.__normalize__(other)
|
|
375
|
+
c_color = self.to_rgb()
|
|
376
|
+
c_color.r /= other.r # type: ignore
|
|
377
|
+
c_color.g /= other.g # type: ignore
|
|
378
|
+
c_color.b /= other.b # type: ignore
|
|
379
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
380
|
+
return self
|
|
381
|
+
|
|
382
|
+
def __ifloordiv__(self, other) -> "Color":
|
|
383
|
+
other = Color.__normalize__(other)
|
|
384
|
+
c_color = self.to_rgb()
|
|
385
|
+
c_color.r //= other.r # type: ignore
|
|
386
|
+
c_color.g //= other.g # type: ignore
|
|
387
|
+
c_color.b //= other.b # type: ignore
|
|
388
|
+
self.__dict__ = c_color.to_mode(self.mode).__dict__
|
|
389
|
+
return self
|
|
390
|
+
|
|
391
|
+
# comparasion
|
|
392
|
+
def __eq__(self, other) -> bool:
|
|
393
|
+
try: other = Color.__normalize__(other)
|
|
394
|
+
except: return False
|
|
395
|
+
c_color = self.to_rgb()
|
|
396
|
+
return c_color.r == other.r and c_color.g == other.g and c_color.b == other.b # type: ignore
|
|
397
|
+
|
|
398
|
+
def __ne__(self, other) -> bool:
|
|
399
|
+
return not self.__eq__(other)
|
|
400
|
+
|
|
401
|
+
def __abs__(self) -> "Color":
|
|
402
|
+
c_color = self.to_rgb()
|
|
403
|
+
return Color(abs(c_color.r), abs(c_color.g), abs(c_color.b)).to_mode(self.mode) # type: ignore
|
|
404
|
+
|
|
405
|
+
def __round__(self, n=1) -> "Color":
|
|
406
|
+
n = Color.__normalize__(n)
|
|
407
|
+
c_color = self.to_rgb()
|
|
408
|
+
return Color(round(c_color.r / n.r) * n.r, round(c_color.g / n.g) * n.g, round(c_color.b / n.b) * n.b).to_mode(self.mode) # type: ignore
|
|
409
|
+
|
|
410
|
+
def __floor__(self, n=1) -> "Color":
|
|
411
|
+
n = Color.__normalize__(n)
|
|
412
|
+
c_color = self.to_rgb()
|
|
413
|
+
return Color((c_color.r / n.r).__floor__() * n.r, (c_color.g / n.g).__floor__() * n.g, (c_color.b / n.b).__floor__() * n.b).to_mode(self.mode) # type: ignore
|
|
414
|
+
|
|
415
|
+
def __ceil__(self, n=1) -> "Color":
|
|
416
|
+
n = Color.__normalize__(n)
|
|
417
|
+
c_color = self.to_rgb()
|
|
418
|
+
return Color((c_color.r / n.r).__ceil__() * n.r, (c_color.g / n.g).__ceil__() * n.g, (c_color.b / n.b).__ceil__() * n.b).to_mode(self.mode) # type: ignore
|
|
419
|
+
|
|
420
|
+
def __float__(self) -> "Color":
|
|
421
|
+
c_color = self.to_rgb()
|
|
422
|
+
return Color(float(c_color.r), float(c_color.g), float(c_color.b)).to_mode(self.mode) # type: ignore
|
|
423
|
+
|
|
424
|
+
def __getitem__(self, n) -> int|float:
|
|
425
|
+
return self.values[n] if isinstance(n, int) else self.values[self.keys.index(n)]
|
|
426
|
+
|
|
427
|
+
def __iter__(self) -> Generator[float, Any, None]:
|
|
428
|
+
for val in self.values:
|
|
429
|
+
yield val
|
|
430
|
+
|
|
431
|
+
@classmethod
|
|
432
|
+
def __normalize__(cls, other) -> "Color":
|
|
433
|
+
if isinstance(other, Color):
|
|
434
|
+
return other
|
|
435
|
+
if isinstance(other, (int, float)):
|
|
436
|
+
return cls(other, other, other)
|
|
437
|
+
if isinstance(other, (list, tuple)):
|
|
438
|
+
return cls(*other[:3])
|
|
439
|
+
try:
|
|
440
|
+
return cls(*other.values, mode=other.mode)
|
|
441
|
+
except:
|
|
442
|
+
raise TypeError(f"The value {other} of type {type(other)} is not a num type: [{int|float}] nor an array type: [{list|tuple}]")
|
|
443
|
+
|
|
444
|
+
@classmethod
|
|
445
|
+
def white(cls) -> "Color": return Color(255,255,255)
|
|
446
|
+
@classmethod
|
|
447
|
+
def black(cls) -> "Color": return Color(0,0,0)
|
|
448
|
+
@classmethod
|
|
449
|
+
def red(cls) -> "Color": return Color(255,0,0)
|
|
450
|
+
@classmethod
|
|
451
|
+
def green(cls) -> "Color": return Color(0,255,0)
|
|
452
|
+
@classmethod
|
|
453
|
+
def blue(cls) -> "Color": return Color(0,0,255)
|
|
454
|
+
|
|
455
|
+
# @classmethod
|
|
456
|
+
# def (cls) -> "Color": return Color(0,0,255)
|
|
457
|
+
|
|
458
|
+
@classmethod
|
|
459
|
+
def randomize(cls) -> "Color":
|
|
460
|
+
return Color(__randint__(0,255), __randint__(0,255), __randint__(0,255))
|
|
461
|
+
|
|
462
|
+
WHITE_COLOR_PYG = Color.white()()
|
|
463
|
+
BLACK_COLOR_PYG = Color.black()()
|
|
464
|
+
RED_COLOR_PYG = Color.red()()
|
|
465
|
+
GREEN_COLOR_PYG = Color.green()()
|
|
466
|
+
BLUE_COLOR_PYG = Color.blue()()
|