forked from thatsIch/sublime-rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorpicker.py
324 lines (252 loc) · 10.9 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""
This module is about the integration with the color picker.
The color picker can detect a color in a substring
and launch a tool to display the current color,
change it and thus also replace the old color.
It supports both ways Rainmeter defines color.
* RRGGBB
* RRGGBBAA
* RRR,GGG,BBB
* RRR,GGG,BBB,AAA
which is hexadecimal and decimal format.
"""
import os
import re
import subprocess
import sublime
import sublime_plugin
from . import logger
from .color import converter
class RainmeterReplaceColorCommand(sublime_plugin.TextCommand): # pylint: disable=R0903; we only need one method
"""
Replace a region with a text.
This command is required because the edit objects passed to TextCommand
are not transferable. We have to call this from the other command
to get a valid edit object.
"""
def run(self, edit, **args):
"""
Method is provided by Sublime Text through the super class TextCommand.
This is run automatically if you initialize the command
through an "command": "rainmeter_replace_color" command
with additional arguments:
* low: start of the region to replace
* high: end of the region to replace
* output: text which will replace the region
"""
low = args["low"]
high = args["high"]
output = args["output"]
region = sublime.Region(low, high)
original_str = self.view.substr(region)
self.view.replace(edit, region, output)
logger.info("Replacing '" + original_str + "' with '" + output + "'")
# encodes RRR,GGG,BBB,AAA with optional alpha channel and supporting all numbers from 0 to 999
# converter will check for 255
# numbers can be spaced anyway
DEC_COLOR_EXP = re.compile(r"(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(\d{1,3}))?")
# support lower and upper case hexadecimal with optional alpha channel
HEX_COLOR_EXP = re.compile(r"(?:[0-9a-fA-F]{2}){3,4}")
def color_or_default(color):
"""If the color is none it returns FFFFFFFF."""
return "FFFFFFFF" if color is None else color
class RainmeterColorPickCommand(sublime_plugin.TextCommand): # pylint: disable=R0903; we only need one method
"""Sublime Text integration running this through an action."""
def run(self, dummy_edit, **dummy_args):
"""
Method is provided by Sublime Text through the super class TextCommand.
This is run automatically if you initialize the command
through an "command": "rainmeter_color_pick" command.
"""
sublime.set_timeout_async(self.__run_picker, 0)
def __get_first_selection(self):
selections = self.view.sel()
first_selection = selections[0]
return first_selection
def __get_selected_line_index(self):
first_selection = self.__get_first_selection()
selection_start = first_selection.begin()
line_cursor = self.view.line(selection_start)
line_index = line_cursor.begin()
return line_index
def __get_selected_line_content(self):
first_selection = self.__get_first_selection()
selection_start = first_selection.begin()
line_cursor = self.view.line(selection_start)
line_content = self.view.substr(line_cursor)
return line_content
@staticmethod
def __get_selected_dec_or_none(caret, line_index, line_content):
# catch case with multiple colors in same line
for match in DEC_COLOR_EXP.finditer(line_content):
low = line_index + match.start()
high = line_index + match.end()
# need to shift the caret to the current line
if low <= caret <= high:
rgba_raw = match.groups()
rgba = [int(color) for color in rgba_raw if color is not None]
hexes = converter.rgbs_to_hexes(rgba)
hex_string = converter.hexes_to_string(hexes)
with_alpha = converter.convert_hex_to_hex_with_alpha(hex_string)
has_alpha = len(rgba) == 4
return low, high, with_alpha, True, False, has_alpha
return None
@staticmethod
def __get_selected_hex_or_none(caret, line_index, line_content):
# we can find multiple color values in the same row
# after iterating through the single elements
# we can use start() and end() of each match to determine the length
# and thus the area the caret had to be in,
# to identify th1e one we are currently in
for match in HEX_COLOR_EXP.finditer(line_content):
low = line_index + match.start()
high = line_index + match.end()
if low <= caret <= high:
hex_values = match.group(0)
is_lower = hex_values.islower()
# color picker requires RGBA
with_alpha = converter.convert_hex_to_hex_with_alpha(hex_values)
has_alpha = len(hex_values) == 8
return low, high, with_alpha, False, is_lower, has_alpha
else:
logger.info(low)
logger.info(high)
logger.info(caret)
return None
def __get_selected_color_or_none(self):
"""Return None in case of not finding the color aka no color is selected."""
caret = self.__get_first_selection().begin()
line_index = self.__get_selected_line_index()
line_content = self.__get_selected_line_content()
# catch case with multiple colors in same line
selected_dec_or_none = self.__get_selected_dec_or_none(caret, line_index, line_content)
if selected_dec_or_none is not None:
return selected_dec_or_none
# if no match was iterated we process furthere starting here
selected_hex_or_none = self.__get_selected_hex_or_none(caret, line_index, line_content)
if selected_hex_or_none is not None:
return selected_hex_or_none
return None, None, None, None, None, None
@staticmethod
def __get_picker_path():
packages = sublime.packages_path()
picker_path = os.path.join(
packages,
"User",
"Rainmeter",
"color",
"picker",
"ColorPicker_win.exe"
)
if not os.path.exists(picker_path):
logger.error("color picker was suposed to be copied to '" + picker_path + "'")
logger.info("found picker in '" + picker_path + "'")
return picker_path
def __replace_color(self, maybe_none, raw_output):
low, high, _, is_dec, is_lower, has_alpha = maybe_none
# if no color is selected we need to modify the low and high to match the caret
if all(value is None for value in maybe_none):
caret = self.__get_first_selection().begin()
low = caret
high = caret
output = raw_output[1:]
else:
output = self.__transform_raw_to_original_fmt(
raw_output,
is_dec,
has_alpha,
is_lower
)
self.view.run_command(
"rainmeter_replace_color",
{
"low": low,
"high": high,
"output": output
}
)
def __run_picker(self):
maybe_none = self.__get_selected_color_or_none()
_, _, maybe_color, _, _, _ = maybe_none
# no color selected, we call the color picker and insert the color at that position
color = color_or_default(maybe_color)
picker = subprocess.Popen(
[self.__get_picker_path(), color],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False
)
output_channel, error_channel = picker.communicate()
raw_output = output_channel.decode("utf-8")
logger.info("output: " + raw_output)
# checking for errors first
error = error_channel.decode("utf-8")
if error is not None and len(error) != 0:
logger.error("Color Picker Error:\n" + error)
return
# len is 9 because of RGBA and '#' resulting into 9 characters
if raw_output is not None and len(raw_output) == 9 and raw_output != 'CANCEL':
logger.info("can write back: " + raw_output)
self.__replace_color(maybe_none, raw_output)
@staticmethod
def __transform_raw_to_original_fmt(raw, is_dec, has_alpha, is_lower):
# cut output from the '#' because Rainmeter does not use # for color codes
output = raw[1:]
if is_dec:
output = converter.convert_hex_str_to_rgba_str(output, has_alpha)
# in case of hexadecimial representation
else:
# doing alpha calculation first so we do not need to catch ff and FF
alpha = output[-2:]
if not has_alpha and alpha is "FF":
output = output[:-2]
# it can be either originally in lower or upper case
if is_lower:
output = output.lower()
return output
def is_enabled(self, **dummy_args): # pylint: disable=R0201; sublime text API, no need for class reference
"""
Return True if the command is able to be run at this time.
The default implementation simply always returns True.
"""
# Check if current syntax is rainmeter
israinmeter = self.view.score_selector(self.view.sel()[0].a,
"source.rainmeter")
return israinmeter > 0
def is_visible(self, **args):
"""."""
if self.is_enabled():
return True
env = args.get("call_env", "")
return env != "context"
def __require_path(path):
if not os.path.exists(path):
os.makedirs(path)
def plugin_loaded():
"""Called automatically from ST3 if plugin is loaded.
Is required now due to async call and ignoring sublime.* from main routine
"""
packages = sublime.packages_path()
colorpicker_dir = os.path.join(packages, "User", "Rainmeter", "color", "picker")
__require_path(colorpicker_dir)
colorpicker_exe = os.path.join(colorpicker_dir, "ColorPicker_win.exe")
# could be already copied on a previous run
need_picker_exe = not os.path.exists(colorpicker_exe)
binary_picker = sublime.load_binary_resource(
"Packages/Rainmeter/color/picker/ColorPicker_win.exe"
)
# could be a newer version of the color picker be there
# only check if no newer is required since expensive
# generally happens in consecutive calls without updates
if not need_picker_exe:
need_picker_exe = os.path.getsize(colorpicker_exe) != len(binary_picker)
if need_picker_exe:
logger.info(
"Newer version of color picker found. Copying data over to '" + colorpicker_exe + "'"
)
with open(colorpicker_exe, "wb") as file_handler:
file_handler.write(binary_picker)
else:
logger.info(
"You are using the most current version of color picker. Continue loading..."
)