-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_colors.py
More file actions
90 lines (70 loc) · 3.62 KB
/
replace_colors.py
File metadata and controls
90 lines (70 loc) · 3.62 KB
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
import os
import re
import colorsys
input_directory = "/home/nobody/Documents/dialer"
output_directory = "/home/nobody/Documents"
#This script is for replacing hex color codes with alpha (ie. #ffffffff), the generated color codes will be in hex code too
print("Input directory:", input_directory)
print("Output directory:", output_directory)
print("Input directory contents:", os.listdir(input_directory))
def generate_target_colors(original_colors):
hues = [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330]
target_colors = []
invalid_colors = []
for color in original_colors:
# Ensure color has enough characters to extract RGB values
if len(color) == 9:
# Extract hue from original color
try:
hex_to_rgb = lambda h: (int(h[1:3], 16), int(h[3:5], 16), int(h[5:7], 16))
rgb_color = hex_to_rgb(color)
hsv_color = colorsys.rgb_to_hsv(*rgb_color)
hue = hsv_color[0] * 360
# Find the closest hue in the hues list
closest_hue_index = round((hue - min(hues)) / (max(hues) - min(hues)))
closest_hue = hues[closest_hue_index]
# Convert closest hue to RGB and generate target colo
rgb_color = colorsys.hsv_to_rgb(closest_hue / 360, hsv_color[1], hsv_color[2])
target_color = "#{:02x}{:02x}{:02x}".format(int(255 * rgb_color[0]), int(255 * rgb_color[1]), int(255 * rgb_color[2]))
# Skip invalid color values and store them in the invalid_colors list
target_colors.append(target_color[:9]) # Only keep the first 9 digits
except (ValueError, IndexError):
invalid_colors.append(color)
target_colors.append(color)
else:
# If color string is not 8-digit hex code, append the original color
target_colors.append(color)
return target_colors, invalid_colors
def replace_colors(file_path):
print("Processing file:", file_path)
print("Reading file content...")
with open(file_path, 'r') as f:
original_content = f.read()
original_colors = re.findall(r"#[0-9a-fA-F]{6,8}", original_content)
print("Found original colors:", original_colors)
# Generate target colors and get invalid colors
target_colors, invalid_colors = generate_target_colors(original_colors)
print("Generated target colors:", target_colors)
print("Invalid colors:", invalid_colors)
file_content = original_content
for original_color, target_color in zip(original_colors, target_colors):
file_content = file_content.replace(original_color[:9], target_color[:9])
print("Modified file content:\n", file_content)
# Save the modified XML files to the output_directory
file_name = os.path.basename(file_path) # Get the file name from the original path
output_file_path = os.path.join(output_directory, file_name) # Construct the output path
print("Saving modified file to:", output_file_path)
with open(output_file_path, 'w') as f:
f.write(file_content)
print("Modified file saved successfully.")
def process_directory(directory_path):
print("Processing directory:", directory_path)
for entry in os.scandir(directory_path):
if entry.is_file():
if entry.name == "colors.xml":
replace_colors(entry.path)
elif entry.is_dir(follow_symlinks=False):
process_directory(entry.path)
# Process colors.xml files in the input_directory and its subdirectories
print("Starting to process input directory...")
process_directory(input_directory)