|
1
|
|
|
"""This module is about converting from/to Rainmeter RGB(A).""" |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class LetterCase(object): # pylint: disable=R0903; used as enum, but we only have Py3.3 |
|
5
|
|
|
""" |
|
6
|
|
|
Represent how a hex value should be displayed. |
|
7
|
|
|
|
|
8
|
|
|
Depending on the incoming String |
|
9
|
|
|
we need to convert it to upper case or lower case. |
|
10
|
|
|
""" |
|
11
|
|
|
|
|
12
|
|
|
Upper, Lower = range(1, 3) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def hex_to_int(hex_value): |
|
16
|
|
|
"""Convert single hex value into a dec value.""" |
|
17
|
|
|
int_value = int(hex_value, 16) |
|
18
|
|
|
|
|
19
|
|
|
assert 0 <= int_value <= 255 |
|
20
|
|
|
|
|
21
|
|
|
return int_value |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def hexes_to_rgbs(hexes): |
|
25
|
|
|
"""Convert a list of hex values into a list of dec values.""" |
|
26
|
|
|
assert 3 <= len(hexes) <= 4 |
|
27
|
|
|
|
|
28
|
|
|
return [hex_to_int(hex_value) for hex_value in hexes] |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
def hexes_to_string(hexes): |
|
32
|
|
|
"""Convert hexes into a string representation.""" |
|
33
|
|
|
return "".join(hexes) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def convert_hex_str_to_rgba_str(hex_string, has_alpha): |
|
37
|
|
|
"""Provided 'FFFFFFFF' it should return 255, 255, 255, 255.""" |
|
38
|
|
|
hexes = [hex_string[i:i + 2] for i in range(0, len(hex_string), 2)] |
|
39
|
|
|
rgba = hexes_to_rgbs(hexes) |
|
40
|
|
|
alpha = rgba[-1] |
|
41
|
|
|
if alpha is 255 and not has_alpha: |
|
42
|
|
|
rgba = rgba[:-1] |
|
43
|
|
|
rgba_str = rgbs_to_string(rgba) |
|
44
|
|
|
|
|
45
|
|
|
return rgba_str |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def convert_hex_to_hex_with_alpha(hexes): |
|
49
|
|
|
"""If no alpha value is provided it defaults to FF.""" |
|
50
|
|
|
if len(hexes) == 6: |
|
51
|
|
|
if hexes.islower(): |
|
52
|
|
|
return hexes + "ff" |
|
53
|
|
|
else: |
|
54
|
|
|
# we default to upper case if user chose upper and lower |
|
55
|
|
|
return hexes + "FF" |
|
56
|
|
|
else: |
|
57
|
|
|
return hexes |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def int_to_hex(int_value, letter_case=LetterCase.Upper): |
|
61
|
|
|
"""Convert single int value in a specific letter case to hex value.""" |
|
62
|
|
|
assert 0 <= int_value <= 255 |
|
63
|
|
|
|
|
64
|
|
|
# returns lower case hex values |
|
65
|
|
|
hex_value = hex(int_value) |
|
66
|
|
|
without_prefix = hex_value[2:] |
|
67
|
|
|
padded = without_prefix.zfill(2) |
|
68
|
|
|
|
|
69
|
|
|
if letter_case == LetterCase.Upper: |
|
70
|
|
|
padded = padded.upper() |
|
71
|
|
|
|
|
72
|
|
|
return padded |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
def rgbs_to_hexes(rgbs): |
|
76
|
|
|
"""Convert a list of dec values into a list of hex values.""" |
|
77
|
|
|
assert 3 <= len(rgbs) <= 4 |
|
78
|
|
|
|
|
79
|
|
|
return [int_to_hex(rgb) for rgb in rgbs] |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def rgbs_to_string(rgbs, spacing=0): |
|
83
|
|
|
"""Convert RGBs into a string representation.""" |
|
84
|
|
|
joiner = ",".ljust(spacing + 1) |
|
85
|
|
|
|
|
86
|
|
|
# you cant join ints |
|
87
|
|
|
return joiner.join(str(x) for x in rgbs) |
|
88
|
|
|
|