-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_classes.py
79 lines (67 loc) · 2.04 KB
/
base_classes.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
77
78
79
from dataclasses import dataclass
from PIL import Image
from io import BytesIO
@dataclass
class Color():
b: int
g: int
r: int
name: str
def __init__(self, b=None, g=None, r=None, hexcode=None, name=None):
if hexcode is not None:
self.from_hex(hexcode=hexcode, name=name)
else:
self.r = r if r is not None else 0
self.b = b if b is not None else 0
self.g = g if g is not None else 0
self.name = name
def get_color(self):
return (int(self.r), int(self.g), int(self.b))
def from_hex(self, hexcode, name=None):
#format: #400040
if len(hexcode)!=7:
return False
hexcode = hexcode[1:]
self.r = int(hexcode[0:2],16)
self.g = int(hexcode[2:4],16)
self.b = int(hexcode[4:],16)
self.name = 'picked' if name is None else name
# print(self.name, self.r, self.g, self.b)
def compare(self, other):
if other is None:
return False
if other.r != self.r or \
self.g != other.g or \
self.b != other.b:
return False
return True
def get_hex(self):
hex_code = '#{0:02x}{1:02x}{2:02x}'.format(self.r, self.g, self.b)
return hex_code
@dataclass
class Point():
x: int
y: int
def get_point(self):
return (int(self.x), int(self.y))
def __str__(self):
return str(self.x) + ',' + str(self.y)
def offset_point(self, offset=None, x_off=None, y_off=None):
if offset is not None:
return Point(self.x + offset, self.y + offset)
else:
return Point(int(self.x + x_off), int(self.y + y_off))
def array_to_data(array, resize=None):
im = Image.fromarray(array)
if resize is not None:
im = im.resize(resize)
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
def convert_to_int(var):
try:
var=int(var)
except ValueError:
var=None
return var