diff options
| author | Chandler J <cjustice2000@gmail.com> | 2024-04-06 20:52:13 -0600 |
|---|---|---|
| committer | Chandler J <cjustice2000@gmail.com> | 2024-04-06 20:52:44 -0600 |
| commit | b09d6a0a2d9887b5560a3b49cb8a603a33ec4828 (patch) | |
| tree | 0fbfeab50c95e6732ab69b1e3dae852fd80b0598 /src | |
| parent | b7dc1316ab0eb51f408b2b7782571d868ad3b864 (diff) | |
improved color picker
Diffstat (limited to 'src')
| -rw-r--r-- | src/color_engine.py | 2 | ||||
| -rw-r--r-- | src/get_args.py | 23 | ||||
| -rw-r--r-- | src/manage_saves.py | 2 | ||||
| -rw-r--r-- | src/user_interface.py | 40 |
4 files changed, 43 insertions, 24 deletions
diff --git a/src/color_engine.py b/src/color_engine.py index 0e43475..c24450b 100644 --- a/src/color_engine.py +++ b/src/color_engine.py @@ -13,8 +13,6 @@ def grabColors(img_path: str, num_colors: int) -> list: # scale image down by factor of 10 to decrease computation time dim = (int(len(img[0])/10), int(len(img)/10)) img = cv.resize(img, dim, interpolation= cv.INTER_AREA) - # TODO: implement KMeans clustering from scratch to - # improve program modularity clt = KMeans(n_clusters=num_colors, n_init='auto') clt.fit(img.reshape(-1, 3)) return clt.cluster_centers_ diff --git a/src/get_args.py b/src/get_args.py index ebdf464..45f41cf 100644 --- a/src/get_args.py +++ b/src/get_args.py @@ -2,20 +2,30 @@ import user_interface import manage_saves import os from rich import print + + def get_args(args, walls_dir) -> tuple: # arguments that can be passed into program + VALID_ARGS = ['-r', '-p', '--initialize', '--reconfigure'] + initialize = False reconfigure = False color_save = None - VALID_ARGS = ['-r', '-p', '--initialize', '--reconfigure'] + + # load saved palette 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) + try: + if os.path.isfile(args[index + 1]): + color_save = manage_saves.load_color_palette(args[index + 1]) + else: + print(f'[bold red] Error!! invalid argument {args[index + 1]}. -p should be followed by a color palette save') + exit(2) + except IndexError: + print('[bold red]Error!! -p should be followed by location of a color palette save') + usage(args) + exit(2) if '-r' in args: img_path = user_interface.pickRandomWallpaper(walls_dir) @@ -32,6 +42,7 @@ def get_args(args, walls_dir) -> tuple: reconfigure = True return img_path, initialize, reconfigure, color_save + def usage(args) -> None: print(f"""Instant Rice - An automatic theming utilitiy diff --git a/src/manage_saves.py b/src/manage_saves.py index 988b9a2..1ada559 100644 --- a/src/manage_saves.py +++ b/src/manage_saves.py @@ -3,7 +3,7 @@ from rich import print def save_color_palette(hex_colors, hex_compliments, save_directory): - colors = [hex_colors, hex_compliments] + colors = (hex_colors, hex_compliments) with open(save_directory, 'wb') as file: pickle.dump(colors, file, pickle.HIGHEST_PROTOCOL) diff --git a/src/user_interface.py b/src/user_interface.py index 934098c..4162d1e 100644 --- a/src/user_interface.py +++ b/src/user_interface.py @@ -4,32 +4,42 @@ import color_engine from rich import print -def colorPickerUI(img_path: str, num_palettes: int) -> tuple: +def colorPickerUI(img_path: str, num_palettes: int, saved_colors = None) -> tuple: #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) + if saved_colors == None: + hex_colors, hex_compliments = selectPalette(img_path, num_palettes) + else: + hex_colors, hex_compliments = saved_colors + print(f'[bold green] colors successfully loaded from file') final_colors, final_compliments = selectColorsFromPalette(hex_colors, hex_compliments) return final_colors, final_compliments +def displayColors(hex_colors: list, hex_compliments: list): + constrast_levels = color_engine.checkContrast(hex_colors, hex_compliments) + main_colors = '' + complimentary_colors = '' + + for color in hex_colors: + main_colors += f'[on {color}] [/on {color}]' + print(main_colors) + for color in hex_compliments: + complimentary_colors += f'[on {color}] [/on {color}]' + print(complimentary_colors, '\n') + for i in range(len(hex_colors)): + print(f'[{hex_compliments[i]} on {hex_colors[i]}]\tGenerated Color Scheme\t\t ({i: 3})', f'contrast: {constrast_levels[i]:.2f}') + + def selectPalette(img_path: str, num_palettes: int) -> tuple: - # + confirmed = False + hex_colors = None + hex_compliments = None while not confirmed: print() popularColors = color_engine.grabColors(img_path, num_palettes) hex_colors = color_engine.rgbToHex(popularColors) hex_compliments = color_engine.compColors(hex_colors) - constrast_levels = color_engine.checkContrast(hex_colors, hex_compliments) - main_colors = '' - complimentary_colors = '' - - for color in hex_colors: - main_colors += f'[on {color}] [/on {color}]' - print(main_colors) - for color in hex_compliments: - complimentary_colors += f'[on {color}] [/on {color}]' - print(complimentary_colors, '\n') - for i in range(len(hex_colors)): - print(f'[{hex_compliments[i]} on {hex_colors[i]}]\tGenerated Color Scheme\t\t ({i: 3})', f'contrast: {constrast_levels[i]:.2f}') + displayColors(hex_colors, hex_compliments) print('[bold](a)ccept palette (g)enerate new palette') response = input('> ') if response == 'a': |
