#!/bin/sh
#by Recobayu
#191105 v-0.1 only selected favorite color (flatui color)
#191106 v-0.2 wdz color, also menu and tray, tooltip name
#191107 v-0.2.1 use yiq to set foreground is black or white (better than just average the rgb)
#191108 v-0.2.2 recolor. Selected color (active window) is the brightest, the menu is darker, the tray is darkest
#191108 v-0.3 add recent color
#191125 v-0.4 change choosen color when click on favorite or recent color button, exit by Esc key, add pure white and pure black color to favorite

cp /root/.jcc/jwmrc-theme-window-only /root/.jcc/jwmrc

recent_color(){
	touch /root/.jcc/recent
	if [ "`cat /root/.jcc/recent`" != "" ];then
	#~ echo ada;
	echo '<text xalign="0">
		<label>Recent color:</label>
	</text>'
	echo '<hbox spacing="0">'

	while read warna;do
	echo '<svg height="40" width="40"><path d="M0 0h40v40H0z" fill="'$warna'"/></svg>' > /root/.jcc/warna/$warna
	echo '	<button relief="2" image-position="2">
		<action>ganti_tema_umum "'$warna'"</action>
		<action>refresh:warna</action>
		<input file>/root/.jcc/warna/'$warna'</input>
	</button>'
	
	done < /root/.jcc/recent
	echo '</hbox>'
	
	fi
	#~ else echo takada;
	#~ fi
	#~ for i in /root/.jcc/
}

#~ recent_color

add_to_recent(){
	grep -v $1 /root/.jcc/recent|tail -7 > /root/.jcc/recent1
	echo $1 >> /root/.jcc/recent1
	mv /root/.jcc/recent1 /root/.jcc/recent
}
export -f add_to_recent

ganti_tema_umum(){
	warnaterang="$1"
	
	# warna_terang
	r=`printf '%d' 0x${warnaterang:1:2}`
	g=`printf '%d' 0x${warnaterang:3:2}`
	b=`printf '%d' 0x${warnaterang:5:2}`
	
	echo r g b = $r $g $b
	
	
	if [ "$r" -lt 60 ];then r=60;fi
	if [ "$g" -lt 60 ];then g=60;fi
	if [ "$b" -lt 60 ];then b=60;fi
	
	echo "r g b (baru)"= $r $g $b
	
	rhex=`printf '%02x' $r`
	ghex=`printf '%02x' $g`
	bhex=`printf '%02x' $b`
		
	warnaterang="#$rhex$ghex$bhex"
	
	
	yiq=`echo "(($r*299)+($g*587)+($b*114))/1000"|bc`
	echo yiq=$yiq
	
	
	# warna_biasa
	r1=$(( r - 30 ))
	g1=$(( g - 30 ))
	b1=$(( b - 30 ))
	
	echo r1 g1 b1 = $r1 $g1 $b1
		
	yiq1=`echo "(($r1*299)+($g1*587)+($b1*114))/1000"|bc`
	echo yiq1=$yiq1
			
	r1hex=`printf '%02x' $r1`
	g1hex=`printf '%02x' $g1`
	b1hex=`printf '%02x' $b1`
		
	warnabiasa="#$r1hex$g1hex$b1hex"
	
	# warna_gelap
	r2=$(( r - 60 ))
	g2=$(( g - 60 ))
	b2=$(( b - 60 ))
	
	echo r2 g2 b2 = $r2 $g2 $b2
	
	yiq2=`echo "(($r2*299)+($g2*587)+($b2*114))/1000"|bc`
	echo yiq2=$yiq2
	
	r2hex=`printf '%02x' $r2`
	g2hex=`printf '%02x' $g2`
	b2hex=`printf '%02x' $b2`
		
	warnagelap="#$r2hex$g2hex$b2hex"
	
	
	# huruf_biasa
	if [ "$yiq" -gt 127 ];then
		hurufterang="#000"
	else
		hurufterang="#FFF"
	fi
	
	
	# huruf_terang
	if [ "$yiq1" -gt 127 ];then
		hurufbiasa="#000"
	else
		hurufbiasa="#FFF"
	fi
	
	
	# huruf_gelap
	if [ "$yiq2" -gt 127 ];then
		hurufgelap="#000"
	else
		hurufgelap="#FFF"
	fi
	
	sed "s/warna_biasa/$warnabiasa/g" /root/.jcc/jwmrc > /root/.jcc/jwmrc-theme-baru
	sed -i "s/warna_terang/$warnaterang/g" /root/.jcc/jwmrc-theme-baru
	sed -i "s/warna_gelap/$warnagelap/g" /root/.jcc/jwmrc-theme-baru

	sed -i "s/huruf_biasa/$hurufbiasa/g" /root/.jcc/jwmrc-theme-baru
	sed -i "s/huruf_terang/$hurufterang/g" /root/.jcc/jwmrc-theme-baru
	sed -i "s/huruf_gelap/$hurufgelap/g" /root/.jcc/jwmrc-theme-baru

	cp /root/.jwm/{jwmrc-theme,jwmrc-theme.bak}
	cp /root/.jcc/jwmrc-theme-baru /root/.jwm/jwmrc-theme
	jwm -restart
	
	add_to_recent $warnaterang
	echo "$warnaterang" > /root/.jcc/warnasekarang
}
export -f ganti_tema_umum

buat_tombol(){
	mkdir -p /root/.jcc/warna
	
	j=1
	k=1
	n=`wc -l /root/.jcc/warna_wdz_all|cut -d" " -f1`
	n=$(( $n - 1 ))
	
	echo '<vbox spacing="0">'
	echo '<hbox spacing="0">'
	while read i;do
	hex=`echo $i|cut -d"|" -f1`
	name=`echo $i|cut -d"|" -f2`
	echo '<svg height="40" width="40"><path d="M0 0h40v40H0z" fill="'$hex'"/></svg>' > /root/.jcc/warna/$hex
	echo '	<button relief="2" image-position="2" tooltip-text="'"$name"'">
		<action>ganti_tema_umum "'$hex'"</action>
		<action>refresh:warna</action>
		<input file>/root/.jcc/warna/'$hex'</input>
	</button>'
	
	if [ "$k" -lt "$n" ] && [ "$j" -eq 8 ];then
	echo '</hbox>
	<hbox spacing="0">'
	fi
	
	j=$(( $j % 8 ))
	((j++))
	((k++))
	
	done < /root/.jcc/warna_wdz_all
	
	echo '</hbox>'
	echo '</vbox>'
}
export -f buat_tombol

[ ! -f /root/.jcc/warnasekarang ] && echo "#0077D6" > /root/.jcc/warnasekarang

export tema_jwm_gui='
<window title="jwm theme color changer" resizable="false" icon-name="desktop" window-position="1">
<vbox>
	<text xalign="0">
		<label>Choose your color window</label>
	</text>
	<colorbutton>
		<input file>/root/.jcc/warnasekarang</input>
		<action>ganti_tema_umum "$warna"</action>
		<variable>warna</variable>
	</colorbutton>
	
	'"`recent_color`"'
	
	<text xalign="0">
		<label>Or Use from this favorite:</label>
	</text>
	'"`buat_tombol`"'
	<checkbox>
		<label>Also menu and tray</label>
		<variable>chkmenu</variable>
		<action>if true cp /root/.jcc/jwmrc-theme /root/.jcc/jwmrc</action>
		<action>if false cp /root/.jcc/jwmrc-theme-window-only /root/.jcc/jwmrc</action>
	</checkbox>
</vbox>
<action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">exit:EXIT</action>
</window>'

echo "$tema_jwm_gui" > /root/.jcc/guiku



gtkdialog -f /root/.jcc/guiku
