diff options
| -rw-r--r-- | src/color_engine.py | 9 | ||||
| -rw-r--r-- | src/get_args.py | 17 | ||||
| -rw-r--r-- | src/instant_rice.py | 17 | ||||
| -rw-r--r-- | src/load_config.py | 7 | ||||
| -rw-r--r-- | src/manage_saves.py | 30 | ||||
| -rw-r--r-- | src/theme.py | 12 | ||||
| -rw-r--r-- | src/update_i3.py | 3 | ||||
| -rw-r--r-- | src/user_interface.py | 19 |
8 files changed, 81 insertions, 33 deletions
diff --git a/src/color_engine.py b/src/color_engine.py index 0e43475..bcb3ba2 100644 --- a/src/color_engine.py +++ b/src/color_engine.py @@ -26,9 +26,9 @@ def compColors(color_list: list) -> list: """ compliments = [] for color in color_list: - curr_hex = color[1:] # slice off the # from the hex code + curr_hex = color[1:] rgb = (curr_hex[0:2], curr_hex[2:4], curr_hex[4:6]) - comp = ['%02X' % (255 - int(a, 16)) for a in rgb] # magic :D + comp = ['%02X' % (255 - int(a, 16)) for a in rgb] compliments.append('#' + ''.join(comp)) return compliments @@ -54,7 +54,7 @@ def checkContrast(hex_color_list: list, hex_compliment_list: list) -> list: def relativeLuminance(color: list): - threshold = 0.03928 # this whole function is magic constants lol + threshold = 0.03928 channels = [] for channel in color: @@ -70,7 +70,6 @@ def relativeLuminance(color: list): return luminance - def rgbToHex(input_values: list) -> list: """ Takes in a list of RBG color values and returns a list of those same colors as hex values @@ -83,6 +82,7 @@ def rgbToHex(input_values: list) -> list: hex_list.append('#{:02x}{:02x}{:02x}'.format(red, green, blue)) return hex_list + def hexToRGB(hex_value: str) -> tuple: """ Takes in a list of Hex values and returns a tuple of those colors as rgb values @@ -90,6 +90,7 @@ def hexToRGB(hex_value: str) -> tuple: hex_value = hex_value.lstrip('#') return tuple(int(hex_value[i:i+2], 16) for i in (0, 2, 4)) # Magic :DDDDDD + def hexToRGB_list(hex_list: list) -> list: colors = [] for color in hex_list: diff --git a/src/get_args.py b/src/get_args.py index ebdf464..da6476e 100644 --- a/src/get_args.py +++ b/src/get_args.py @@ -7,15 +7,16 @@ def get_args(args, walls_dir) -> tuple: # arguments that can be passed into program initialize = False reconfigure = False - color_save = None + theme = None VALID_ARGS = ['-r', '-p', '--initialize', '--reconfigure'] + if '-p' in args: index = args.index('-p') - if os.path.isfile(args[index + 1]): - color_save = manage_saves.load_color_palette(args[index + 1]) - else: - print('invalid arguments. -p should be followed by a color palette save') - exit(2) + isFile = os.path.isfile(args[index + 1]) + if isFile == True: + print("reached") + theme = manage_saves.load_theme(args[index + 1]) + if '-r' in args: img_path = user_interface.pickRandomWallpaper(walls_dir) @@ -24,13 +25,13 @@ def get_args(args, walls_dir) -> tuple: if not os.path.exists(img_path) and\ args[1] not in VALID_ARGS: print(f'[bold red]ERROR: invalid image path {os.getcwd()}/{args[1]}') - exit(3) + exit(2) if '--initialize' in args: initialize = True if '--reconfigure' in args: reconfigure = True - return img_path, initialize, reconfigure, color_save + return img_path, initialize, reconfigure, theme def usage(args) -> None: print(f"""Instant Rice - An automatic theming utilitiy diff --git a/src/instant_rice.py b/src/instant_rice.py index e13f67b..453cc01 100644 --- a/src/instant_rice.py +++ b/src/instant_rice.py @@ -4,7 +4,6 @@ import user_interface import update_rofi import update_i3 import update_polybar -import initialize_i3 import initialize_rofi from get_args import get_args,usage from load_config import systemConfig @@ -14,17 +13,29 @@ if __name__ == '__main__': if len(sys.argv) > 1: config = systemConfig() - img_path, initialize, reconfigure, color_save = get_args(sys.argv, config.wallpaper_directory) + + img_path, initialize, reconfigure, theme = get_args(sys.argv, config.wallpaper_directory) + if initialize: initialize_rofi.reconfigureRofi() print('[bold green]Initialization Completed. Exiting...') exit(0) - hex_colors, hex_compliments = user_interface.colorPickerUI(img_path, config.num_palettes) + + if theme == None: + hex_colors, hex_compliments = user_interface.colorPickerUI(img_path, config.num_palettes) + user_interface.saveThemePrompt(hex_colors, hex_compliments, img_path, config.theme_directory) + else: + hex_colors = theme['colors'] + hex_compliments = theme['comp_colors'] + if config.polybar_config: update_polybar.updatePolybarTheme(config.polybar_config, hex_colors, hex_compliments) + if config.rofi_config: update_rofi.updateRofiTheme(config.rofi_config, hex_colors, hex_compliments) + if config.i3_config: update_i3.updatei3Theme(config.i3_config, img_path, hex_colors, hex_compliments, config.generate_i3_lock, config.use_dmenu, config.i3_lock_image) + else: usage(sys.argv) diff --git a/src/load_config.py b/src/load_config.py index dbc0d81..5768b3e 100644 --- a/src/load_config.py +++ b/src/load_config.py @@ -20,6 +20,7 @@ class systemConfig: num_palettes = 15 use_dmenu = False generate_i3_lock = False + theme_directory = "" def __init__(self): print('[bold green]Loading configuration files') @@ -36,6 +37,7 @@ class systemConfig: self.polybar_config = f'/home/{self.username}/.config/polybar/config.ini' if os.path.exists(f'/home/{self.username}/.config/rofi/config.rasi'): self.rofi_config = f'/home/{self.username}/.config/rofi/config.rasi' + if os.path.exists(f'/home/{self.username}/.config/instantrice/'): self.rice_config = f'/home/{self.username}/.config/instantrice/config.rice' self.get_user_preferences() @@ -80,3 +82,8 @@ class systemConfig: else: print(f'Invalid configuration parameter at line {i}:\n{line}.\nUsing default \ configuration of 15 palettes.') + if "theme_directory" in line: + match = line.strip().split(' ') + if not match[2].endswith('/'): + match[2] += '/' + self.theme_directory = match[2] diff --git a/src/manage_saves.py b/src/manage_saves.py index 988b9a2..f57f7b8 100644 --- a/src/manage_saves.py +++ b/src/manage_saves.py @@ -1,21 +1,21 @@ -import pickle from rich import print - -def save_color_palette(hex_colors, hex_compliments, save_directory): +from theme import Theme +import json +def save_theme(hex_colors, hex_compliments, wallpaper_location, save_location): + """ + Save a theme to the disk at a specified location as a json object. + """ - colors = [hex_colors, hex_compliments] + theme = Theme(hex_colors, hex_compliments, wallpaper_location) - with open(save_directory, 'wb') as file: - pickle.dump(colors, file, pickle.HIGHEST_PROTOCOL) - + with open(save_location, 'w') as file: + json.dump(theme.toDict(), file) print('[bold green]color palette saved successfully') -def load_color_palette(save_location): - with open(save_location, 'rb') as file: - try: - data = pickle.load(file) - except: - print('invalid arguments. -p should be followed by a color palette save') - exit(2) - +def load_theme(save_location): + """ + Load a theme from disk at a specified location + """ + with open(save_location, 'r') as file: + data = json.loads(file.read()) return data diff --git a/src/theme.py b/src/theme.py new file mode 100644 index 0000000..4bda7e1 --- /dev/null +++ b/src/theme.py @@ -0,0 +1,12 @@ + +class Theme: + def __init__(self, colors, comp_colors, wallpaper): + self.colors = colors + self.comp_colors = comp_colors + self.wallpaper = wallpaper + def toDict(self): + return { + "colors" : self.colors, + "comp_colors": self.comp_colors, + "wallpaper" : self.wallpaper + } diff --git a/src/update_i3.py b/src/update_i3.py index 1d4b792..f0282c8 100644 --- a/src/update_i3.py +++ b/src/update_i3.py @@ -51,11 +51,12 @@ def update_i3_colors( dmenu: bool, img_path: str ) -> None: + data = '' + with open(config_path, 'r') as file: data = file.readlines() - for i, line in enumerate(data): # update colors if "set $bgcolor" in line: diff --git a/src/user_interface.py b/src/user_interface.py index 934098c..f55c588 100644 --- a/src/user_interface.py +++ b/src/user_interface.py @@ -1,17 +1,20 @@ import os import random import color_engine +import manage_saves from rich import print def colorPickerUI(img_path: str, num_palettes: int) -> tuple: -#display the selected color scheme and ask user if they like it or want to generate a new color scheme + """ + display the selected color scheme and ask user if they like it + or want to generate a new color scheme + """ hex_colors, hex_compliments = selectPalette(img_path, num_palettes) final_colors, final_compliments = selectColorsFromPalette(hex_colors, hex_compliments) return final_colors, final_compliments def selectPalette(img_path: str, num_palettes: int) -> tuple: - # confirmed = False while not confirmed: print() @@ -86,3 +89,15 @@ def pickRandomWallpaper(walls_dir) -> str: confirmed = True return wallpaper + +def saveThemePrompt(hex_colors, hex_compliments, wallpaper_location, theme_location): + print('[bold green]Save configuration as preset? (y/n)') + do_save = input('>') + if do_save == 'y': + print('[bold green]enter theme name') + theme_name = input('>') + theme_path = theme_location + theme_name + ".theme" + + manage_saves.save_theme(hex_colors, hex_compliments, wallpaper_location, theme_path) + print(f'[bold green]Theme {theme_name} saved!') + |
