summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChandler J <cjustice2000@gmail.com>2024-04-12 20:54:00 -0600
committerGitHub <noreply@github.com>2024-04-12 20:54:00 -0600
commit8a1903bb16d9e04795c33f3eb226486d06e8a642 (patch)
treee8fc9dc0bb690cc1976a47fe1166f4bafcafcb40 /src
parent95e73b141ca8f3618701ff59658ef519353cc7c0 (diff)
parent2564189a769a5bd3de63086bb62dc3532d1b07b9 (diff)
Merge branch 'main' into saves
Diffstat (limited to 'src')
-rw-r--r--src/color_engine.py19
-rw-r--r--src/get_args.py7
-rw-r--r--src/instant_rice.py4
-rw-r--r--src/load_config.py46
-rw-r--r--src/manage_saves.py2
-rw-r--r--src/update_i3.py10
-rw-r--r--src/user_interface.py39
7 files changed, 83 insertions, 44 deletions
diff --git a/src/color_engine.py b/src/color_engine.py
index bcb3ba2..504e84b 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_
@@ -52,8 +50,17 @@ def checkContrast(hex_color_list: list, hex_compliment_list: list) -> list:
return contrast_values
-def relativeLuminance(color: list):
-
+
+def relativeLuminance(color: list) -> float:
+ """
+ Determines the luminance of color. The luminance allows us to
+ compute the contrast between two colors by comparing their
+ relative luminance.
+
+ The magic constants in this function are all taken from the following
+ description of relative luminance:
+ https://www.w3.org/TR/WCAG20/#relativeluminancedef
+ """
threshold = 0.03928
channels = []
@@ -88,13 +95,13 @@ def hexToRGB(hex_value: str) -> tuple:
Takes in a list of Hex values and returns a tuple of those colors as rgb values
"""
hex_value = hex_value.lstrip('#')
- return tuple(int(hex_value[i:i+2], 16) for i in (0, 2, 4)) # Magic :DDDDDD
+ return tuple(int(hex_value[i:i+2], 16) for i in (0, 2, 4))
def hexToRGB_list(hex_list: list) -> list:
colors = []
for color in hex_list:
hex_value = color.lstrip('#')
- colors.append(tuple(int(hex_value[i:i+2], 16) for i in (0, 2, 4))) # Magic :DDDDDD
+ colors.append(tuple(int(hex_value[i:i+2], 16) for i in (0, 2, 4)))
return colors
diff --git a/src/get_args.py b/src/get_args.py
index da6476e..f878e77 100644
--- a/src/get_args.py
+++ b/src/get_args.py
@@ -2,11 +2,16 @@ 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
+
theme = None
VALID_ARGS = ['-r', '-p', '--initialize', '--reconfigure']
@@ -16,7 +21,6 @@ def get_args(args, walls_dir) -> tuple:
if isFile == True:
print("reached")
theme = manage_saves.load_theme(args[index + 1])
-
if '-r' in args:
img_path = user_interface.pickRandomWallpaper(walls_dir)
@@ -33,6 +37,7 @@ def get_args(args, walls_dir) -> tuple:
reconfigure = True
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 453cc01..4102fa1 100644
--- a/src/instant_rice.py
+++ b/src/instant_rice.py
@@ -35,7 +35,7 @@ if __name__ == '__main__':
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)
-
+ update_i3.updatei3Theme(config.i3_config, img_path, hex_colors, hex_compliments, config.generate_i3_lock, config.use_dmenu, config.i3_lock_image, config.menu_keybind)
+
else:
usage(sys.argv)
diff --git a/src/load_config.py b/src/load_config.py
index 5768b3e..5fe99b9 100644
--- a/src/load_config.py
+++ b/src/load_config.py
@@ -9,20 +9,22 @@ class systemConfig:
file for instant rice.
"""
- i3_config = ""
- i3_lock_image = ""
- polybar_config = ""
- wallpaper_directory = ""
- rofi_config = ""
- username = ""
- rice_config = ""
- #instant rice configuration settings
- num_palettes = 15
- use_dmenu = False
- generate_i3_lock = False
- theme_directory = ""
-
+
def __init__(self):
+ self.i3_config = ""
+ self.i3_lock_image = ""
+ self.polybar_config = ""
+ self.wallpaper_directory = ""
+ self.rofi_config = ""
+ self.username = ""
+ self.rice_config = ""
+ #instant rice configuration settings
+ self.num_palettes = 15
+ self.use_dmenu = False
+ self.generate_i3_lock = False
+ self.menu_keybind = ""
+ self.theme_directory = ""
+
print('[bold green]Loading configuration files')
#Grab username to find configuration files
@@ -66,15 +68,21 @@ class systemConfig:
for i, line in enumerate(data):
if "use_dmenu" in line:
match = line.split(' ')
- self.use_dmenu = True if match[2] == 'True' else False
+ self.use_dmenu = True if match[2].lower() == 'true' else False
+
if "generate_i3_lock" in line:
match = line.split(' ')
- self.generate_i3_lock = True if match[2] == 'True' else False
+ self.generate_i3_lock = True if match[2].lower() == 'true' else False
+
if "wallpaper_directory" in line:
match = line.strip().split(' ')
if not match[2].endswith('/'):
match[2] += '/'
- self.wallpaper_directory = match[2]
+ if os.path.exists(match[2]):
+ self.wallpaper_directory = match[2]
+ else:
+ print(f'Invalid configuration parameter at line {i}:\n{line}Directory does not exist; Please fix error before rerunning.')
+
if "num_palettes" in line:
match = line.strip().split(' ')
if match[2].isdigit():
@@ -87,3 +95,9 @@ class systemConfig:
if not match[2].endswith('/'):
match[2] += '/'
self.theme_directory = match[2]
+ print(f'Invalid configuration parameter at line {i}:\n{line}Using default configuration of 15 palettes.')
+
+ if "menu_keybind" in line:
+ match = line.strip().split(' ')
+ self.menu_keybind = match[2]
+ print(self.menu_keybind)
diff --git a/src/manage_saves.py b/src/manage_saves.py
index f57f7b8..44ce2a3 100644
--- a/src/manage_saves.py
+++ b/src/manage_saves.py
@@ -6,8 +6,10 @@ 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.
"""
+
theme = Theme(hex_colors, hex_compliments, wallpaper_location)
+
with open(save_location, 'w') as file:
json.dump(theme.toDict(), file)
print('[bold green]color palette saved successfully')
diff --git a/src/update_i3.py b/src/update_i3.py
index f0282c8..b06b47e 100644
--- a/src/update_i3.py
+++ b/src/update_i3.py
@@ -12,12 +12,13 @@ def updatei3Theme(
update_i3_lock: bool,
dmenu: bool,
lock_img_path: str,
+ menu_keybind: str,
) -> None:
print('[bold red]Updating i3 color scheme')
- update_i3_colors(config_path, colors, compliments, dmenu, img_path)
+ update_i3_colors(config_path, colors, compliments, dmenu, img_path, menu_keybind)
if update_i3_lock:
change_i3_lock_img(img_path, lock_img_path)
@@ -49,7 +50,8 @@ def update_i3_colors(
colors: list,
compliments: list,
dmenu: bool,
- img_path: str
+ img_path: str,
+ menu_keybind: str,
) -> None:
data = ''
@@ -73,10 +75,10 @@ def update_i3_colors(
if "set $bgimage" in line:
data[i] = 'set $bgimage ' + img_path + '\n'
# update i3 lock image
- if "bindsym $mod+d exec --no-startup-id dmenu_run" in line:
+ if f"bindsym {menu_keybind} exec --no-startup-id dmenu_run" in line:
if dmenu:
print('[bold red]Updating Dmenu color scheme')
- data[i] = f"bindsym $mod+d exec --no-startup-id dmenu_run -nb '{colors[0]}' -sf '{compliments[0]}' -sb '{colors[1]}' -nf '{compliments[1]}'\n"
+ data[i] = f"bindsym {menu_keybind} exec --no-startup-id dmenu_run -nb '{colors[0]}' -sf '{compliments[0]}' -sb '{colors[1]}' -nf '{compliments[1]}'\n"
with open(config_path, 'w') as file:
diff --git a/src/user_interface.py b/src/user_interface.py
index f55c588..bcc5ceb 100644
--- a/src/user_interface.py
+++ b/src/user_interface.py
@@ -5,6 +5,7 @@ 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
@@ -14,25 +15,31 @@ def colorPickerUI(img_path: str, num_palettes: int) -> tuple:
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':
@@ -43,7 +50,8 @@ def selectPalette(img_path: str, num_palettes: int) -> tuple:
return hex_colors, hex_compliments
-def selectColorsFromPalette(hex_colors, hex_compliments):
+def selectColorsFromPalette(hex_colors: list, hex_compliments: list) -> tuple:
+ selectedColors = []
selected = False
while not selected:
print('[bold blue]Select top 3 colors from list in order Primary, Secondary, Accent (IE, "4 10 6")')
@@ -55,7 +63,7 @@ def selectColorsFromPalette(hex_colors, hex_compliments):
else:
print('[bold red]Invalid selection. Use positive integers corresponding to color pair to select.')
continue
-
+
selectedColors = [int(i) for i in selectedColors]
final_colors = []
final_compliments = []
@@ -66,10 +74,11 @@ def selectColorsFromPalette(hex_colors, hex_compliments):
return final_colors, final_compliments
-def pickRandomWallpaper(walls_dir) -> str:
+def pickRandomWallpaper(walls_dir: str) -> str:
confirmed = False
history = []
num_wallpapers = len(os.listdir(walls_dir))
+ wallpaper = ''
while not confirmed:
if len(history) == num_wallpapers:
print('[bold blue] Wallpapers exhausted. Resetting history...')