efficient vim config
[dotfiles/.git] / .local / lib / python3.9 / site-packages / pywal / util.py
1 """
2 Misc helper functions.
3 """
4 import colorsys
5 import json
6 import logging
7 import os
8 import subprocess
9 import sys
10
11
12 class Color:
13     """Color formats."""
14     alpha_num = "100"
15
16     def __init__(self, hex_color):
17         self.hex_color = hex_color
18
19     def __str__(self):
20         return self.hex_color
21
22     @property
23     def rgb(self):
24         """Convert a hex color to rgb."""
25         return "%s,%s,%s" % (*hex_to_rgb(self.hex_color),)
26
27     @property
28     def xrgba(self):
29         """Convert a hex color to xrdb rgba."""
30         return hex_to_xrgba(self.hex_color)
31
32     @property
33     def rgba(self):
34         """Convert a hex color to rgba."""
35         return "rgba(%s,%s,%s,%s)" % (*hex_to_rgb(self.hex_color),
36                                       int(self.alpha_num)/100)
37
38     @property
39     def alpha(self):
40         """Add URxvt alpha value to color."""
41         return "[%s]%s" % (self.alpha_num, self.hex_color)
42
43     @property
44     def octal(self):
45         """Export color in octal"""
46         return "%s%s" % ("#", oct(int(self.hex_color[1:], 16))[2:])
47
48     @property
49     def octal_strip(self):
50         """Strip '#' from octal color."""
51         return oct(int(self.hex_color[1:], 16))[2:]
52
53     @property
54     def strip(self):
55         """Strip '#' from color."""
56         return self.hex_color[1:]
57
58
59 def read_file(input_file):
60     """Read data from a file and trim newlines."""
61     with open(input_file, "r") as file:
62         return file.read().splitlines()
63
64
65 def read_file_json(input_file):
66     """Read data from a json file."""
67     with open(input_file, "r") as json_file:
68         return json.load(json_file)
69
70
71 def read_file_raw(input_file):
72     """Read data from a file as is, don't strip
73        newlines or other special characters.."""
74     with open(input_file, "r") as file:
75         return file.readlines()
76
77
78 def save_file(data, export_file):
79     """Write data to a file."""
80     create_dir(os.path.dirname(export_file))
81
82     try:
83         with open(export_file, "w") as file:
84             file.write(data)
85     except PermissionError:
86         logging.warning("Couldn't write to %s.", export_file)
87
88
89 def save_file_json(data, export_file):
90     """Write data to a json file."""
91     create_dir(os.path.dirname(export_file))
92
93     with open(export_file, "w") as file:
94         json.dump(data, file, indent=4)
95
96
97 def create_dir(directory):
98     """Alias to create the cache dir."""
99     os.makedirs(directory, exist_ok=True)
100
101
102 def setup_logging():
103     """Logging config."""
104     logging.basicConfig(format=("[%(levelname)s\033[0m] "
105                                 "\033[1;31m%(module)s\033[0m: "
106                                 "%(message)s"),
107                         level=logging.INFO,
108                         stream=sys.stdout)
109     logging.addLevelName(logging.ERROR, '\033[1;31mE')
110     logging.addLevelName(logging.INFO, '\033[1;32mI')
111     logging.addLevelName(logging.WARNING, '\033[1;33mW')
112
113
114 def hex_to_rgb(color):
115     """Convert a hex color to rgb."""
116     return tuple(bytes.fromhex(color.strip("#")))
117
118
119 def hex_to_xrgba(color):
120     """Convert a hex color to xrdb rgba."""
121     col = color.lower().strip("#")
122     return "%s%s/%s%s/%s%s/ff" % (*col,)
123
124
125 def rgb_to_hex(color):
126     """Convert an rgb color to hex."""
127     return "#%02x%02x%02x" % (*color,)
128
129
130 def darken_color(color, amount):
131     """Darken a hex color."""
132     color = [int(col * (1 - amount)) for col in hex_to_rgb(color)]
133     return rgb_to_hex(color)
134
135
136 def lighten_color(color, amount):
137     """Lighten a hex color."""
138     color = [int(col + (255 - col) * amount) for col in hex_to_rgb(color)]
139     return rgb_to_hex(color)
140
141
142 def blend_color(color, color2):
143     """Blend two colors together."""
144     r1, g1, b1 = hex_to_rgb(color)
145     r2, g2, b2 = hex_to_rgb(color2)
146
147     r3 = int(0.5 * r1 + 0.5 * r2)
148     g3 = int(0.5 * g1 + 0.5 * g2)
149     b3 = int(0.5 * b1 + 0.5 * b2)
150
151     return rgb_to_hex((r3, g3, b3))
152
153
154 def saturate_color(color, amount):
155     """Saturate a hex color."""
156     r, g, b = hex_to_rgb(color)
157     r, g, b = [x/255.0 for x in (r, g, b)]
158     h, l, s = colorsys.rgb_to_hls(r, g, b)
159     s = amount
160     r, g, b = colorsys.hls_to_rgb(h, l, s)
161     r, g, b = [x*255.0 for x in (r, g, b)]
162
163     return rgb_to_hex((int(r), int(g), int(b)))
164
165
166 def rgb_to_yiq(color):
167     """Sort a list of colors."""
168     return colorsys.rgb_to_yiq(*hex_to_rgb(color))
169
170
171 def disown(cmd):
172     """Call a system command in the background,
173        disown it and hide it's output."""
174     subprocess.Popen(cmd,
175                      stdout=subprocess.DEVNULL,
176                      stderr=subprocess.DEVNULL)
177
178
179 def get_pid(name):
180     """Check if process is running by name."""
181     try:
182         subprocess.check_output(["pidof", "-s", name])
183     except subprocess.CalledProcessError:
184         return False
185
186     return True