-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinitializePositions.py
234 lines (189 loc) · 6.34 KB
/
initializePositions.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import json
from pynput import mouse
import pyautogui
import sys
def boolInput(question):
while True:
try:
return {"y": True, "n": False}[input(question + "(y/n) ").lower()]
except KeyError:
print("Invalid input please enter true or false")
# this function writes the json with the position content
filename = "positions.json"
name = input("Please enter the name of the position preset: ")
# validBucket = False
# while not validBucket:
# bucket = input("Has the position set a bucket? (y/n) ")
# validBucket = bucket == "y" or bucket == "n"
bucket = boolInput("Has the position set a bucket?")
twoColors = boolInput("Can there be drawn with two colors? (left and right click)")
clickToExpand = boolInput("Do you need to click to select (more) colors?")
clickToClose = False
if clickToExpand:
clickToClose = boolInput("Do you need to click to close the color picker?")
positions = {
name: {
"topleft": {
"x": -1,
"y": -1
},
"bottomright": {
"x": -1,
"y": -1
},
"primaryColor": {
"x": -1,
"y": -1
},
"secondaryColor": {
"x": -1,
"y": -1
},
"bucket": {
"x": -1,
"y": -1
},
"pen": {
"x": -1,
"y": -1
},
"clickToExpand": {
"x": -1,
"y": -1
},
"clickToClose": {
"x": -1,
"y": -1
},
"colors": {
}
}
}
status = "topleft"
listener = ''
if twoColors:
status = "primaryColor"
else:
if bucket:
status = "bucket"
def rgbToHex(r, g, b):
return '#{:02x}{:02x}{:02x}'.format(r, g, b)
def onClick(x, y, button, pressed):
global status
if pressed:
if button == mouse.Button.right:
# save the json file while appending it
oldData = {}
with open(filename, 'r') as f:
oldData = json.load(f)
with open(filename, 'w') as f:
oldData[name] = positions[name]
json.dump(oldData, f)
print("Saved to file")
listener.stop()
sys.exit()
return
# print("Click: " + str(x) + " " + str(y) +" " + str(button) + " " + str(pressed))
if status == "primaryColor":
positions[name]["primaryColor"]["x"] = x
positions[name]["primaryColor"]["y"] = y
status = "secondaryColor"
return
if status == "secondaryColor":
positions[name]["secondaryColor"]["x"] = x
positions[name]["secondaryColor"]["y"] = y
if bucket:
status = "bucket"
else:
status = "topleft"
return
if status == "bucket":
positions[name]["bucket"]["x"] = x
positions[name]["bucket"]["y"] = y
status = "pen"
return
if status == "pen":
positions[name]["pen"]["x"] = x
positions[name]["pen"]["y"] = y
# if clickToExpand:
# status = "clickToExpand"
# else:
status = "topleft"
# status = "topleft"
return
if status == "clickToExpand":
positions[name]["clickToExpand"]["x"] = x
positions[name]["clickToExpand"]["y"] = y
if clickToClose:
# print('click to close the color picker')
status = "clickToClose"
else:
status = "colors"
return
if status == "clickToClose":
positions[name]["clickToClose"]["x"] = x
positions[name]["clickToClose"]["y"] = y
# print('click to close the color picker')
# status = "topleft"
status = "skipClick"
return
if status == "topleft":
positions[name]["topleft"]["x"] = x
positions[name]["topleft"]["y"] = y
status = "bottomright"
return
if status == "bottomright":
positions[name]["bottomright"]["x"] = x
positions[name]["bottomright"]["y"] = y
if clickToExpand:
# print('click to expand the color picker to be able to click on all colors')
# status = "skipClick"
status = "clickToExpand"
else:
status = "colors"
return
if status == "skipClick":
# print('click to expand the color picker to be able to click on all colors')
status = "colors"
return
if status == "colors":
color = pyautogui.pixel(x, y)
hex = rgbToHex(color[0], color[1], color[2])
print(f'{x} {y} {hex}'.format(x, y, hex))
positions[name]["colors"][hex] = {
"x": x,
"y": y
}
if not clickToClose and clickToExpand:
status = "skipClick"
return
else: # button released
match status:
case "primaryColor":
print('put the mouse at the primary color and click')
case "secondaryColor":
print('put the mouse at the secondary color and click')
case "bucket":
print('click on the bucket')
case "pen":
print('put the mouse at the tool that you want to use to draw and click')
case "topleft":
print('put the mouse at the top left of the drawing canvas and click')
case "bottomright":
print('now at the bottom right')
case "colors":
print('and now over every printable color')
case "clickToExpand":
print('click to expand the color picker')
case "clickToClose":
print('click to close the color picker')
case "skipClick":
print('the next click is not counted. open the color picker and click on the next color')
def main():
with mouse.Listener(on_click=onClick) as l:
global listener
listener = l
onClick(-1, -1, mouse.Button.left, False) # trigger the first message
l.join()
if __name__ == "__main__":
main()