mabox-tools/bin/mb-canvas

410 lines
11 KiB
Bash
Executable File

#!/bin/bash
## An Imagemagick Script To Generate Wallpapers.
## Based on https://github.com/adi1090x/canvas by Aditya Shakya (adi1090x)
## Modified by Daniel Napora for Mabox Linux to run from menu
# TODO: add missing options to some effects
# TODO: add ability to pick and use 4 predefined colors
# TODO: choose picker: xcolor or gpick
CONFIG_DIR="$HOME/.config/mbxutils/imgw"
CONFIG_FILE="$CONFIG_DIR/imgw.cfg"
mkdir -p $CONFIG_DIR
if [ ! -f $CONFIG_FILE ]; then
cat <<EOF > ${CONFIG_FILE}
show_preview=yes
EOF
fi
source <(grep = $CONFIG_FILE)
case $LANG in
pl*)
IMWG="Generator Tapet"
PICKCOLOR="Pobierz kolor z ekranu"
PICKCOLORS="Pobierz kolory z ekranu"
PICKEDCOLOR="Pobrany kolor"
PICKNEXT="Pobierz nastepny"
PICKEDALL="Generuję tapetę..."
SETASWP="Ustawić jako tapetę?"
YES="Tak"
NO="Nie"
;;
*)
IMWG="ImageMagick Wallpaper Generator"
PICKCOLOR="Pick color from screen"
PICKCOLORS="Pick colors from screen"
PICKEDCOLOR="Picked color"
PICKNEXT="Please pick next..."
PICKEDALL="Generating Wallpaper..."
SETASWP="Set as Wallpaper?"
YES="Yes"
NO="No"
;;
esac
DIR="$(xdg-user-dir PICTURES)/mb-canvas"
size="$(xrandr --current | grep '*' | uniq | awk '{print $1}')"
name="mb-canvas_$(date +%Y-%m-%d_%I-%M-%S).png"
#SETTER="feh --bg-scale";
SETTER="nitrogen --set-scaled --save"
#picker_app="gpick -s -o --no-newline"
picker_app="xcolor"
if [[ ! -d "$DIR" ]]; then
mkdir -p "$DIR"
fi
set_bg() {
if [[ "$show_preview" == "yes" ]];then
X="${size%x*}"
Y="${size#*x}"
P_X=$((X/4))
P_Y=$((Y/4+30))
yad --title="$SETASWP" --window-icon=mbcc --borders=0 --geometry="${P_X}x${P_Y}+200+30" \
--picture --size=fit --filename="$DIR/$name" \
--button="$NO":1 --button="$YES":2 \
ODP="$?"
[[ "$ODP" -eq "1" ]] && exit 0
if [[ "$ODP" -eq "2" ]]; then
$SETTER "$DIR/$name" && exit 0
fi
else
$SETTER "$DIR/$name"
fi
}
## Pick the colors
color_picker() {
if [[ "$SOLID" == "true" ]]; then
notify-send.sh "$IMWG" "$PICKCOLOR" -i mbcc --expire-time=120000
color=$($picker_app)
elif [[ "$GRADIENT" == "true" ]] || [[ "$RGRADIENT" == "true" ]] || [[ "$TGRADIENT" == "true" ]] || "$PLASMA" == "true" ]]; then
notify-send.sh "$IMWG" "$PICKCOLORS: 2" -i mbcc --expire-time=120000
color1=$($picker_app)
FNAME="/tmp/${color1:1:6}.png"
convert -size 100x100 xc:"$color1" "$FNAME"
notify-send.sh "$PICKEDCOLOR 1" "$color1\n $PICKNEXT..." --icon="$FNAME" --expire-time=120000
color2=$($picker_app)
FNAME="/tmp/${color2:1:6}.png"
convert -size 100x100 xc:"$color2" "$FNAME"
notify-send.sh "$PICKEDCOLOR 2" "$color1\n $PICKEDALL" --icon="$FNAME" --expire-time=120000
color="$color1-$color2"
elif [[ "$BGRADIENT" == "true" ]]; then
notify-send.sh "$IMWG" "$PICKCOLORS: 4" -i mbcc --expire-time=120000
color1=$($picker_app)
FNAME="/tmp/${color1:1:6}.png"
convert -size 100x100 xc:"$color1" "$FNAME"
notify-send.sh "$PICKEDCOLOR 1" "$color1\n $PICKNEXT..." --icon="$FNAME" --expire-time=120000
color2=$($picker_app)
FNAME="/tmp/${color2:1:6}.png"
convert -size 100x100 xc:"$color2" "$FNAME"
notify-send.sh "$PICKEDCOLOR 2" "$color2\n $PICKNEXT..." --icon="$FNAME" --expire-time=120000
color3=$($picker_app)
FNAME="/tmp/${color3:1:6}.png"
convert -size 100x100 xc:"$color3" "$FNAME"
notify-send.sh "$PICKEDCOLOR 3" "$color3\n $PICKNEXT..." --icon="$FNAME" --expire-time=120000
color4=$($picker_app)
FNAME="/tmp/${color4:1:6}.png"
convert -size 100x100 xc:"$color4" "$FNAME"
notify-send.sh "$PICKEDCOLOR 4" "$color4\n $PICKEDALL" --icon="$FNAME" --expire-time=120000
fi
}
## Get colors
get_color() {
while true; do
color_picker
break
done
}
## Generate a solid color wallpaper
solid() {
if [[ "$RANDOMIZE" == "true" ]]; then
get_random_color
color="$RCOLOR"
else
get_color
fi
convert -size "$size" canvas:"$color" "$DIR/$name"
set_bg
}
## Generate a linear gradient wallpaper
linear_gradient() {
if [[ "$RANDOMIZE" == "true" ]]; then
get_random_color
color="$RCOLOR"
get_random_number "360"
angle="$RNUM"
else
get_color
angle=$(yad --center --window-icon=mbcc --title="Linear gradient" --text="\tRotation angle (default is 0):" --width=400 --scale --min-value=0 --max-value=360 --step=45 --enforce-step --inc-buttons)
fi
name="linear_$name"
convert -size "$size" -define gradient:angle="${angle:-0}" gradient:"$color" "$DIR/$name"
set_bg
}
## Generate a radial gradient wallpaper
radial_gradient() {
if [[ "$RANDOMIZE" == "true" ]]; then
get_random_color
color1="$RCOLOR"
get_random_color
color="$color1-$RCOLOR"
get_random_number "360"
angle="$RNUM"
getRandomShape
shape="$RSHAPE"
else
get_color
shape=$(yad --center --window-icon=mbcc --title='Choose Shape' --width=180 --height=145 --button=Ok:0 --list --no-headers --column=Shape diagonal ellipse maximum minimum | tr -d '|')
angle=$(yad --center --window-icon=mbcc --title="Radial gradient" --text="\tRotation angle:" --width=400 --scale --min-value=0 --max-value=360 --step=45 --enforce-step --inc-buttons)
fi
name="radial_$name"
convert -size "$size" -define gradient:extent="${shape:-maximum}" -define gradient:angle="${angle:-0}" radial-gradient:"$color" "$DIR/$name"
set_bg
}
## Generate a twisted gradient wallpaper
twisted_gradient() {
if [[ "$RANDOMIZE" == "true" ]]; then
get_random_color
color1="$RCOLOR"
get_random_color
color="$color1-$RCOLOR"
get_random_number "500"
twist="$RNUM"
else
get_color
twist=$(yad --center --window-icon=mbcc --title="Radial gradient" --text="\tTwisting amount (default is 150):" --width=400 --scale --value=150 --min-value=0 --max-value=500 --step=20 --inc-buttons)
fi
name="twisted_$name"
convert -size "$size" gradient:"$color" -swirl "${twist:-150}" "$DIR/$name"
set_bg
}
## Generate a 4 point gradient wallpaper
bilinear_gradient() {
if [[ "$RANDOMIZE" == "true" ]]; then
get_random_color
color1="$RCOLOR"
get_random_color
color2="$RCOLOR"
get_random_color
color3="$RCOLOR"
get_random_color
color4="$RCOLOR"
get_random_number "2"
if [[ "$RNUM" == "1" ]]; then
answer="Smooth"
else
answer="Regular"
fi
else
get_color
answer=$(yad --title='Choose Style' --width=220 --height=120 --button=Ok:0 --list --no-headers --column=Style Smooth Regular | tr -d '|')
fi
name="bilinear_$name"
if [[ $answer == "Smooth" ]] || [[ $answer == "S" ]]; then
convert \( xc:"$color1" xc:"$color2" +append \) \( xc:"$color3" xc:"$color4" +append \) -append -size "$size" xc: +swap -fx 'v.p{i/(w-1),j/(h-1)}' "$DIR/$name"
else
convert \( xc:"$color1" xc:"$color2" +append \) \( xc:"$color3" xc:"$color4" +append \) -append -filter triangle -resize "$size"\! "$DIR/$name"
fi
set_bg
}
## Generate a plasma wallpaper
plasma() {
if [[ "$RANDOMIZE" == "true" ]]; then
get_random_number "3"
if [[ "$RNUM" == "1" ]]; then
answer="Random"
elif [[ "$RNUM" == "2" ]]; then
answer="Twisted"
get_random_number "500"
twist="$RNUM"
else
answer="c"
get_random_color
color="$RCOLOR"
fi
else
answer=$(yad --title='Choose Style' --width=190 --height=150 --button=Ok:0 --list --no-headers --column=Style Random Twisted 'Custom colors' | tr -d '|')
fi
name="plasma_$name"
if [[ $answer == "r" ]] || [[ $answer == "Random" ]]; then
convert -size "$size" plasma: "$DIR/$name"
elif [[ $answer == "t" ]] || [[ $answer == "Twisted" ]]; then
if [[ "$RANDOMIZE" != "true" ]]; then
twist=$(yad --center --window-icon=mbcc --title="Plasma" --text="\tTwisting amount (default is 150):" --width=400 --scale --value=150 --min-value=0 --max-value=500 --step=20 --inc-buttons)
fi
convert -size "$size" plasma:fractal -swirl "${twist:-150}" "$DIR/$name"
else
if [[ "$RANDOMIZE" != "true" ]]; then
get_color
fi
convert -size "$size" plasma:"$color" "$DIR/$name"
fi
set_bg
}
## Generate a random, multi-colored blurred/gradient wallpaper
blurred_noise() {
if [[ "$RANDOMIZE" == "true" ]]; then
get_random_number "30"
blur="$RNUM"
else
{ echo; read -p ${ORANGE}"Enter the blur strength (maximum 30): "${BLUE} blur; }
fi
convert -size "100x56" xc: +noise Random "/tmp/noise.png"
convert "/tmp/noise.png" -virtual-pixel tile -blur 0x"${blur:-14}" -auto-level -resize "$size" "$DIR/$name"
set_bg
}
## Generate random number lower than giver parameter
get_random_number() {
RNUM=$(( ($RANDOM % $1) + 1 ))
}
## Generate random color
get_random_color() {
RCOLOR="#"
for i in 1 2 3 4 5 6
do
get_random_number "16"
case $RNUM in
"1") NEXTDIGIT="1";;
"2") NEXTDIGIT="2";;
"3") NEXTDIGIT="3";;
"4") NEXTDIGIT="4";;
"5") NEXTDIGIT="5";;
"6") NEXTDIGIT="6";;
"7") NEXTDIGIT="7";;
"8") NEXTDIGIT="8";;
"9") NEXTDIGIT="9";;
"10") NEXTDIGIT="A";;
"11") NEXTDIGIT="B";;
"12") NEXTDIGIT="C";;
"13") NEXTDIGIT="D";;
"14") NEXTDIGIT="E";;
"15") NEXTDIGIT="F";;
"16") NEXTDIGIT="0";;
esac
RCOLOR="$RCOLOR$NEXTDIGIT"
done
}
## Generate random shape option
getRandomShape() {
get_random_number "4"
case $RNUM in
"1") RSHAPE="diagonal";;
"2") RSHAPE="ellipse";;
"3") RSHAPE="maximum";;
"4") RSHAPE="minimum";;
esac
}
## Generate random wallpaper
randomize() {
get_random_number "7"
case $RNUM in
"1") solid;;
"2") linear_gradient;;
"3") radial_gradient;;
"4") twisted_gradient;;
"5") bilinear_gradient;;
"6") plasma;;
"7") blurred_noise;;
esac
}
## Parse the arguments
options=$(getopt -o S:,s,l,r,t,b,p,B,h,a,R --long size:,solid,linear,radial,twisted,bilinear,plasma,blurred,help,autobg,randomize -- "$@")
eval set -- "$options"
while true; do
case "$1" in
-S|--size)
shift;
size=$1
;;
-s|--solid)
SOLID="true"
COLOR=$1
;;
-l|--linear)
GRADIENT="true"
;;
-r|--radial)
RGRADIENT="true"
;;
-t|--twisted)
TGRADIENT="true"
;;
-b|--bilinear)
BGRADIENT="true"
;;
-p|--plasma)
PLASMA="true"
;;
-B|--blurred)
NOISE="true"
;;
-a|--autobg)
AUTOBG="true"
;;
-R|--randomize)
RANDOMIZE="true"
;;
-h|--help)
usage
exit
;;
--)
shift
break
;;
esac
shift
done
## Main Execution
if [[ "$SOLID" == "true" ]]; then
solid
elif [[ "$GRADIENT" == "true" ]]; then
linear_gradient
elif [[ "$RGRADIENT" == "true" ]]; then
radial_gradient
elif [[ "$TGRADIENT" == "true" ]]; then
twisted_gradient
elif [[ "$BGRADIENT" == "true" ]]; then
bilinear_gradient
elif [[ "$PLASMA" == "true" ]]; then
plasma
elif [[ "$NOISE" == "true" ]]; then
blurred_noise
elif [[ "$RANDOMIZE" == "true" ]]; then
randomize
else
usage
fi