Skip to content
This repository was archived by the owner on Aug 11, 2025. It is now read-only.

Commit cda72dc

Browse files
committed
Merge pull request #17 from TheDutchCoder/feature-disable-alpha
Added "enable_alpha" option + menu.
2 parents a007005 + 4990bd0 commit cda72dc

File tree

3 files changed

+100
-24
lines changed

3 files changed

+100
-24
lines changed

Main.sublime-menu

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[
2+
{
3+
"caption": "Preferences",
4+
"mnemonic": "n",
5+
"id": "preferences",
6+
"children":
7+
[
8+
{
9+
"caption": "Package Settings",
10+
"mnemonic": "P",
11+
"id": "package-settings",
12+
"children":
13+
[
14+
{
15+
"caption": "CSS Color Converter",
16+
"children":
17+
[
18+
{
19+
"command": "open_file", "args":
20+
{
21+
"file": "${packages}/CSS Color Converter/colorconvert.sublime-settings"
22+
},
23+
"caption": "Settings – Default"
24+
}
25+
]
26+
}
27+
]
28+
}
29+
]
30+
}
31+
]

colorconvert.py

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import re
1313
import math
1414

15+
1516
# Global color channel values.
1617
_hr = None
1718
_hg = None
@@ -25,7 +26,8 @@
2526
_s = None
2627
_l = None
2728

28-
alpha = 1.0
29+
_alpha = 1.0
30+
_alpha_enabled = True
2931

3032

3133
class colorconvertCommand(sublime_plugin.TextCommand):
@@ -49,6 +51,12 @@ def __init__(self, view):
4951
# Store the view.
5052
self.view = view
5153

54+
# Load settings
55+
settings = sublime.load_settings("colorconvert.sublime-settings")
56+
settings.add_on_change("enable_alpha", loadSettings)
57+
58+
loadSettings()
59+
5260

5361
def hueToRgb(self, v1, v2, vH):
5462
"""Assists with converting hsl to rgb values.
@@ -94,7 +102,7 @@ def hexToRgb(self, hr, hg, hb):
94102
global _g
95103
global _b
96104

97-
global alpha
105+
global _alpha
98106

99107
r = (int(hr, 16))
100108
g = (int(hg, 16))
@@ -106,7 +114,7 @@ def hexToRgb(self, hr, hg, hb):
106114
_g = g
107115
_b = b
108116

109-
return [r, g, b, alpha]
117+
return [r, g, b, _alpha]
110118

111119

112120
def rgbToHex(self, r, g, b, a):
@@ -121,11 +129,11 @@ def rgbToHex(self, r, g, b, a):
121129
122130
"""
123131

124-
global alpha
132+
global _alpha
125133

126134
# Get the alpha channel and store it globally (if present).
127135
if a is not None:
128-
alpha = a
136+
_alpha = a
129137

130138
# Convert the channels' values to hex values.
131139
r = hex(r)[2:].zfill(2)
@@ -157,11 +165,11 @@ def rgbToHsl(self, r, g, b, a):
157165
global _s
158166
global _l
159167

160-
global alpha
168+
global _alpha
161169

162170
# Get the alpha channel and store it globally (if present).
163171
if a is not None:
164-
alpha = a
172+
_alpha = a
165173

166174
r = float(r) / 255.0
167175
g = float(g) / 255.0
@@ -206,7 +214,7 @@ def rgbToHsl(self, r, g, b, a):
206214
_s = s
207215
_l = l
208216

209-
return [h, s, l, alpha]
217+
return [h, s, l, _alpha]
210218

211219

212220
def hslToRgb(self, h, s, l, a):
@@ -221,15 +229,15 @@ def hslToRgb(self, h, s, l, a):
221229
222230
"""
223231

224-
global alpha
232+
global _alpha
225233

226234
h = float(h)
227235
s = float(s) / 100
228236
l = float(l) / 100
229237

230238
# Get the alpha channel and store it globally (if present).
231239
if a is not None:
232-
alpha = a
240+
_alpha = a
233241

234242
# Unsaturated colors have equal rgb channels.
235243
if s is 0:
@@ -256,7 +264,7 @@ def hslToRgb(self, h, s, l, a):
256264
g = int(math.ceil(g))
257265
b = int(math.ceil(b))
258266

259-
return [r, g, b, alpha]
267+
return [r, g, b, _alpha]
260268

261269

262270
# Main function.
@@ -287,7 +295,8 @@ def run(self, edit):
287295
global _l
288296

289297
# Global values for the alpha channel.
290-
global alpha
298+
global _alpha
299+
global _alpha_enabled
291300

292301
for sel in sels:
293302

@@ -345,15 +354,24 @@ def run(self, edit):
345354
# accuracy. Otherwise convert the values.
346355
if _r is not None:
347356

348-
rgba_vals = [_r, _g, _b, alpha]
357+
rgba_vals = [_r, _g, _b, _alpha]
349358

350359
else:
351360

352361
rgba_vals = self.hexToRgb(_hr, _hg, _hb)
353362

354363
# Write the output to the selection.
355-
output = 'rgba(%d, %d, %d, %s)' % (rgba_vals[0], rgba_vals[1],
356-
rgba_vals[2], rgba_vals[3])
364+
if _alpha_enabled is True:
365+
366+
output = 'rgba(%d, %d, %d, %s)' % (rgba_vals[0],
367+
rgba_vals[1],
368+
rgba_vals[2],
369+
rgba_vals[3])
370+
else:
371+
372+
output = 'rgba(%d, %d, %d)' % (rgba_vals[0], rgba_vals[1],
373+
rgba_vals[2])
374+
357375
self.view.replace(edit, sel, output)
358376

359377

@@ -376,23 +394,32 @@ def run(self, edit):
376394
a = float(rgb_match.group(4))
377395

378396
else:
379-
a = alpha
397+
a = _alpha
380398

381399
# If global hsl values are present, use those for speed and
382400
# accuracy. Otherwise convert the values.
383401
if _h is not None:
384402

385-
hsla_vals = [_h, _s, _l, alpha]
403+
hsla_vals = [_h, _s, _l, _alpha]
386404

387405
else:
388406

389407
hsla_vals = self.rgbToHsl(r, g, b, a)
390408

391409
# Write the output to the selection.
392-
output = 'hsla(%.1f, %.1f%%, %.1f%%, %s)' % (hsla_vals[0],
410+
if _alpha_enabled is True:
411+
412+
output = 'hsla(%.1f, %.1f%%, %.1f%%, %s)' % (hsla_vals[0],
413+
hsla_vals[1],
414+
hsla_vals[2],
415+
hsla_vals[3])
416+
417+
else:
418+
419+
output = 'hsla(%.1f, %.1f%%, %.1f%%)' % (hsla_vals[0],
393420
hsla_vals[1],
394-
hsla_vals[2],
395-
hsla_vals[3])
421+
hsla_vals[2])
422+
396423
self.view.replace(edit, sel, output)
397424

398425

@@ -415,19 +442,19 @@ def run(self, edit):
415442
a = float(hsl_match.group(4))
416443

417444
else:
418-
a = alpha
445+
a = _alpha
419446

420447
# If global hex or rgb values are present, use those for speed
421448
# and accuracy. Otherwise convert the values.
422449
if _hr is not None:
423450

424-
hex_vals = [_hr, _hg, _hb, alpha]
451+
hex_vals = [_hr, _hg, _hb, _alpha]
425452

426453
else:
427454

428455
if _r is not None:
429456

430-
rgba_vals = [_r, _g, _b, alpha]
457+
rgba_vals = [_r, _g, _b, _alpha]
431458

432459
else:
433460

@@ -473,7 +500,7 @@ def on_selection_modified(self, view):
473500
global _s
474501
global _l
475502

476-
global alpha
503+
global _alpha
477504

478505
# If the selection changed to a 'cursor' (e.g. no selection at all)
479506
# then we reset the global values.
@@ -492,3 +519,18 @@ def on_selection_modified(self, view):
492519
_l = None
493520

494521
alpha = 1.0
522+
523+
524+
def loadSettings():
525+
"""Loads settings from the .sublime-settings file
526+
527+
Attributes:
528+
sublime_plugin.EventListener: Sublime Text class basis.
529+
530+
"""
531+
532+
global _alpha_enabled
533+
534+
settings = sublime.load_settings("colorconvert.sublime-settings")
535+
536+
_alpha_enabled = settings.get("enable_alpha")

colorconvert.sublime-settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"enable_alpha": true
3+
}

0 commit comments

Comments
 (0)