-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
170 lines (135 loc) · 5.89 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import logging
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.client.Extension import Extension
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
import converter
logger = logging.getLogger(__name__)
class HexCodeExtension(Extension):
def __init__(self):
super(HexCodeExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
def hexadecimal(self, text):
if converter.check_hex_format(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, hsl, cmyk)
else:
return self.return_format_error("hex")
def rgb(self, text):
if converter.check_rgb_format(text):
rgb = converter.get_int_tuple(text)
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, hsl, cmyk)
else:
return self.return_format_error("rgb")
def hsv(self, text):
if converter.check_hsv_hsl_format(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)
else:
return self.return_format_error("hsv")
def hsl(self, text):
if converter.check_hsv_hsl_format(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)
else:
return self.return_format_error("hsl")
def cmyk(self, text):
if converter.check_cmyk_format(text):
cmyk = converter.get_float_tuple(text)
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)
else:
return self.return_format_error("cmyk")
@staticmethod
def get_doc_info(color_format: str):
if color_format == "hex":
return "Example: #BD93F9"
if color_format == "rgb":
return "Example: 189, 147, 249"
if color_format == "hsv":
return "Example: 265, 41%, 98%"
if color_format == "hsl":
return "Example: 265, 91%, 78%"
if color_format == "cmyk":
return "Example: 24%, 41%, 0%, 2%"
return ""
def return_format_error(self, color_format: str):
return [
ExtensionResultItem(
icon='images/icon.png',
name="Incorrect format",
description=self.get_doc_info(color_format),
)
]
@staticmethod
def return_results(hexadecimal, rgb, hsv, hsl, cmyk):
return [
ExtensionResultItem(
icon='images/icon.png',
name=hexadecimal,
description='HEX',
on_enter=CopyToClipboardAction(hexadecimal)
),
ExtensionResultItem(
icon='images/icon.png',
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=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))
)
]
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
items = []
text = event.get_argument() or ""
if event.get_keyword() == extension.preferences.get("color_converter_hex"):
return RenderResultListAction(extension.hexadecimal(text))
if event.get_keyword() == extension.preferences.get("color_converter_rgb"):
return RenderResultListAction(extension.rgb(text))
if event.get_keyword() == extension.preferences.get("color_converter_hsv"):
return RenderResultListAction(extension.hsv(text))
if event.get_keyword() == extension.preferences.get("color_converter_hsl"):
return RenderResultListAction(extension.hsl(text))
if event.get_keyword() == extension.preferences.get("color_converter_cmyk"):
return RenderResultListAction(extension.cmyk(text))
return RenderResultListAction(items)
if __name__ == '__main__':
HexCodeExtension().run()