This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
util.py
120 lines (82 loc) · 2.34 KB
/
util.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
# coding: utf-8
"""Helper functions."""
DESKTOP_KEYS_FILE = 'desktop.keys'
ANDROID_KEYS_FILE = 'android.keys'
SINGLE_COMMENT_CHARS = ['//', ';', '#']
def apply_transparency(color, alpha):
"""Apply transparency level to given color in RGB format."""
return (color & 0xFFFFFF00) | (alpha)
def get_android_theme_keys():
"""Return list of Android theme keys."""
return get_theme_keys(ANDROID_KEYS_FILE)
def get_desktop_theme_keys():
"""Return list of desktop theme keys."""
return get_theme_keys(DESKTOP_KEYS_FILE)
def get_theme_keys(filename):
"""Return list of theme keys.
Arguments:
filename - path to file where keys are stored
"""
theme_keys = []
with open(filename, 'r') as fp:
theme_keys = [line.strip() for line in fp if line]
return theme_keys
def get_empty_theme():
"""Create object that contains empty theme."""
return {
'theme': {},
'background': bytearray()
}
def argb2rgba(argb):
"""Convert color from ARGB to RGBA format.
Arguments:
argb - color in ARGB format
"""
a, r, g, b = get_argb_from_color(argb)
return (r << 24) | (g << 16) | (b << 8) | a
def get_argb_from_color(argb):
"""Return typle of A, R, G, B components from given color.
Arguments:
rgb - color
"""
a = (argb & 0xFF000000) >> 24
r = (argb & 0x00FF0000) >> 16
g = (argb & 0x0000FF00) >> 8
b = (argb & 0x000000FF)
return a, r, g, b
def get_rgba_from_color(rgba):
"""Return typle of R, G, B, A components from given color.
Arguments:
rgba - color
"""
r = (rgba & 0xFF000000) >> 24
g = (rgba & 0x00FF0000) >> 16
b = (rgba & 0x0000FF00) >> 8
a = (rgba & 0x000000FF)
return r, g, b, a
def is_number(string):
"""Check if given string is a number.
Arguments:
string - string that supposed to have number
"""
try:
complex(string)
except ValueError:
return False
return True
def is_comment(line):
"""Check if line contains comment.
Arguments:
line - single line of file
"""
for char in SINGLE_COMMENT_CHARS:
if char in line:
return True
return False
def is_key_val_pair(line, sep):
"""Check if line contains key=val pair.
Arguments:
line - single line of file
sep - separator
"""
return sep in line