16 def __init__(self, hex_color):
17 self.hex_color = hex_color
24 """Convert a hex color to rgb."""
25 return "%s,%s,%s" % (*hex_to_rgb(self.hex_color),)
29 """Convert a hex color to xrdb rgba."""
30 return hex_to_xrgba(self.hex_color)
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)
40 """Add URxvt alpha value to color."""
41 return "[%s]%s" % (self.alpha_num, self.hex_color)
45 """Export color in octal"""
46 return "%s%s" % ("#", oct(int(self.hex_color[1:], 16))[2:])
49 def octal_strip(self):
50 """Strip '#' from octal color."""
51 return oct(int(self.hex_color[1:], 16))[2:]
55 """Strip '#' from color."""
56 return self.hex_color[1:]
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()
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)
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()
78 def save_file(data, export_file):
79 """Write data to a file."""
80 create_dir(os.path.dirname(export_file))
83 with open(export_file, "w") as file:
85 except PermissionError:
86 logging.warning("Couldn't write to %s.", export_file)
89 def save_file_json(data, export_file):
90 """Write data to a json file."""
91 create_dir(os.path.dirname(export_file))
93 with open(export_file, "w") as file:
94 json.dump(data, file, indent=4)
97 def create_dir(directory):
98 """Alias to create the cache dir."""
99 os.makedirs(directory, exist_ok=True)
103 """Logging config."""
104 logging.basicConfig(format=("[%(levelname)s\033[0m] "
105 "\033[1;31m%(module)s\033[0m: "
109 logging.addLevelName(logging.ERROR, '\033[1;31mE')
110 logging.addLevelName(logging.INFO, '\033[1;32mI')
111 logging.addLevelName(logging.WARNING, '\033[1;33mW')
114 def hex_to_rgb(color):
115 """Convert a hex color to rgb."""
116 return tuple(bytes.fromhex(color.strip("#")))
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,)
125 def rgb_to_hex(color):
126 """Convert an rgb color to hex."""
127 return "#%02x%02x%02x" % (*color,)
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)
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)
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)
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)
151 return rgb_to_hex((r3, g3, b3))
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)
160 r, g, b = colorsys.hls_to_rgb(h, l, s)
161 r, g, b = [x*255.0 for x in (r, g, b)]
163 return rgb_to_hex((int(r), int(g), int(b)))
166 def rgb_to_yiq(color):
167 """Sort a list of colors."""
168 return colorsys.rgb_to_yiq(*hex_to_rgb(color))
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)
180 """Check if process is running by name."""
182 subprocess.check_output(["pidof", "-s", name])
183 except subprocess.CalledProcessError: