From 51f6143a1bd4b389f8a77eae6d4e815925cfe8e8 Mon Sep 17 00:00:00 2001 From: Daniel Napora Date: Sat, 9 Sep 2023 09:18:19 +0200 Subject: [PATCH] GTK colors added to color submenu --- bin/colorizer-conky | 2 +- bin/colorizer-menus | 4 +- bin/gtkcolors.py | 119 ++++++++++++++++++++++++++++++++++++++++++++ bin/mbclr | 58 ++++++++++----------- 4 files changed, 151 insertions(+), 32 deletions(-) create mode 100755 bin/gtkcolors.py diff --git a/bin/colorizer-conky b/bin/colorizer-conky index 79a46ac..6c8d475 100755 --- a/bin/colorizer-conky +++ b/bin/colorizer-conky @@ -268,7 +268,7 @@ if pgrep -u $USER -af "conky.*mbcolor" >/dev/null; then fi if [[ ${#notstarted[@]} > 0 ]];then out+=("^sep($MORECONKIES)") - if [[ "$ALLSUPP" < 15 ]];then + if [[ "$ALLSUPP" < 12 ]];then out+=("^sep($CLICKTOSTART)") for c in ${notstarted[@]};do filename=${c##*/} diff --git a/bin/colorizer-menus b/bin/colorizer-menus index 4aa07dc..3fe1517 100755 --- a/bin/colorizer-menus +++ b/bin/colorizer-menus @@ -333,8 +333,8 @@ out2+=("$TIGHT,jgctl tweak sizing tight;$me") -#### COLOR SCHEMES -out+=(" $SCHEMES,^checkout(schemes)") +#### COLOR SCHEMES  +out+=(" $SCHEMES,^checkout(schemes)") out2+=("^tag(schemes)") out2+=("^sep($SCHEMES)") out2+=("^sep($SYSTEM_SCHEMES)") diff --git a/bin/gtkcolors.py b/bin/gtkcolors.py new file mode 100755 index 0000000..955873b --- /dev/null +++ b/bin/gtkcolors.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python3 + +""" +Based on labwc-gtktheme.py: +Create labwc theme based on the current Gtk theme + +SPDX-License-Identifier: GPL-2.0-only + +Copyright (C) @Misko_2083 2019 +Copyright (C) Johan Malm 2019-2022 +""" + +import os +import errno +from tokenize import tokenize, NUMBER, NAME, OP +from io import BytesIO +import gi +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk + +def parse(tokens): + """ + Parse css color expression token list and return red/green/blue values + Valid name-tokens include 'rgb' and 'rgba', whereas 'alpha', 'shade' and + 'mix' are ignored. @other-color references are still to be implemented. + """ + nr_colors_to_parse = 0 + in_label = False + color = [] + for toknum, tokval, _, _, _ in tokens: + if '@' in tokval: + in_label = True + continue + if toknum == NAME and in_label: + color.clear() + color.append(f"@{tokval}") + return color + if nr_colors_to_parse > 0: + if toknum == OP and tokval in ')': + print("warn: still parsing numbers; did not expect ')'") + if toknum == NUMBER: + color.append(tokval) + nr_colors_to_parse -= 1 + continue + if toknum == NAME and tokval in 'rgb': + nr_colors_to_parse = 3 + elif toknum == NAME and tokval in 'rgba': + nr_colors_to_parse = 4 + return color + +def color_hex(color): + """ return rrggbb color hex from list [r, g, b,...] """ + if not color: + return "None" + elif len(color) < 3: + return f"{color[0]}" + return '{:02x}{:02x}{:02x}'.format(*(int(x) for x in color[:3])) + +def hex_from_expr(line): + """ parse color expression to return hex style rrggbb """ + tokens = tokenize(BytesIO(line.encode('utf-8')).readline) + color = parse(tokens) + return color_hex(color) + +def parse_section(lines, name): + theme = {} + inside = False + for line in lines: + if f"{name} {{" in line: + inside = True + continue + if inside: + if "}" in line or "{" in line: + inside = False + break + if 'color' not in line: + continue + key, value = line.strip().split(":", maxsplit=1) + theme[f'{name}.{key.replace(" ", "")}'] = hex_from_expr(value) + return theme + +def resolve_labels(theme): + for key, label in theme.items(): + if '@' in label: + for tmp, value in theme.items(): + if tmp == label[1:]: + theme[key] = value + return resolve_labels(theme) + return theme + +def main(): + """ main """ + + gset = Gtk.Settings.get_default() + themename = gset.get_property("gtk-theme-name") + css = Gtk.CssProvider.get_named(themename).to_string() + + lines = css.split("\n") + theme = {} + + # Parse @define-color lines using syntax "@define-color " + for line in lines: + if "@define-color" not in line: + continue + x = line.split(" ", maxsplit=2) + theme[x[1]] = hex_from_expr(x[2]) + + # Add the color definitions in the headerbar{} and menu{} sections + theme |= parse_section(lines, "headerbar") + theme |= parse_section(lines, "menu") + + theme = resolve_labels(theme) + + for key, value in theme.items(): + print(f"{key}: {value}") + return + +if __name__ == '__main__': + main() diff --git a/bin/mbclr b/bin/mbclr index dd45978..d5f585e 100755 --- a/bin/mbclr +++ b/bin/mbclr @@ -21,7 +21,6 @@ #notify-send.sh "mbclr" "${1}\n${2}\n${3}\n${4}\n${5}\n${6}" case "$LANG" in pl*) - SELECT_COLOR="Wybierz kolor" PICK_FROM_SCREEN="Pobierz kolor z ekranu" SELECT_OR_PICK="Wybierz kolor z selektora" OPACITY="Nieprzezroczystość" @@ -30,29 +29,30 @@ case "$LANG" in LIGHTEN_DARKEN="Jaśniejsze / Ciemniejsze" SATURATION="Nasycenie..." ROTATE_HSL_HUE="Obróć kanał barwy HSL..." + CHANGE_COLOR="Zmień na..." CURRENT="obecny kolor" - WALLPAPER_COLORS="Kolory z tapety" + WALLPAPER_COLORS="...kolory z tapety" + GTKCOLORS="Kolory motywu GTK" ACCENT_COLORS="Popularne kolory akcentu" - LIGHTCOLORS="Jasne kolory..." - DARKCOLORS="Ciemne kolory..." + GRAYS="Odcienie szarości" ;; *) - SELECT_COLOR="Select color" PICK_FROM_SCREEN="Pick color from screen" SELECT_OR_PICK="Select or pick from screen" OPACITY="Opacity" - SHADES_OF_CURRENT="Calculate from current" + SHADES_OF_CURRENT="Calculate from" SHADES_OF="Shades of" LIGHTEN_DARKEN="Lighten / Darken" SATURATION="Saturation..." ROTATE_HSL_HUE="Rotate HSL hue channel..." + CHANGE_COLOR="Change to..." CURRENT="current color" - WALLPAPER_COLORS="Wallpaper colors" + WALLPAPER_COLORS="...wallpaper colors" + GTKCOLORS="GTK theme colors" ACCENT_COLORS="Popular accent colors" - LIGHTCOLORS="Light colors..." - DARKCOLORS="Dark colors..." - ;; + GRAYS="Shades of Gray" + ;; esac @@ -79,8 +79,7 @@ wallcolors+=("#2e3440" "#4c566a" "#5e81ac" "#bf616b" "#b48ead" "#a3be8c" "#ebcb8 fi -dark=("#000000" "#111111" "#222222" "#333333" "#444444" "#555555" "#666666") -light=("#FFFFFF" "#EEEEEE" "#DDDDDD" "#CCCCCC" "#BBBBBB" "#AAAAAA" "#999999") +grays=("#000000" "#0d0d0d" "#1b1b1b" "#282828" "#363636" "#434343" "#515151" "#5e5e5e" "#6b6b6b" "#797979" "#868686" "#949494" "#a1a1a1" "#aeaeae" "#bcbcbc" "#c9c9c9" "#d7d7d7" "#e4e4e4" "#f2f2f2" "#FFFFFF") accents=("#e93d57" "#c7162b" "#e9633a" "#e95620" "#f4a300" "#e8cc2d" "#3cd425" "#0e8420" "#00d3b7" "#18b0b0" "#3dade9" "#686b6f" "#3584e4" "#916ee4" "#b875dc" "#77216e" "#c748ba" "#e93a9a") out+=("^sep($4)") @@ -135,10 +134,6 @@ out2+=(" 5 < out2+=(" 0 ,t2ctl $3 0 ;${5}") fi -#Current -out+=("^sep($SELECT_COLOR)") -out+=(" $PICK_FROM_SCREEN,mbclr pixelcolor $2 $3 ${5} ${6}") -out+=(" $SELECT_OR_PICK,mbclr pick $2 $3 ${5} ${6}") CUR_FG=$(pastel textcolor $1|pastel format hex) out+=("^sep($SHADES_OF_CURRENT $1)") @@ -146,26 +141,31 @@ out+=("$LIGHTEN_DARKEN,^checkout(curshades)") out+=("$SATURATION,^checkout(saturate)") out+=("󰑧 $ROTATE_HSL_HUE,^checkout(rotate_hsl)") #out+=("$SHADES_OF_CURRENT ($1),^checkout(curshades)") - +out+=("^sep($CHANGE_COLOR)") +#Current +out+=(" $PICK_FROM_SCREEN,mbclr pixelcolor $2 $3 ${5} ${6}") +out+=(" $SELECT_OR_PICK,mbclr pick $2 $3 ${5} ${6}") out+=("^sep()") out+=(" $ACCENT_COLORS,^checkout(accents)") out2+=("^tag(accents)") out2+=("^sep($ACCENT_COLORS)") for i in ${accents[@]};do out2+=("$i ,$2 $3 '${i}' ${6};${5}");done +out+=("^sep()") +mapfile -t gtkcolors < <( gtkcolors.py 2>/dev/null| grep -v None|awk '{print "#"$2}'|sort|uniq) +if [ "${#gtkcolors[@]}" -gt 0 ];then +out+=("$GTKCOLORS,^checkout(gtkcols)") +out2+=("^tag(gtkcols)") +out2+=("^sep($GTKCOLORS)") +for i in ${gtkcolors[@]};do out2+=("$i ,$2 $3 '${i}' ${6};${5}");done out+=("^sep()") -out+=(" $LIGHTCOLORS,^checkout(light)") -out2+=("^tag(light)") -out2+=("^sep($LIGHTCOLORS)") -for i in "${light[@]}" -do -out2+=("$i ,$2 $3 '${i}' ${6};${5}") -done -out+=("^sep()") -out+=(" $DARKCOLORS,^checkout(dark)") -out2+=("^tag(dark)") -out2+=("^sep($DARKCOLORS)") -for i in "${dark[@]}" +fi + + +out+=("$GRAYS,^checkout(grayscol)") +out2+=("^tag(grayscol)") +out2+=("^sep($GRAYS)") +for i in "${grays[@]}" do out2+=("$i ,$2 $3 '${i}' ${6};${5}") done