1212import re
1313
1414
15+ alpha = 1.0
16+
17+
1518class colorconvertCommand (sublime_plugin .TextCommand ):
1619 """Converts a CSS color from hex to rgba to hsla to hex (etc).
1720
@@ -42,8 +45,6 @@ def __init__(self, view):
4245 self .s = 0.0
4346 self .l = 0.0
4447
45- self .alpha = 1.0
46-
4748
4849 def hexToRgb (self , r , g , b ):
4950 """Converts a hex value to an rgb value.
@@ -60,7 +61,9 @@ def hexToRgb(self, r, g, b):
6061 g = (int (g , 16 ))
6162 b = (int (b , 16 ))
6263
63- return [r , g , b , self .alpha ]
64+ global alpha
65+
66+ return [r , g , b , alpha ]
6467
6568
6669 def rgbToHex (self , r , g , b , a ):
@@ -79,9 +82,11 @@ def rgbToHex(self, r, g, b, a):
7982 g = hex (g )[2 :].zfill (2 )
8083 b = hex (b )[2 :].zfill (2 )
8184
85+ global alpha
86+
8287 # Get the alpha channel and store it globally (if present).
8388 if a is not None :
84- self . alpha = a
89+ alpha = a
8590
8691 # If a short notation is possible, splice the values.
8792 if (r [0 :1 ] == r [1 :2 ]) and (g [0 :1 ] == g [1 :2 ]) and (b [0 :1 ] == b [1 :2 ]):
@@ -108,9 +113,11 @@ def rgbToHsl(self, r, g, b, a):
108113 g = float (g ) / 255.0
109114 b = float (b ) / 255.0
110115
116+ global alpha
117+
111118 # Get the alpha channel and store it globally (if present).
112119 if a is not None :
113- self . alpha = a
120+ alpha = a
114121
115122 # Calculate the hsl values.
116123 cmax = max (r , g , b )
@@ -150,7 +157,7 @@ def rgbToHsl(self, r, g, b, a):
150157 self .s = s
151158 self .l = l
152159
153- return [h , s , l , self . alpha ]
160+ return [h , s , l , alpha ]
154161
155162
156163 def hslToRgb (self , h , s , l , a ):
@@ -169,9 +176,11 @@ def hslToRgb(self, h, s, l, a):
169176 s = float (s ) / 100
170177 l = float (l ) / 100
171178
179+ global alpha
180+
172181 # Get the alpha channel and store it globally (if present).
173182 if a is not None :
174- self . alpha = a
183+ alpha = a
175184
176185 # Calculate the rgb values.
177186 c = (1 - abs ((2 * l ) - 1 )) * s
@@ -212,7 +221,7 @@ def hslToRgb(self, h, s, l, a):
212221 g = int ((g + m ) * 255.0 )
213222 b = int ((b + m ) * 255.0 )
214223
215- return [r , g , b , self . alpha ]
224+ return [r , g , b , alpha ]
216225
217226
218227 # Main function.
@@ -227,6 +236,8 @@ def run(self, edit):
227236
228237 sels = self .view .sel ()
229238
239+ global alpha
240+
230241 for sel in sels :
231242
232243 # Get the selection and its length.
@@ -237,8 +248,8 @@ def run(self, edit):
237248 reg_hex = '^[\#]?([\dabcdefABCDEF]{3}){1,2}$'
238249 reg_rgb = ('^rgb[a]?\((\s*\d+\s*),(\s*\d+\s*),(\s*\d+\s*),'
239250 '?(\s*(0?.?\d)+\s*)?\)$' )
240- reg_hsl = ('^hsl[a]?\((\s*\d? .?\d+ \s*),(\s*\d? .?\d+ \%\s*),(\s*\d? .?\d+ \%\s*),'
241- '?(\s*(0? .?\d)+ \s*)?\)$' )
251+ reg_hsl = ('^hsl[a]?\((\s*\d+ .?\d? \s*),(\s*\d+ .?\d? \%\s*),(\s*\d+ .?\d? \%\s*),'
252+ '?(\s*\d+ .?\d? \s*)?\)$' )
242253
243254 hex_match = re .match (reg_hex , str )
244255 rgb_match = re .match (reg_rgb , str )
@@ -294,7 +305,7 @@ def run(self, edit):
294305 a = float (rgb_match .group (4 ))
295306
296307 else :
297- a = self . alpha
308+ a = alpha
298309
299310 # Get the hsl(a) values and output them to the current
300311 # selection.
@@ -319,7 +330,7 @@ def run(self, edit):
319330 a = float (hsl_match .group (4 ))
320331
321332 else :
322- a = self . alpha
333+ a = alpha
323334
324335 # Get the rgb(a) values, then the hex values and output them to
325336 # the current selection.
@@ -328,3 +339,30 @@ def run(self, edit):
328339 rgba_vals [2 ], rgba_vals [3 ])
329340 output = '#%s%s%s' % (hex_vals [0 ], hex_vals [1 ], hex_vals [2 ])
330341 self .view .replace (edit , sel , output )
342+
343+
344+ class colorconvertEvents (sublime_plugin .EventListener ):
345+ """Event listener for the ColorConvert plugin.
346+
347+ Attributes:
348+ sublime_plugin.EventListener: Sublime Text class basis.
349+
350+ """
351+
352+ def on_selection_modified (self , view ):
353+ """Listener for selection change.
354+
355+ Attributes:
356+ self: The colorconvertCommand object.
357+ view: The current view.
358+
359+ """
360+
361+ sels = view .sel ()
362+
363+ global alpha
364+
365+ # If the selection changed to a 'cursor' (e.g. no selection at all)
366+ # then we reset the alpha channel.
367+ if len (sels ) is 1 and sels [0 ].empty ():
368+ alpha = 1.0
0 commit comments