Skip to content

Commit

Permalink
馃敄 Color converter 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sergius02 committed Nov 21, 2020
2 parents 74892c3 + a0bb1cb commit c520d1a
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 18 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ This is an extension for [ULauncher](https://ulauncher.io/), it helps you to con

| ![alt](screenshots/hex.png) | ![alt](screenshots/rgb.png) |
|-----------------------------|-----------------------------|
| ![alt](screenshots/hsv.png) |
| ![alt](screenshots/hsv.png) | ![alt](screenshots/hsl.png) |
| ![alt](screenshots/cmyk.png)|

## Options

The options are the format that you want to convert
* hex > Hexadecimal
* rgb > RGB
* hsv > HSV
* hsl > HSL
* cmyk > CMYK

See the screenshots for examples

Expand All @@ -32,4 +35,3 @@ You can get the code to your clipboard, just select and pressing enter!
If you like my work you can

<a href="https://www.buymeacoffee.com/sergius02" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>

83 changes: 79 additions & 4 deletions converter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
def get_int_tuple(values: str):
return tuple(map(int, values.replace('(', '').replace(')', '').replace(' ', '').split(',')))

return tuple(map(int, remove_special_chars(values).split(',')))

def get_float_tuple(values: str):
return tuple(map(float, values.replace('(', '').replace(')', '').replace(' ', '').split(',')))

return tuple(map(float, remove_special_chars(values).split(',')))

def remove_special_chars(value: str):
return value.replace('(', '').replace(')', '').replace(' ', '').replace('%', '')


def normalize_hexadecimal(hex: str):
Expand All @@ -12,6 +15,27 @@ def normalize_hexadecimal(hex: str):
else:
return hex

def normalize_rgb(rgb: tuple):
result = str(rgb[0]) + ', '
result += str(rgb[1]) + ', '
result += str(rgb[2])

return result

def normalize_hsl_hsv(values: tuple):
result = str(int(values[0])) + ', '
result += str(int(values[1])) + '%, '
result += str(int(values[2])) + '%'

return result

def normalize_cmyk(cmyk: tuple):
result = str(int(cmyk[0])) + '%, '
result += str(int(cmyk[1])) + '%, '
result += str(int(cmyk[2])) + '%, '
result += str(int(cmyk[3])) + '%'

return result

# Thanks to https://www.w3resource.com/python-exercises/math/python-math-exercise-77.php
def rgb_to_hsv(rgb: tuple):
Expand All @@ -32,7 +56,7 @@ def rgb_to_hsv(rgb: tuple):
else:
s = (df / mx) * 100
v = mx * 100
return round(h, 1), round(s, 1), round(v, 1)
return round(h, 0), round(s, 0), round(v, 0)


# Thanks to https://www.rapidtables.com/convert/color/hsv-to-rgb.html
Expand Down Expand Up @@ -70,6 +94,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, 0), round(ll * 100, 0)

# 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, 0), round(v * 100, 0)


def rgb_to_hex(rgb: tuple):
return '#%02x%02x%02x' % rgb
Expand All @@ -78,3 +133,23 @@ def rgb_to_hex(rgb: tuple):
def hex_to_rgb(hex: str):
hex = hex.replace("#", "")
return tuple(int(hex[i:i + 2], 16) for i in (0, 2, 4))


def rgb_to_cmyk(rgb: tuple):
r, g, b = rgb[0]/255, rgb[1]/255, rgb[2]/255

k = 1 - max(r, g, b)
c = (1 - r - k) / (1 - k)
m = (1 - g - k) / (1 - k)
y = (1 - b - k) / (1 - k)

return round(c * 100, 0), round(m * 100, 0), round(y * 100, 0), round(k * 100, 0)

def cmyk_to_rgb(cmyk: tuple):
c, m, y, k = cmyk[0]/100, cmyk[1]/100, cmyk[2]/100, cmyk[3]/100

r = 255 * (1 - c) * (1 - k)
g = 255 * (1 - m) * (1 - k)
b = 255 * (1 - y) * (1 - k)

return int(r), int(g), int(b)
72 changes: 60 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,86 @@ def __init__(self):

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)
cmyk = converter.rgb_to_cmyk(rgb)

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

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)
cmyk = converter.rgb_to_cmyk(rgb)

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

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)
cmyk = converter.rgb_to_cmyk(rgb)

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

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)
cmyk = converter.rgb_to_cmyk(rgb)

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

return self.return_results(hexadecimal, rgb, hsv)
def cmyk(self, text):
cmyk = converter.get_float_tuple(text)

def return_results(self, hexadecimal, rgb, hsv):
rgb = converter.cmyk_to_rgb(cmyk)
hsv = converter.rgb_to_hsv(rgb)
hsl = converter.hsv_to_hsl(hsv)
hexadecimal = converter.rgb_to_hex(rgb)

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

@staticmethod
def return_results(hexadecimal, rgb, hsv, hsl, cmyk):
return [
ExtensionResultItem(
icon='images/icon.png',
name=hexadecimal,
description='HEX FORMAT',
description='HEX',
on_enter=CopyToClipboardAction(hexadecimal)
),
ExtensionResultItem(
icon='images/icon.png',
name=rgb.__str__(),
description='RGB FORMAT',
on_enter=CopyToClipboardAction(rgb.__str__())
name=converter.normalize_rgb(rgb),
description='RGB',
on_enter=CopyToClipboardAction(converter.normalize_rgb(rgb))
),
ExtensionResultItem(
icon='images/icon.png',
name=converter.normalize_hsl_hsv(hsv),
description='HSV',
on_enter=CopyToClipboardAction(converter.normalize_hsl_hsv(hsv))
),
ExtensionResultItem(
icon='images/icon.png',
name=hsv.__str__(),
description='HSV FORMAT',
on_enter=CopyToClipboardAction(hsv.__str__())
name=converter.normalize_hsl_hsv(hsl),
description='HSL',
on_enter=CopyToClipboardAction(converter.normalize_hsl_hsv(hsl))
),
ExtensionResultItem(
icon='images/icon.png',
name=converter.normalize_cmyk(cmyk),
description='CMYK',
on_enter=CopyToClipboardAction(converter.normalize_cmyk(cmyk))
)
]

Expand All @@ -75,6 +117,12 @@ 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))

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

return RenderResultListAction(items)


Expand Down
12 changes: 12 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
"type": "keyword",
"name": "hsv",
"default_value": "hsv"
},
{
"id": "color_converter_rgb",
"type": "keyword",
"name": "hsl",
"default_value": "hsl"
},
{
"id": "color_converter_rgb",
"type": "keyword",
"name": "cmyk",
"default_value": "cmyk"
}
]
}
Binary file added screenshots/cmyk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/hex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/hsl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/hsv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/rgb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c520d1a

Please sign in to comment.