-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorPicker.py
74 lines (56 loc) · 2.21 KB
/
colorPicker.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
import ctypes
import ctypes.wintypes as wtypes
"""
https://stackoverflow.com/a/49237204/12471420
thanks!
"""
class CHOOSECOLOR(ctypes.Structure):
""" " a class to represent CWPRETSTRUCT structure
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646830(v=vs.85).aspx"""
_fields_ = [
("lStructSize", wtypes.DWORD),
("hwndOwner", wtypes.HWND),
("hInstance", wtypes.HWND),
("rgbResult", wtypes.COLORREF),
("lpCustColors", ctypes.POINTER(wtypes.COLORREF)),
("Flags", wtypes.DWORD),
("lCustData", wtypes.LPARAM),
("lpfnHook", wtypes.LPARAM),
("lpTemplateName", ctypes.c_char_p),
]
class ColorChooser:
"""a class to represent Color dialog box
https://msdn.microsoft.com/en-gb/library/windows/desktop/ms646912(v=vs.85).aspx"""
CC_SOLIDCOLOR = 0x80
CC_FULLOPEN = 0x02
CC_RGBINIT = 0x01
WS_EX_TOPMOST = 0x08
custom_color_array = ctypes.c_uint32 * 16
color_chooser = ctypes.windll.Comdlg32.ChooseColorW
def to_custom_color_array(self, custom_colors):
custom_int_colors = self.custom_color_array()
for i in range(16):
custom_int_colors[i] = (
rgb_to_int(*custom_colors[i]) if i < len(custom_colors) else rgb_to_int(*(255, 255, 255))
)
return custom_int_colors
def askcolor(self, hostId, initialColor, custom_colors):
struct = CHOOSECOLOR()
ctypes.memset(ctypes.byref(struct), 0, ctypes.sizeof(struct))
struct.lStructSize = ctypes.sizeof(struct)
struct.Flags = self.CC_SOLIDCOLOR | self.CC_FULLOPEN | self.CC_RGBINIT
struct.lpCustColors = self.to_custom_color_array(custom_colors)
struct.rgbResult = rgb_to_int(*initialColor)
struct.hwndOwner = hostId
if self.color_chooser(ctypes.byref(struct)):
result = int_to_rgb(struct.rgbResult)
else:
result = None
return result, [int_to_rgb(struct.lpCustColors[i]) for i in range(16)]
def rgb_to_int(red, green, blue):
return red + (green << 8) + (blue << 16)
def int_to_rgb(int_color):
red = int_color & 255
green = (int_color >> 8) & 255
blue = (int_color >> 16) & 255
return red, green, blue