Last active
October 1, 2023 14:16
-
-
Save polkaulfield/26ce791732341eced59ec268e135f1bc to your computer and use it in GitHub Desktop.
recolor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys, re, os, shutil, colorsys | |
home_dir = os.path.expanduser('~') + "/" | |
icon_theme_dir = home_dir + ".local/share/icons/Adwaita-Colored-Folders/" | |
repo_name = "adwaita-icon-theme" | |
icon_repo = "https://gitlab.gnome.org/GNOME/" + repo_name | |
target_color = sys.argv[1] | |
theme_template = """[Icon Theme] | |
Name=Adwaita-Colored-Folders | |
Comment=Adwaita Colored Folders | |
Example=folder | |
Inherits=Adwaita,hicolor | |
# Directory list | |
Directories=scalable/places, | |
[scalable/places] | |
Context=Places | |
Size=128 | |
MinSize=8 | |
MaxSize=512 | |
Type=Scalable""" | |
def create_icon_theme(): | |
dest_path = icon_theme_dir + "scalable/places/" | |
if not os.path.exists(icon_theme_dir): | |
try: | |
os.system("git clone " + icon_repo + " /tmp/" + repo_name) | |
except: | |
pass | |
os.makedirs(dest_path, exist_ok=True) | |
for root, dirs, files in os.walk("/tmp/" + repo_name, topdown=True): | |
for name in files: | |
if (name.startswith("folder") or name.startswith("user")) and name.endswith("svg") and "symbolic" not in name: | |
filepath = os.path.join(root, name) | |
print("copying " + filepath + " " + dest_path + name) | |
shutil.copy(filepath, dest_path + name) | |
with open(icon_theme_dir + "index.theme","w+") as index_theme_file: | |
index_theme_file.write(theme_template) | |
return | |
def sanitize_input_hex(hex): | |
hex = input.str.lower() | |
if (input.startswith('#') and len(hex) == 7): | |
hex = input[1:] | |
if len(input) == 6: | |
return hex | |
def get_colors(content): | |
colors_hex = re.findall(r"#\b[0-9a-f]{6}\b", content) | |
for n in range(0, len(colors_hex)): | |
colors_hex[n] = colors_hex[n][1:] | |
return colors_hex | |
def create_color_dict(input_hex, content): | |
colors_dict = {} # original: changed | |
original_colors_hex = get_colors(content) | |
for color_hex in original_colors_hex: | |
colors_dict[color_hex] = make_new_color(input_hex, color_hex) | |
return colors_dict | |
def get_hls_from_hex(hex): | |
hex_arr = re.findall('..', hex) | |
hls = colorsys.rgb_to_hls(int(hex_arr[0], 16), int(hex_arr[1], 16), int(hex_arr[2], 16)) | |
return hls | |
def make_new_color(input_hex, original_hex): | |
new_hls = get_hls_from_hex(input_hex) | |
old_hls = get_hls_from_hex(original_hex) | |
rgb_tuple = colorsys.hls_to_rgb(new_hls[0], old_hls[1], old_hls[2]) | |
rgb_str = hex(int(rgb_tuple[0]))[2:] + hex(int(rgb_tuple[1]))[2:] + hex(int(rgb_tuple[2]))[2:] | |
return rgb_str | |
def change_icon_file_color(filepath, input_hex): | |
icon_file_content = "" | |
with open(filepath, 'r') as icon_file: | |
icon_file_content = icon_file.read() | |
color_dict = create_color_dict(input_hex, icon_file_content) | |
for old_value, new_value in color_dict.items(): | |
icon_file_content = icon_file_content.replace(old_value, new_value) | |
with open(filepath, 'w') as icon_file: | |
icon_file.write(icon_file_content) | |
def show_usage(): | |
print("Usage: recolor.py hex_color") | |
return | |
def main(): | |
if target_color != None: # TODO better validation | |
if not os.path.exists(icon_theme_dir): | |
create_icon_theme() | |
for root, dirs, files in os.walk(icon_theme_dir, topdown=True): | |
for file in files: | |
if file != "index.theme": | |
file_path = os.path.join(root, file) | |
print("Changing color for: " + file_path) | |
change_icon_file_color(file_path, target_color) | |
else: | |
show_usage() | |
sys.exit(0) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment