Skip to content

Commit

Permalink
✨ Added HSL
Browse files Browse the repository at this point in the history
Fix #2
  • Loading branch information
sergius02 committed Nov 21, 2020
1 parent ef65c19 commit 4530c5a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
31 changes: 31 additions & 0 deletions converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ def hsv_to_rgb(hsv: tuple):

return int((r+m)*255), int((g+m)*255), int((b+m)*255)

# Thanks to https://ariya.blogspot.com/2008/07/converting-between-hsl-and-hsv.html
def hsv_to_hsl(hsv: tuple):
h, s, v = hsv[0], hsv[1]/100, hsv[2]/100

hh = h
ll = (2.0 - s) * v
ss = s * v
if ll <= 1:
ss /= ll
else:
ss /= 2 - ll
ll /= 2

return round(hh, 0), round(ss * 100, 1), round(ll * 100, 1)

# Thanks to https://ariya.blogspot.com/2008/07/converting-between-hsl-and-hsv.html
def hsl_to_hsv(hsl: tuple):
hh, ss, ll = hsl[0], hsl[1]/100, hsl[2]/100
h, s, l = 0, 0, 0

h = hh
ll *= 2
if ll <= 1:
ss *= ll
else:
ss *= 2 - ll
v = (ll + ss) / 2
s = (2 * ss) / (ll + ss)

return round(h, 0), round(s * 100, 1), round(v * 100, 1)


def rgb_to_hex(rgb: tuple):
return '#%02x%02x%02x' % rgb
Expand Down
30 changes: 25 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,35 @@ def hexadecimal(self, text):
hexadecimal = converter.normalize_hexadecimal(text)
rgb = converter.hex_to_rgb(hexadecimal)
hsv = converter.rgb_to_hsv(rgb)
hsl = converter.hsv_to_hsl(hsv)

return self.return_results(hexadecimal, rgb, hsv)
return self.return_results(hexadecimal, rgb, hsv, hsl)

def rgb(self, text):
rgb = converter.get_int_tuple(text)
hsv = converter.rgb_to_hsv(rgb)
hexadecimal = converter.rgb_to_hex(rgb)
hsv = converter.rgb_to_hsv(rgb)
hsl = converter.hsv_to_hsl(hsv)

return self.return_results(hexadecimal, rgb, hsv)
return self.return_results(hexadecimal, rgb, hsv, hsl)

def hsv(self, text):
hsv = converter.get_float_tuple(text)
rgb = converter.hsv_to_rgb(hsv)
hexadecimal = converter.rgb_to_hex(rgb)
hsl = converter.hsv_to_hsl(hsv)

return self.return_results(hexadecimal, rgb, hsv, hsl)

def hsl(self, text):
hsl = converter.get_float_tuple(text)
hsv = converter.hsl_to_hsv(hsl)
rgb = converter.hsv_to_rgb(hsv)
hexadecimal = converter.rgb_to_hex(rgb)

return self.return_results(hexadecimal, rgb, hsv)
return self.return_results(hexadecimal, rgb, hsv, hsl)

def return_results(self, hexadecimal, rgb, hsv):
def return_results(self, hexadecimal, rgb, hsv, hsl):
return [
ExtensionResultItem(
icon='images/icon.png',
Expand All @@ -56,6 +67,12 @@ def return_results(self, hexadecimal, rgb, hsv):
name=hsv.__str__(),
description='HSV FORMAT',
on_enter=CopyToClipboardAction(hsv.__str__())
),
ExtensionResultItem(
icon='images/icon.png',
name=hsl.__str__(),
description='HSL FORMAT',
on_enter=CopyToClipboardAction(hsl.__str__())
)
]

Expand All @@ -75,6 +92,9 @@ def on_event(self, event, extension):
if event.get_keyword() == "hsv":
return RenderResultListAction(extension.hsv(text))

if event.get_keyword() == "hsl":
return RenderResultListAction(extension.hsl(text))

return RenderResultListAction(items)


Expand Down
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"type": "keyword",
"name": "hsv",
"default_value": "hsv"
},
{
"id": "color_converter_rgb",
"type": "keyword",
"name": "hsl",
"default_value": "hsl"
}
]
}

0 comments on commit 4530c5a

Please sign in to comment.