code improvements

This commit is contained in:
#root_informatica 2023-09-25 23:34:52 -03:00
parent bce9589697
commit a843bd1c1d

View file

@ -1,38 +1,32 @@
#!/bin/sh
TARGET=$1 # image target
COLORS="/tmp/mimic" # path to the extracted colors file
TMPCOL="/tmp/tmpcol" # path to the extracted colors file
WMVAR="$HOME/.config/wms/wms_var" # window manager variables
DAC="a0a0a0" # default active color
DIC="404040" # default inactive color
DBC="#000000" # default background color
NC="3" # number of colors to extract
if [ -f "$TARGET" ]; then # if cw.png exist, extract colors
convert $TARGET -colors 3 -depth 6 -format '%c' histogram:info:- | awk '{print $3}' | cut -c 2-7 > $COLORS
# color extractor
colext() {
convert $TARGET -colors $NC -depth 6 -format '%c' histogram:info:- | awk -v RS="" '{print $11,$7,$3}' | tr -d '#'
}
if [ "$(wc -w < $COLORS)" = "3" ]; then # if two colors turned out
mainc=$(sed '3 p;d' < $COLORS) # main color
secondc=$(sed '2 p;d' < $COLORS) # second color
thirdc=$(sed '1 p;d' < $COLORS) # third color
# if the image file exists, extract the colors and store them temporarily
if [ -f "$TARGET" ]; then
colext > $TMPCOL
# change variables in ~/.wmvar
sed -i "s/^.*\bAC=\b.*$/AC=$mainc/" $WMVAR # new acive color
sed -i "s/^.*\bIC=\b.*$/IC=$secondc/" $WMVAR # new inactive color
sed -i "s/^.*\bBC=\b.*$/BC=$thirdc/" $WMVAR # new background color
# check if N colors have been extracted
if [ "$(wc -w < $TMPCOL)" = "$NC" ]; then
# load extracted colors into variables
read ac ic bc < $TMPCOL
# overwrite the values in ~/.config/wms/wms_var
sed -i "s/^.*\bAC=\b.*$/AC=$ac/" $WMVAR # new acive color
sed -i "s/^.*\bIC=\b.*$/IC=$ic/" $WMVAR # new inactive color
sed -i "s/^.*\bBC=\b.*$/BC=$bc/" $WMVAR # new background color
else # if no two colors turned out
sed -i "s/^.*\bAC=\b.*$/AC=$DAC/" $WMVAR # default acive color
sed -i "s/^.*\bIC=\b.*$/IC=$DIC/" $WMVAR # default inactive color
sed -i "s/^.*\bBC=\b.*$/BC=$DBC/" $WMVAR # default background color
# apply the change
wms_value.sh -d
wms_mainrole.sh -t
fi
cp .config/wms/wms_var /tmp/wms_var; # copy wms_var file into /tmp
wms_mainrole.sh -t # make it happen
else # if image target not exist
exit 0 # leave without change
fi
fi