URI:
       theme - scripts - various script and utils
  HTML git clone git://z3bra.org/scripts
   DIR Log
   DIR Files
   DIR Refs
       ---
       theme (3565B)
       ---
            1 #!/bin/sh
            2 
            3 themedir="${XDG_CONFIG_DIR:-$HOME/.config}/theme"
            4 
            5 usage() {
            6         echo "usage: $(basename $0) [-dlr] [-fb color[,secondary]] [-p name] [[-c name] file]" >&2
            7 }
            8 
            9 preset() {
           10         theme="${themedir}/$1"
           11         if [ ! -e "$theme" ]; then
           12                 printf '%s: No such theme\n' "$1" >&2
           13                 exit 1
           14         fi
           15 
           16         . $theme
           17         ln -sf $img $HOME/.bg.png
           18 }
           19 
           20 mktheme() {
           21         if [ ! -e "$img" ]; then
           22                 printf '%s: No such file or directory\n' "$image" >&2
           23                 exit 1
           24         fi
           25 
           26         # display picture and colorspace
           27         2ff <$img|lel -a -w 540 -h 304 &
           28         colors -pn16 <$img|hex2col
           29 
           30         printf 'bg: '; read bg
           31         printf 'dm: '; read dm
           32         printf 'fg: '; read fg
           33         printf 'hl: '; read hl
           34 
           35         path=$(readlink -f $img|sed "s,$HOME,\$HOME,")
           36         printf 'img="%s"\nbg="%s,%s"\nfg="%s,%s"\n' "$path" "$bg" "$dm" "$fg" "$hl" \
           37                 |tee "$HOME/.config/theme/${1}"
           38 }
           39 
           40 repack() {
           41         (cd $1; pack >/dev/null 2>&1)
           42 }
           43 
           44 setup_lowlife() {
           45         sed -ri \
           46                 -e "/color_unfocus/s/0x[0-9a-f]+/0x${dm#\#}/" \
           47                 -e "/color_focus/s/0x[0-9a-f]+/0x${hl#\#}/" \
           48                 $HOME/build/lowlife/config.h
           49         repack $HOME/build/lowlife
           50         pkill lowlife; nohup lowlife >/dev/null &
           51 }
           52 
           53 setup_menuw() {
           54         sed -i \
           55                 -e "/background =/s/=.*/= 0x${bg#\#};/" \
           56                 -e "/foreground =/s/=.*/= 0x${hl#\#};/" \
           57                 $HOME/build/menuw/config.h
           58         repack $HOME/build/menuw
           59         pkill menuw; nohup rootmenu >/dev/null &
           60 }
           61 
           62 setup_foot() {
           63         [ "$mood" = "dark" ] && theme=1 || theme=2
           64         sed -i \
           65                 -e "/foreground/s/=.*/=${fg#\#}/" \
           66                 -e "/background/s/=.*/=${bg#\#}/" \
           67                 -e "/initial-color-theme/s/=.*/=${theme}/" \
           68                 $HOME/.config/foot/foot.ini
           69 }
           70 
           71 setup_vis() {
           72         sed -i \
           73                 -e "/color_high =/s/=.*/= '$hl'/" \
           74                 -e "/color_low  =/s/=.*/= '$dm'/" \
           75                 $HOME/.config/vis/themes/simple.lua
           76 }
           77 
           78 setup_tofi() {
           79         sed -i \
           80                 -e "/^text-color/s/=.*/= $fg/" \
           81                 -e "/^prompt-color/s/=.*/= $dm/" \
           82                 -e "/^selection-color/s/=.*/= $hl/" \
           83                 -e "/^background-color/s/=.*/= $bg/" \
           84                 $HOME/.config/tofi/config
           85 }
           86 
           87 setup_waybar() {
           88         # Make sure waybar contrasts with the background filling color
           89         # When theme is "light", fg/bg colors are swapped
           90         [ "$mood" = "$(colorscheme $fill)" ] && c=$hl || c=$dm
           91         sed -ri "/^#(clock|battery)/s/color:.*;/color:${c};/" $HOME/.config/waybar/style.css
           92 }
           93 
           94 now=$(date +%H%M)
           95 case $(date +%H) in
           96 0[7-9]|1*) mood=light;;
           97 0[0-6]|2*) mood=dark;;
           98 esac
           99 
          100 while getopts "hdlrc:f:b:p:m:" OPT; do
          101         case $OPT in
          102         c) create=$OPTARG;;
          103         d) dryrun=1;;
          104         f) fg="$OPTARG";;
          105         b) bg="$OPTARG";;
          106         l) ls $themedir; exit 0;;
          107         r) preset=$(ls $themedir|shuf|head -n1);;
          108         m) mood="$OPTARG";;
          109         p) preset="$OPTARG";;
          110         h) usage; exit 0;;
          111         *) usage; exit 1;;
          112         esac
          113 done
          114 shift $((OPTIND - 1))
          115 
          116 if [ -n "$preset" ]; then
          117         preset $preset
          118 else
          119         preset $(ls $themedir|shuf|head -n1)
          120 fi
          121 
          122 if [ -n "$create" ]; then
          123         img=${1:-$HOME/.bg.png}
          124         mktheme "$create"
          125 fi
          126 
          127 # Invert colors for light themes
          128 if [ "$mood" = "light" ]; then
          129         tmp=$fg
          130         fg=$bg
          131         bg=$tmp
          132 fi
          133 
          134 hl=${fg#*,}
          135 dm=${bg#*,}
          136 fg=${fg%,*}
          137 bg=${bg%,*}
          138 
          139 
          140 # Use first pixel to compute filler color using raw farbfeld.
          141 # Pixelspace is 16 bits, so we must do a quick cut(1) dance to get the
          142 # color as 8 bits
          143 alpha=$(<$img png2ff|xxd -c0 -p|cut -b45-46)
          144 rgb=$(<$img png2ff|xxd -c0 -p|cut -b33-34,37-38,41-42)
          145 
          146 # Use random pastel colors for transparent pixel (unless specified in theme)
          147 case "$alpha" in
          148 00) fill=${fill:-$(pastel -m $mood)} ;;
          149  *) fill="#${rgb}" ;;
          150 esac
          151 
          152 if [ ${dryrun:-0} -eq 1 ]; then
          153         readlink -f $img
          154         2ff <$img|lel -a -w 540 -h 304 &
          155         printf "%s\n" "$bg" "$dm" "$fg" "$hl"|hex2col
          156         exit 0
          157 fi
          158 
          159 pkill swaybg; swaybg -c "$fill" -i $img -m ${mode:-fit} 2>/dev/null &
          160 setup_foot  &
          161 setup_waybar  &
          162 setup_menuw  &
          163 setup_tofi  &
          164 setup_vis &