-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ch_native_picker.py
184 lines (147 loc) · 5.38 KB
/
ch_native_picker.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
"""OS specific color pickers."""
import sublime
import subprocess
import time
MAC_CHOOSE_COLOR = '''\
tell current application to choose color default color {{{}, {}, {}}}
'''
UINT = 65535
if sublime.platform() == "windows":
import ctypes
class CHOOSECOLOR(ctypes.Structure):
"""Structure for `CHOOSECOLOR`."""
_fields_ = [('lStructSize', ctypes.c_uint32),
('hwndOwner', ctypes.c_void_p),
('hInstance', ctypes.c_void_p),
('rgbResult', ctypes.c_uint32),
('lpCustColors', ctypes.POINTER(ctypes.c_uint32)),
('Flags', ctypes.c_uint32),
('lCustData', ctypes.c_void_p),
('lpfnHook', ctypes.c_void_p),
('lpTemplateName', ctypes.c_wchar_p)]
CC_SOLIDCOLOR = 0x80
CC_RGBINIT = 0x01
CC_FULLOPEN = 0x02
ChooseColorW = ctypes.windll.Comdlg32.ChooseColorW
ChooseColorW.argtypes = [ctypes.POINTER(CHOOSECOLOR)]
ChooseColorW.restype = ctypes.c_int32
class _ColorPicker:
"""Generic color picker class."""
def __init__(self, color):
"""Initialize the color."""
self.base = type(color)
self.color = color
def pick(self):
"""Pick the color."""
return self.color
class MacPick(_ColorPicker):
"""MacOS color picker."""
def pick(self):
"""Pick the color."""
color = self.color.convert('srgb').normalize()
coords = [x * UINT for x in color.clone().fit()[:-1]]
try:
p = subprocess.Popen(
['osascript', '-e', MAC_CHOOSE_COLOR.format(*coords)],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
out = p.communicate()
returncode = p.returncode
if returncode:
color = None
else:
color = self.base("srgb", [int(x) / UINT for x in out[0].split(b', ')])
self.color = color
except Exception:
color = None
return color
class WinPick(_ColorPicker):
"""
Windows color picker.
https://docs.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-choosecolorw-r1
"""
def get_win_pick_colors(self):
"""Get windows custom colors."""
CustomColors = ctypes.c_uint32 * 16 # noqa: N806
colors = sublime.load_settings('color_helper.palettes').get("win_picker_custom", [])
length = len(colors)
if length > 16:
colors = colors[0:16]
if length < 16:
delta = 16 - length
colors.extend(['color(srgb 1 1 1)'] * delta)
for index, color in enumerate(colors, 0):
try:
hx = self.base(color).to_string(hex=True, alpha=False)[1:]
colors[index] = int(hx[4:6] + hx[2:4] + hx[0:2], 16)
except Exception:
colors[index] = 0xffffff
return CustomColors(*colors)
def set_win_pick_colors(self, colors):
"""Set windows custom colors."""
pcolors = []
for index in range(16):
hx = '{:06x}'.format(colors[index])
pcolors.append(
self.base('rgb({} {} {})'.format(int(hx[4:6], 16), int(hx[2:4], 16), int(hx[0:2], 16))).to_string(
color=True, fit=False, precision=-1
)
)
s = sublime.load_settings('color_helper.palettes')
s.set("win_picker_custom", pcolors)
sublime.save_settings('color_helper.palettes')
def pick(self):
"""Pick the color."""
color = self.color.convert('srgb').normalize()
hx = color.to_string(hex=True, alpha=False)[1:]
bgr = int(hx[4:6] + hx[2:4] + hx[0:2], 16)
picker = CHOOSECOLOR()
picker.lStructSize = ctypes.sizeof(picker)
picker.lpCustColors = self.get_win_pick_colors()
picker.Flags = CC_SOLIDCOLOR | CC_FULLOPEN | CC_RGBINIT
picker.rgbResult = ctypes.c_uint32(bgr)
time.sleep(1)
if ChooseColorW(ctypes.pointer(picker)):
hx = '{:06x}'.format(picker.rgbResult)
color = self.base('rgb({} {} {})'.format(int(hx[4:6], 16), int(hx[2:4], 16), int(hx[0:2], 16)))
self.color = color
else:
color = None
self.set_win_pick_colors(picker.lpCustColors)
return color
class LinuxPick(_ColorPicker):
"""
Linux color picker.
https://apps.kde.org/en/kcolorchooser
"""
def pick(self):
"""Pick the color."""
color = self.color.convert('srgb').normalize()
hx = color.to_string(hex=True, alpha=False)
try:
p = subprocess.Popen(
['kcolorchooser', '--print', '--color', hx],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
out = p.communicate()
returncode = p.returncode
if returncode:
color = None
else:
color = self.base(out[0].decode('utf-8').strip())
self.color = color
except Exception:
color = None
return color
def pick(color):
"""Get the color picker for the OS."""
platform = sublime.platform()
if platform == "windows":
picker = WinPick(color)
elif platform == "osx":
picker = MacPick(color)
else:
picker = LinuxPick(color)
return picker.pick()