-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs_and_classes.py
150 lines (110 loc) · 4.42 KB
/
funcs_and_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from gui import *
from ast import literal_eval
#Color functions
def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
def calculate_alpha_color(color,bg_color,alpha):
alpha = alpha/255
red = color[0] * alpha + bg_color[0] * 1 * (1 - alpha)
green = color[1] * alpha + bg_color[1] * 1 * (1 - alpha)
blue = color[2] * alpha + bg_color[2] * 1 * (1 - alpha)
return (int(red),int(green),int(blue))
class color_class:
def __init__(self,_rgba_=[255,255,255,255]):
self._rgba_ = _rgba_
def set(self,color,color_type="RGBA"):
if color_type == "hex":
aux_rgb_color = hex_to_rgb(color)
self._rgba_ = [aux_rgb_color[0], aux_rgb_color[1], aux_rgb_color[2],self._rgba_[3],]
elif color_type == "rgb":
self._rgba_[0]=color[0]
self._rgba_[1]=color[1]
self._rgba_[2]=color[2]
else:
self._rgba_[0]=color[0]
self._rgba_[1]=color[1]
self._rgba_[2]=color[2]
self._rgba_[3]=color[3]
def setRed(self,red):
self._rgba_[0] = red
def getRed(self):
return self._rgba_[0]
def setGreen(self,green):
self._rgba_[1] = green
def getGreen(self):
return self._rgba_[1]
def setBlue(self,blue):
self._rgba_[2] = blue
def getBlue(self):
return self._rgba_[2]
def set255Alpha(self,alpha):
self._rgba_[3] = alpha
def get255Alpha(self):
return self._rgba_[3]
def toHex(self):
return rgb_to_hex((self._rgba_[0],self._rgba_[1],self._rgba_[2]))
def toRgb(self):
return (self._rgba_[0],self._rgba_[1],self._rgba_[2])
def toRgba(self):
return (self._rgba_[0],self._rgba_[1],self._rgba_[2],self._rgba_[3])
def get255alpha(self):
return self._rgba_[3]
def create_matrix(amount_rows,amount_columns):
matrix = []
for f in range(amount_rows):
matrix = matrix + [[]]
for c in matrix:
for f in range(amount_columns):
c.append((0,0,0,0))
return matrix
def read_matrix_from_file():
file_name = call_file_chooser(".pxpf")
if file_name != None:
with open(file_name,"r") as file:
new_matrix = literal_eval(file.read())
return new_matrix
else:
return None
class class_backup_manager:
def __init__(self):
self.backups = []
self.backup_current_index = -1
def create_backup(self):
print("BUCKUP ")
print(f"backup_current_index: {self.backup_current_index} backups: {len(self.backups)}")
if self.backup_current_index<9:
self.backup_current_index += 1
if len(self.backups) < 10:
aux_matrix = []
for row in range(amount_rows):
aux_matrix.append(matrix[row].copy())
self.backups.append(aux_matrix)
else:
aux_matrix = []
for row in range(amount_rows):
aux_matrix.append(matrix[row].copy())
self.backups.append(aux_matrix)
del self.backups[0]
def load_backup_to_canvas(self,event=None):
#print(self.backup_current_index)
mtx = self.backups[self.backup_current_index]
print("LOADED ")
print(f"backup_current_index: {self.backup_current_index} backups: {len(self.backups)}")
global matrix; matrix = mtx
if self.backup_current_index > 0:
del self.backups[self.backup_current_index]
if self.backup_current_index > 0:
self.backup_current_index -= 1
button_number = 0
for row in range(amount_rows):
for column in range(amount_columns):
button_number += 1
if not button_number % 2 == 0:
aux_hex_color = rgb_to_hex( calculate_alpha_color( mtx[row][column][0:3] ,hex_to_rgb("#222222"),mtx[row][column][3]))
else:
aux_hex_color = rgb_to_hex( calculate_alpha_color( mtx[row][column][0:3] ,hex_to_rgb("#333333"),mtx[row][column][3]))
canvas.itemconfig(button_number, fill= aux_hex_color,outline =aux_hex_color )