-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex-percent.py
More file actions
33 lines (25 loc) · 890 Bytes
/
hex-percent.py
File metadata and controls
33 lines (25 loc) · 890 Bytes
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
#!/usr/bin/env python3
#[x-cmds]: UPDATE
"""Quickly convert a 2-digit HEX value to a percentage."""
from xulbux import FormatCodes, Console
ARGS = Console.get_args({"hex_value": "before"})
def hex_to_percent(hex_val: str) -> float:
return round((int(hex_val, 16) / 255) * 100, 2)
def main():
hex_val = (
ARGS.hex_value.values[0] if ARGS.hex_value.values else Console.input(
"\n[b](Enter 2 digit HEX value) (e.g. [br:cyan](FF)) [b](>) ",
min_len=2,
max_len=2,
allowed_chars="0123456789abcdefABCDEF",
).strip().upper()
)
percent = hex_to_percent(hex_val)
FormatCodes.print(f"\n [dim|br:white](=) [white][b]({percent})%[_]\n")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print()
except Exception as e:
Console.fail(e, start="\n", end="\n\n")