Filled color tweak #7, modified installation & colors JSON

This commit is contained in:
Vladyslav Hroshev
2023-07-21 21:56:19 +03:00
parent e3de2b2e75
commit 4d747e8892
37 changed files with 651 additions and 180 deletions

View File

@@ -15,11 +15,22 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import colorsys
import os
import json
import argparse
import textwrap
import colorsys # convert hsl to rgv
import os # system commands, working with files
import json # colors.json
import argparse # command-line options
import textwrap # example text in argparse
# folder definitions
temp_folder = "./.temp"
gnome_folder = "gnome-shell"
temp_gnome_folder = f"{temp_folder}/{gnome_folder}"
tweaks_folder = "./tweaks"
themes_folder = "~/.themes"
# files definitions
gnome_shell_css = f"{temp_gnome_folder}/gnome-shell.css"
def generate_file(folder, final_file):
@@ -38,20 +49,27 @@ def generate_file(folder, final_file):
def concatenate_files(file, edit_file):
"""
Merge two files
:param file: file you want to append
:param edit_file: where it will be appended
"""
open(edit_file, 'a').write('\n' + open(file).read())
def remove_files():
# Define the paths of the folders to be deleted
paths = ["~/.themes", "~/.local/share/themes"]
"""
Delete already installed Marble theme
"""
# Ask for confirmation before deleting
confirmation = input(f"Do you want to delete all \"Marble\" folders in {' and in '.join(paths)}? (y/N) ")
paths = (themes_folder, "~/.local/share/themes")
print("💡 You do not need to delete files if you want to update theme.\n")
confirmation = input(f"Do you want to delete all \"Marble\" folders in {' and in '.join(paths)}? (y/N) ").lower()
# If the user confirms, delete the folder
if confirmation == "y":
# Loop through the paths
for path in paths:
# Check if the path exists
@@ -60,13 +78,10 @@ def remove_files():
# Get the list of folders in the path
folders = os.listdir(os.path.expanduser(path))
# Loop through the folders
# toggle if folder has no marble theme
found_folder = False
# Loop through the folders
for folder in folders:
# Check if the folder starts with "Marble"
if folder.startswith("Marble"):
folder_path = os.path.join(os.path.expanduser(path), folder)
print(f"Deleting folder {folder_path}...", end='')
@@ -90,13 +105,13 @@ def remove_files():
def destination_return(path_name, theme_mode):
"""
Copied/modified gnome-shell theme location
Copied/modified theme location
:param path_name: color name
:param theme_mode: theme name (light or dark)
:return: copied files' folder location
"""
return f"~/.themes/Marble-{path_name}-{theme_mode}/gnome-shell"
return f"{themes_folder}/Marble-{path_name}-{theme_mode}/"
def copy_files(source, destination):
@@ -106,10 +121,10 @@ def copy_files(source, destination):
:param destination: where files will be pasted
"""
destination_dirs = destination.split("/")
destination_dirs = destination.split("/") # list of folders
loop_create_dirs = f"{destination_dirs[0]}/"
# create necessary folders
# traverse through folders and create them
for i in range(1, len(destination_dirs)):
loop_create_dirs += f"{destination_dirs[i]}/"
os.system(f"mkdir -p {loop_create_dirs}")
@@ -117,6 +132,27 @@ def copy_files(source, destination):
os.system(f"cp -aT {source} {destination}")
def replace_keywords(file, *args):
"""
Replace file with several keywords
:param file: file name where keywords must be replaced
:param args: (keyword, replacement), (...), ...
"""
# skip binary files in project
if not file.lower().endswith(('.css', '.scss', '.svg')):
return
with open(file, "r") as read_file:
content = read_file.read()
for keyword, replacement in args:
content = content.replace(keyword, replacement)
with open(file, "w") as write_file:
write_file.write(content)
def apply_colors(hue, destination, theme_mode, apply_file, sat=None):
"""
Install accent colors from colors.json to different file
@@ -127,30 +163,31 @@ def apply_colors(hue, destination, theme_mode, apply_file, sat=None):
:param sat: color saturation (optional)
"""
if (not apply_file.lower().endswith(('.css', '.svg'))):
return
# list of (keyword, replaced value)
replaced_colors = list()
with open(os.path.expanduser(f"{destination}/{apply_file}"), "r") as file:
edit_file = file.read()
# colorsys works in range(0, 1)
h = hue / 360
for element in colors["elements"]:
# if color is has default color and hasn't been replaced
if theme_mode not in colors["elements"][element] and colors["elements"][element]["default"]:
default_element = colors["elements"][element]["default"]
default_color = colors["elements"][default_element][theme_mode]
colors["elements"][element][theme_mode] = default_color
# colorsys works in range(0, 1)
h = hue / 360
for element in colors["elements"]:
# convert to range(0, 1)
lightness = int(colors["elements"][element][theme_mode]["l"]) / 100
saturation = int(colors["elements"][element][theme_mode]["s"]) / 100 if sat is None else int(
colors["elements"][element][theme_mode]["s"]) * (sat / 100) / 100
alpha = colors["elements"][element][theme_mode]["a"]
# convert sla to range(0, 1)
lightness = int(colors["elements"][element][theme_mode]["l"]) / 100
saturation = int(colors["elements"][element][theme_mode]["s"]) / 100 if sat is None else \
int(colors["elements"][element][theme_mode]["s"]) * (sat / 100) / 100
alpha = colors["elements"][element][theme_mode]["a"]
# convert hsl to rgb and multiple every item
red, green, blue = [int(item * 256) for item in colorsys.hls_to_rgb(h, lightness, saturation)]
# convert hsl to rgb and multiply every item
red, green, blue = [int(item * 256) for item in colorsys.hls_to_rgb(h, lightness, saturation)]
replace_keyword = colors["elements"][element]["replace"]
replaced_colors.append((element, f"rgba({red}, {green}, {blue}, {alpha})"))
edit_file = edit_file.replace(replace_keyword, f"rgba({red}, {green}, {blue}, {alpha})")
with open(os.path.expanduser(f"{destination}/{apply_file}"), "w") as file:
file.write(edit_file)
# replace colors
replace_keywords(os.path.expanduser(f"{destination}/{apply_file}"), *replaced_colors)
def apply_theme(hue, destination, theme_mode, sat=None):
@@ -162,7 +199,7 @@ def apply_theme(hue, destination, theme_mode, sat=None):
:param sat: color saturation (optional)
"""
for apply_file in os.listdir("./gnome-shell/"):
for apply_file in os.listdir(f"{temp_gnome_folder}/"):
apply_colors(hue, destination, theme_mode, apply_file, sat=sat)
@@ -179,9 +216,10 @@ def install_color(hue, name, theme_mode, sat=None):
try:
for mode in theme_mode:
copy_files("./gnome-shell", destination_return(name, mode))
destination = destination_return(name, mode)
apply_theme(hue, destination_return(name, mode), mode, sat=sat)
copy_files(temp_folder, destination)
apply_theme(hue, f"{destination}/{gnome_folder}", mode, sat=sat)
except Exception as err:
print("\nError: " + str(err))
@@ -199,7 +237,7 @@ def hex_to_rgba(hex_color):
try:
if len(hex_color) in range(6, 10):
hex_color = hex_color.lstrip('#') + "ff"
# is convertable
# if is convertable
int(hex_color[:], 16)
else:
raise ValueError
@@ -208,10 +246,14 @@ def hex_to_rgba(hex_color):
raise ValueError(f'Error: Invalid HEX color code: {hex_color}')
else:
return int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16), int(hex_color[6:8], 16) / 255
return int(hex_color[0:2], 16), \
int(hex_color[2:4], 16), \
int(hex_color[4:6], 16), \
int(hex_color[6:8], 16) / 255
def main():
# script description
parser = argparse.ArgumentParser(prog="python install.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''
@@ -228,7 +270,6 @@ def main():
# Default arguments
parser.add_argument('-r', '--remove', action='store_true', help='remove all "Marble" themes')
# Default accent colors
default_args = parser.add_argument_group('Install default theme')
default_args.add_argument('-a', '--all', action='store_true', help='all available accent colors')
default_args.add_argument('--red', action='store_true', help='red theme only')
@@ -239,43 +280,49 @@ def main():
default_args.add_argument('--yellow', action='store_true', help='yellow theme only')
default_args.add_argument('--gray', action='store_true', help='gray theme only')
# Custom accent colors
custom_args = parser.add_argument_group('Install custom color theme')
custom_args.add_argument('--hue', type=int, choices=range(0, 361), help='generate theme from Hue prompt',
metavar='(0 - 360)')
custom_args.add_argument('--name', nargs='?', help='theme name (optional)')
# Optional theme tweaks
color_styles = parser.add_argument_group("Theme color tweaks")
color_styles.add_argument("--filled", action="store_true", help="make accent color more vibrant")
color_tweaks = parser.add_argument_group('Optional theme tweaks')
color_tweaks.add_argument('--mode', choices=['light', 'dark'], help='select a specific theme mode to install')
color_tweaks.add_argument('--sat', type=int, choices=range(0, 251),
help='custom color saturation (<100%% - reduce, >100%% - increase)', metavar='(0 - 250)%')
# Panel tweaks
panel_args = parser.add_argument_group('Panel tweaks')
panel_args.add_argument('-Pds', '--panel_default_size', action='store_true', help='set default panel size')
panel_args.add_argument('-Pnp', '--panel_no_pill', action='store_true', help='remove panel button background')
panel_args.add_argument('-Ptc', '--panel_text_color', type=str, nargs='?', help='custom panel HEX(A) text color')
# Overview tweaks
overview_args = parser.add_argument_group('Overview tweaks')
overview_args.add_argument('--launchpad', action='store_true', help='change Show Apps icon to MacOS Launchpad icon')
args = parser.parse_args()
# is used as list because of install_color
mode = [args.mode] if args.mode else ['light', 'dark']
generate_file("./css/", gnome_shell_css)
# move files to temp folder
copy_files(f"./theme/{gnome_folder}/", f"{temp_gnome_folder}")
generate_file(f"./theme/{gnome_folder}_css/", gnome_shell_css)
# remove marble theme
if args.remove:
remove_files()
# panel tweaks
if args.panel_default_size:
concatenate_files("./tweaks/panel/def-size.css", gnome_shell_css)
concatenate_files(f"{tweaks_folder}/panel/def-size.css", gnome_shell_css)
if args.panel_no_pill:
concatenate_files("./tweaks/panel/no-pill.css", gnome_shell_css)
concatenate_files(f"{tweaks_folder}/panel/no-pill.css", gnome_shell_css)
if args.panel_text_color:
open('./gnome-shell/gnome-shell.css', 'a') \
open(f"{temp_gnome_folder}/{gnome_folder}.css", "a") \
.write(".panel-button,\
.clock,\
.clock-display StIcon {\
@@ -284,45 +331,52 @@ def main():
# dock tweaks
if args.launchpad:
concatenate_files("./tweaks/launchpad/launchpad.css", gnome_shell_css)
os.system("cp ./tweaks/launchpad/launchpad.png ./gnome-shell/")
concatenate_files(f"{tweaks_folder}/launchpad/launchpad.css", gnome_shell_css)
os.system(f"cp {tweaks_folder}/launchpad/launchpad.png {temp_gnome_folder}/")
# Process the arguments and perform the installation accordingly
if args.remove:
remove_files()
# color tweaks
if args.filled:
for apply_file in os.listdir(f"{temp_gnome_folder}/"):
replace_keywords(f"{temp_gnome_folder}/{apply_file}",
("BUTTON-COLOR", "ACCENT-FILLED-COLOR"),
("BUTTON_HOVER", "ACCENT-FILLED_HOVER"),
("BUTTON_INSENSITIVE", "ACCENT-FILLED_INSENSITIVE"),
("BUTTON-TEXT-COLOR", "TEXT-BLACK-COLOR"),
("BUTTON-TEXT_SECONDARY", "TEXT-BLACK_SECONDARY"))
elif args.all:
# what argument colors defined
if args.all:
# install hue colors listed in colors.json
for color in colors["colors"]:
hue = colors["colors"][color]["h"]
# if saturation already defined in color (gray)
sat = colors["colors"][color]["s"] if colors["colors"][color]["s"] is not None else args.sat
install_color(hue, color, mode, sat)
elif args.red or args.pink or args.purple or args.blue or args.green or args.yellow or args.gray:
for color_name in colors["colors"]:
if getattr(args, color_name):
hue = colors["colors"][color_name]["h"]
sat = colors["colors"][color_name]["s"] if colors["colors"][color_name]["s"] is not None else args.sat
for color in colors["colors"]:
if getattr(args, color): # if argument name is in defined colors
hue = colors["colors"][color]["h"]
# if saturation already defined in color (gray)
sat = colors["colors"][color]["s"] if colors["colors"][color]["s"] is not None else args.sat
install_color(hue, color_name, mode, sat)
install_color(hue, color, mode, sat)
# custom color
elif args.hue:
hue = args.hue
theme_name = args.name if args.name else f'hue{hue}'
theme_name = args.name if args.name else f'hue{hue}' # if defined name
install_color(hue, theme_name, mode, args.sat)
else:
print('No arguments specified. Use -h or --help to see the available options.')
print('No arguments or no color arguments specified. Use -h or --help to see the available options.')
if __name__ == "__main__":
gnome_shell_css = "./gnome-shell/gnome-shell.css"
colors = json.load(open("colors.json")) # used as database for replacing colors
main()
os.remove(gnome_shell_css)
if os.path.exists("./gnome-shell/launchpad.png"):
os.remove("./gnome-shell/launchpad.png")
os.system(f"rm -r {temp_folder}")