Skip to content
This repository was archived by the owner on Aug 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"caption": "Preferences",
"mnemonic": "n",
"id": "preferences",
"children":
[
{
"caption": "Package Settings",
"mnemonic": "P",
"id": "package-settings",
"children":
[
{
"caption": "CSS Color Converter",
"children":
[
{
"command": "open_file", "args":
{
"file": "${packages}/CSS Color Converter/colorconvert.sublime-settings"
},
"caption": "Settings – Default"
}
]
}
]
}
]
}
]
90 changes: 66 additions & 24 deletions colorconvert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import re
import math


# Global color channel values.
_hr = None
_hg = None
Expand All @@ -25,7 +26,8 @@
_s = None
_l = None

alpha = 1.0
_alpha = 1.0
_alpha_enabled = True


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

# Load settings
settings = sublime.load_settings("colorconvert.sublime-settings")
settings.add_on_change("enable_alpha", loadSettings)

loadSettings()


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

global alpha
global _alpha

r = (int(hr, 16))
g = (int(hg, 16))
Expand All @@ -106,7 +114,7 @@ def hexToRgb(self, hr, hg, hb):
_g = g
_b = b

return [r, g, b, alpha]
return [r, g, b, _alpha]


def rgbToHex(self, r, g, b, a):
Expand All @@ -121,11 +129,11 @@ def rgbToHex(self, r, g, b, a):

"""

global alpha
global _alpha

# Get the alpha channel and store it globally (if present).
if a is not None:
alpha = a
_alpha = a

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

global alpha
global _alpha

# Get the alpha channel and store it globally (if present).
if a is not None:
alpha = a
_alpha = a

r = float(r) / 255.0
g = float(g) / 255.0
Expand Down Expand Up @@ -206,7 +214,7 @@ def rgbToHsl(self, r, g, b, a):
_s = s
_l = l

return [h, s, l, alpha]
return [h, s, l, _alpha]


def hslToRgb(self, h, s, l, a):
Expand All @@ -221,15 +229,15 @@ def hslToRgb(self, h, s, l, a):

"""

global alpha
global _alpha

h = float(h)
s = float(s) / 100
l = float(l) / 100

# Get the alpha channel and store it globally (if present).
if a is not None:
alpha = a
_alpha = a

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

return [r, g, b, alpha]
return [r, g, b, _alpha]


# Main function.
Expand Down Expand Up @@ -287,7 +295,8 @@ def run(self, edit):
global _l

# Global values for the alpha channel.
global alpha
global _alpha
global _alpha_enabled

for sel in sels:

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

rgba_vals = [_r, _g, _b, alpha]
rgba_vals = [_r, _g, _b, _alpha]

else:

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

# Write the output to the selection.
output = 'rgba(%d, %d, %d, %s)' % (rgba_vals[0], rgba_vals[1],
rgba_vals[2], rgba_vals[3])
if _alpha_enabled is True:

output = 'rgba(%d, %d, %d, %s)' % (rgba_vals[0],
rgba_vals[1],
rgba_vals[2],
rgba_vals[3])
else:

output = 'rgba(%d, %d, %d)' % (rgba_vals[0], rgba_vals[1],
rgba_vals[2])

self.view.replace(edit, sel, output)


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

else:
a = alpha
a = _alpha

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

hsla_vals = [_h, _s, _l, alpha]
hsla_vals = [_h, _s, _l, _alpha]

else:

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

# Write the output to the selection.
output = 'hsla(%.1f, %.1f%%, %.1f%%, %s)' % (hsla_vals[0],
if _alpha_enabled is True:

output = 'hsla(%.1f, %.1f%%, %.1f%%, %s)' % (hsla_vals[0],
hsla_vals[1],
hsla_vals[2],
hsla_vals[3])

else:

output = 'hsla(%.1f, %.1f%%, %.1f%%)' % (hsla_vals[0],
hsla_vals[1],
hsla_vals[2],
hsla_vals[3])
hsla_vals[2])

self.view.replace(edit, sel, output)


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

else:
a = alpha
a = _alpha

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

hex_vals = [_hr, _hg, _hb, alpha]
hex_vals = [_hr, _hg, _hb, _alpha]

else:

if _r is not None:

rgba_vals = [_r, _g, _b, alpha]
rgba_vals = [_r, _g, _b, _alpha]

else:

Expand Down Expand Up @@ -473,7 +500,7 @@ def on_selection_modified(self, view):
global _s
global _l

global alpha
global _alpha

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

alpha = 1.0


def loadSettings():
"""Loads settings from the .sublime-settings file

Attributes:
sublime_plugin.EventListener: Sublime Text class basis.

"""

global _alpha_enabled

settings = sublime.load_settings("colorconvert.sublime-settings")

_alpha_enabled = settings.get("enable_alpha")
3 changes: 3 additions & 0 deletions colorconvert.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"enable_alpha": true
}