-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColorConvert.py
223 lines (172 loc) · 6.06 KB
/
ColorConvert.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sublime
import sublime_plugin
import re
from .utils import util
from .utils import colorName
matchRGBNumber = re.compile( r'\d{1,3}')
"""settings"""
convertMode = 'rgb'
capitalization = True
isAndroid = False
lossTransparent = False
class ColorConvertCommand(sublime_plugin.TextCommand):
# all value outputs
outputs = []
# view and edit
view = None
edit = None
# if innerConvertMode != "", must convert to innerConvertMode
innerConvertMode = convertMode
def __init__(self, view):
# Load settings
self.view = view
global convertMode
global capitalization
global isAndroid
global loss_transparent
settings = sublime.load_settings("ColorConvert.sublime-settings")
capitalization = settings.get("capitalization")
lossTransparent = settings.get("loss_transparent")
settings.add_on_change("convertMode", loadSettings) # addEventListener for convertMode
settings.add_on_change("capitalization", loadSettings) # addEventListener for capitalization
settings.add_on_change("isAndroid", loadSettings)
settings.add_on_change("lossTransparent", loadSettings)
loadSettings()
# main
def run(self, edit, value, isSelect):
self.outputs = []
self.edit = edit
if (value != ""):
self.innerConvertMode = value
else:
self.innerConvertMode = convertMode
# select section entry
if isSelect:
self.selectModeReplace(self.view.sel())
# all page(todo)
else:
self.allReplace()
def handle(self, selectPart):
# selectPart: '#1722DF' or 'rgba(0,0,0,1)' or 'hsla(100, 80.0%, 29.2%, 0.2)'...
output = util.convertColor(selectPart, self.innerConvertMode, isAndroid) # core handle function
if output != None:
if capitalization:
output = output.upper()
self.outputs.append(output)
"""replace view select color"""
def selectModeReplace(self, regions):
"""convert select section"""
for region in regions:
if not region.empty():
self.handle(self.view.substr(region))
for i, output in enumerate(self.outputs):
for j, region in enumerate(regions):
if i == j and not region.empty():
self.view.replace(self.edit, region, output)
"""replace view all color"""
def allReplace(self):
for name in ['rgb', 'rgba', 'hsl', 'hsla', 'hex', 'cmyk', 'hsv']:
currentMatchRegion = self.view.find(util.matchRE.get(name), 0, sublime.IGNORECASE)
allMatchRegin = self.view.find_all(util.matchRE.get(name), sublime.IGNORECASE)
if name == self.innerConvertMode:
continue
for i in range(len(allMatchRegin)):
if currentMatchRegion.empty():
continue
output = util.convertColor(self.view.substr(currentMatchRegion), self.innerConvertMode, isAndroid) # core handle function
if output != None:
self.view.replace(self.edit, currentMatchRegion, convertCase(output))
currentMatchRegion = self.view.find(util.matchRE.get(name), currentMatchRegion.end(), sublime.IGNORECASE)
class ColorConvertNameToHexCommand(sublime_plugin.TextCommand):
"""color name convert to hex
"""
def __init__(self, view):
self.view = view
# main
def run(self, edit):
self.edit = edit
self.convertColorName()
"""convert color name"""
def convertColorName(self):
regions = self.view.sel()
outputs = []
for region in regions:
if not region.empty():
name = colorName.mapColorName.get(self.view.substr(region).lower(), self.view.substr(region))
output = colorName.colorName.get(name, name)
if output != name:
outputs.append(output)
for i, output in enumerate(outputs):
for j, region in enumerate(regions):
if i == j and not region.empty():
self.view.replace(self.edit, region, convertCase(output))
class ColorConvertHexToNameCommand(sublime_plugin.TextCommand):
"""HEX convert To ColorName"""
def __init__(self, view):
self.view = view
# main
def run(self, edit):
self.edit = edit
self.covertColorName()
def covertColorName(self):
"""covert color name"""
regions = self.view.sel()
outputs = []
hexsDict = colorName.getHexColorNameData()
for region in regions:
if not region.empty():
hexName = util.handleHEXValueString(self.view.substr(region), isAndroid, lossTransparent)
if hexsDict.get(hexName):
outputs.append(hexsDict.get(hexName))
for i, output in enumerate(outputs):
for j, region in enumerate(regions):
if i == j and not region.empty():
self.view.replace(self.edit, region, convertCase(output, True))
class ColorConvertAllHexToNameCommand(sublime_plugin.TextCommand):
"""all HEX convert To ColorName"""
def __init__(self, view):
self.view = view
# main
def run(self, edit):
self.edit = edit
self.covertColorName()
def covertColorName(self):
"""covert color name"""
currentMatchRegion = self.view.find(util.matchRE.get('hex'), 0, sublime.IGNORECASE)
allMatchRegin = self.view.find_all(util.matchRE.get('hex'), sublime.IGNORECASE)
hexsDict = colorName.getHexColorNameData()
for i in range(len(allMatchRegin)):
if currentMatchRegion.empty():
continue
select = util.handleHEXValueString(self.view.substr(currentMatchRegion), isAndroid, lossTransparent)
if select != None:
hexName = hexsDict.get(select.upper())
if hexName:
self.view.replace(self.edit, currentMatchRegion, convertCase(hexName, True))
currentMatchRegion = self.view.find(util.matchRE.get('hex'), currentMatchRegion.end(), sublime.IGNORECASE)
def convertCase(value, isColorName = False):
"""Change case according to configuration
Arguments:
value {[str]} -- value data.
isColorName {boolean} -- color name use Hump uppercase
Returns:
[str] -- Case value
"""
if capitalization:
if isColorName:
return colorName.mapColorName.get(value.lower())
return value.upper()
return value.lower()
def loadSettings():
"""Loads settings from the ColorConvert.sublime-settings file"""
global convertMode
global capitalization
global isAndroid
global lossTransparent
settings = sublime.load_settings("ColorConvert.sublime-settings")
convertMode = settings.get("convert_mode")
capitalization = settings.get("capitalization")
isAndroid = settings.get("is_android")
lossTransparent = settings.get("loss_transparent")