Skip to content

Commit

Permalink
Merge pull request #81 from luisbocanegra/dev
Browse files Browse the repository at this point in the history
Add custom color option
  • Loading branch information
luisbocanegra authored Sep 15, 2022
2 parents 78b8f35 + 7638688 commit db50e39
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 40 deletions.
8 changes: 7 additions & 1 deletion src/kde-material-you-colors
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ if __name__ == '__main__':
help='ToolBar opacity, needs Lightly Application Style (value from 0 to 100, default is None)', default=None)
parser.add_argument('--konsole-opacity', '-ko', type=int,
help='Konsole background opacity (value from 0 to 100, default is None)', default=None)
parser.add_argument('--color', '-col', type=str,
help='Custom color (hex or rgb) used to generate M3 color scheme', default=None)

# Get commandline arguments
args = parser.parse_args()
Expand Down Expand Up @@ -75,7 +77,11 @@ if __name__ == '__main__':
config_watcher.set_value(config.options)
# Get wallpaper
wallpaper_watcher.set_value(
utils.currentWallpaper(config_watcher.get_new_value()))
utils.get_wallpaper_data(
plugin=config_watcher.get_new_value()['plugin'],
monitor=config_watcher.get_new_value()['monitor'],
color=config_watcher.get_new_value()['color']
))

if wallpaper_watcher.get_new_value() != None:
# Get light/dark scheme status
Expand Down
4 changes: 4 additions & 0 deletions src/sample_config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ plugin = org.kde.image
# Commented by default
#file = /tmp/000_eDP-1_current_wallpaper

# Custom color used to generate M3 color scheme (Takes precedence over the plugin, monitor and file options)
# Accepted values are hex (e.g #ff0000) and rgb (e.g 255,0,0) colors (rgb is converted to hex)
#color = 255,0,1

# Enable Light mode
# Accepted values are True or False
# Default is False
Expand Down
99 changes: 60 additions & 39 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from schemeconfigs import ThemeConfig
import signal
from logging.handlers import RotatingFileHandler
Expand Down Expand Up @@ -141,20 +142,21 @@ def __init__(self, args):
config.read(USER_CONFIG_PATH+CONFIG_FILE)
if 'CUSTOM' in config:
custom = config['CUSTOM']
c_light = custom.getboolean('light')
c_light = custom.getboolean('light', False)
c_file = custom.get('file')

c_monitor = custom.getint('monitor')
c_monitor = custom.getint('monitor', 0)
if c_monitor < 0:
raise ValueError(
'Value for monitor must be a positive integer')

c_ncolor = custom.getint('ncolor')
c_ncolor = custom.getint('ncolor', 0)
if c_ncolor < 0:
raise ValueError(
'Value for ncolor must be a positive integer')

c_plugin = custom.get('plugin')
c_color = custom.get('color')
c_iconslight = custom.get('iconslight')
c_iconsdark = custom.get('iconsdark')
c_pywal = custom.getboolean('pywal')
Expand All @@ -179,17 +181,18 @@ def __init__(self, args):
'sierra_breeze_buttons_color')
c_konsole_profile = custom.get('konsole_profile')

c_titlebar_opacity = custom.getint('titlebar_opacity')
c_titlebar_opacity = custom.getint('titlebar_opacity', 100)
if c_titlebar_opacity < 0 or c_titlebar_opacity > 100:
raise ValueError(
'Value for titlebar_opacity must be an integer betwritten 0 and 100, using default 100')

c_toolbar_opacity = custom.getint('toolbar_opacity')
c_toolbar_opacity = custom.getint('toolbar_opacity', 100)
if c_toolbar_opacity < 0 or c_toolbar_opacity > 100:
raise ValueError(
'Value for toolbar_opacity must be an integer betwritten 0 and 100, using default 100')

c_konsole_opacity = custom.getint('konsole_opacity')
c_konsole_opacity = custom.getint('konsole_opacity', 100)

if c_konsole_opacity < 0 or c_konsole_opacity > 100:
raise ValueError(
'Value for konsole_opacity must be an integer betwritten 0 and 100, using default 100')
Expand Down Expand Up @@ -223,56 +226,41 @@ def __init__(self, args):

if args.file != None:
c_file = args.file
elif c_file == None:
c_file = args.file

if args.monitor != None:
if args.monitor < 0:
logging.error('Value for --monitor must be a positive integer')
raise ValueError
else:
c_monitor = args.monitor
elif args.monitor == None and c_monitor == None:
c_monitor = 0

if args.ncolor != None:
if args.ncolor < 0:
logging.error('Value for --ncolor must be a positive integer')
raise ValueError
else:
c_ncolor = args.ncolor
elif args.ncolor == None and c_ncolor == None:
c_ncolor = 0

if args.plugin != None:
c_plugin = args.plugin
elif args.plugin == None and c_plugin == None:
c_plugin = DEFAULT_PLUGIN

if args.color != None:
c_color = args.color

if args.iconslight != None:
c_iconslight = args.iconslight
elif c_iconslight == None:
c_iconslight = args.iconslight

if args.iconsdark != None:
c_iconsdark = args.iconsdark
elif c_iconsdark == None:
c_iconsdark = args.iconsdark

if args.on_change_hook != None:
c_on_change_hook = args.on_change_hook
elif c_on_change_hook == None:
c_on_change_hook = args.on_change_hook

if args.sierra_breeze_buttons_color == True:
c_sierra_breeze_buttons_color = args.sierra_breeze_buttons_color
elif c_sierra_breeze_buttons_color != None:
c_sierra_breeze_buttons_color = c_sierra_breeze_buttons_color

if args.konsole_profile != None:
c_konsole_profile = args.konsole_profile
elif c_konsole_profile == None:
c_konsole_profile = args.konsole_profile

if args.titlebar_opacity != None:
if args.titlebar_opacity < 0 or args.titlebar_opacity > 100:
Expand All @@ -281,8 +269,6 @@ def __init__(self, args):
raise ValueError
else:
c_titlebar_opacity = args.titlebar_opacity
elif args.titlebar_opacity == None and c_titlebar_opacity == None:
c_titlebar_opacity = None

if args.toolbar_opacity != None:
if args.toolbar_opacity < 0 or args.toolbar_opacity > 100:
Expand All @@ -291,8 +277,6 @@ def __init__(self, args):
raise ValueError
else:
c_toolbar_opacity = args.toolbar_opacity
elif args.toolbar_opacity == None and c_toolbar_opacity == None:
c_toolbar_opacity = None

if args.konsole_opacity != None:
if args.konsole_opacity < 0 or args.konsole_opacity > 100:
Expand Down Expand Up @@ -321,15 +305,16 @@ def __init__(self, args):
"konsole_profile": c_konsole_profile,
"titlebar_opacity": c_titlebar_opacity,
"toolbar_opacity": c_toolbar_opacity,
"konsole_opacity": c_konsole_opacity
"konsole_opacity": c_konsole_opacity,
"color": c_color
}

@property
def options(self):
return self._options


def get_wallpaper_data(plugin=DEFAULT_PLUGIN, monitor=0, file=None):
def get_wallpaper_data(plugin=DEFAULT_PLUGIN, monitor=0, file=None, color=None):
"""Get current wallpaper or color from text file or plugin + containment combo
and return a string with its type (color or image file)
Expand All @@ -341,6 +326,8 @@ def get_wallpaper_data(plugin=DEFAULT_PLUGIN, monitor=0, file=None):
Returns:
tuple: (type (int), data (str))
"""
if plugin == None:
plugin = DEFAULT_PLUGIN
if file:
if os.path.exists(file):
with open(file) as file:
Expand All @@ -352,6 +339,11 @@ def get_wallpaper_data(plugin=DEFAULT_PLUGIN, monitor=0, file=None):
else:
logging.error(f'File "{file}" does not exist')
return None
elif color:
if validate_color(color):
return ("color", color)
else:
logging.error(f'Error: Color format "{color}" is incorrect')
else:
# special case for picture of the day plugin that requires a
# directory, provider and a category
Expand Down Expand Up @@ -585,12 +577,13 @@ def get_color_schemes(wallpaper, ncolor=None):

elif wallpaper_type == "color":
source_type = "color"
wallpaper_data = color2hex(wallpaper_data)
materialYouColors = get_material_you_colors(
wallpaper_data, ncolor=ncolor, source_type=source_type)

if materialYouColors != None:
try:
if wallpaper_type != "color":
if len(materialYouColors['bestColors']) > 1:
best_colors = f'Best colors:'

for index, col in materialYouColors['bestColors'].items():
Expand Down Expand Up @@ -646,10 +639,6 @@ def set_icons(icons_light, icons_dark, light=False):
logging.info(f'{icons} {changeicons_error}')


def currentWallpaper(options):
return get_wallpaper_data(plugin=options['plugin'], monitor=options['monitor'], file=options['file'])


def make_plasma_scheme(schemes=None):
# Make sure the schemes path exists
if not os.path.exists(USER_SCHEMES_PATH):
Expand Down Expand Up @@ -1296,10 +1285,12 @@ def apply_themes(
if wallpaper_watcher.has_changed or group1_watcher.has_changed or wallpaper_modified.has_changed:
if wallpaper_watcher.has_changed:
logging.info(
f'Wallpaper changed: ({wallpaper_new_type}) {wallpaper_new_data}')
material_colors.set_value(get_color_schemes(
wallpaper_watcher.get_new_value(),
config_watcher.get_new_value()['ncolor']))
f'Source changed: ({wallpaper_new_type}) {wallpaper_new_data}')
material_colors.set_value(
get_color_schemes(
wallpaper_watcher.get_new_value(),
config_watcher.get_new_value()['ncolor'])
)
if material_colors.get_new_value() != None:
# Genrate color schemes from MYou colors
schemes_watcher.set_value(ThemeConfig(
Expand Down Expand Up @@ -1472,3 +1463,33 @@ def apply_themes(
kwin_reload()
needs_kwin_reload == False
first_run_watcher.set_value(False)


def validate_color(color):
"""check if a color is either a valid hex or rgb format
Args:
color (str): Hex or rgb color
Returns:
int: color type rgb(1) or hex(2)
None: for invalid color
"""
is_hex = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', color)
is_rgb = re.search(
r'^(?:(?:^|,\s*)([01]?\d\d?|2[0-4]\d|25[0-5])){3}$', color)
if is_rgb:
return 1
elif is_hex:
return 2
else:
return None


def color2hex(color):
format = validate_color(color)
if format == 1:
r, g, b = [int(c) for c in color.split(",")]
return rgb2hex(r, g, b)
elif format == 2:
return color

0 comments on commit db50e39

Please sign in to comment.