diff --git a/bin/color_picker.sh b/bin/color_picker.sh index e3a8507..54e6daa 100755 --- a/bin/color_picker.sh +++ b/bin/color_picker.sh @@ -1,25 +1,70 @@ +#!/usr/bin/env bash . ~/cshenv/lib/vulgar.sh -save_block 11 1 15 32 -erase_block 11 1 15 32 +echo '[?25l' +echo '[?25h' +echo '[?25l' -create_button Red 12 2 Red 1 -highlight_button Red +erase_block 1 1 15 34 +build_box 1 1 15 34 +fill_block_attributes 1 1 15 34 47 30 +write_string "Terminal Color Picker" 2 7 47 30 + +create_button Red 12 3 Red 1 #build_box 12 2 14 8 #write_string "Red" 13 4 #fill_block_attributes 12 2 14 8 41 -create_button Green 12 12 Green 2 +create_button Green 12 13 Green 2 #build_box 12 12 14 20 #write_string "Green" 13 14 -create_button Blue 12 24 Blue 4 +create_button Blue 12 25 Blue 4 #build_box 12 24 14 31 #write_string "Blue" 13 26 +highlight_button Red + +# TODO: Add color picker +# TODO: Negative colors support specials: +# -1 is background +# -2 is foreground +# -3 is cursor +color=12 + +write_string "Index:" 4 3 47 30 +erase_block 4 10 4 12 +color_padded=" ${color}" +write_string "${color_padded:-3}" 4 10 + +write_string "Code:" 4 14 47 30 +erase_block 4 20 4 25 +get_color_hex ${color} red green blue +write_string "${red}${green}${blue}" 4 20 + + +build_box 6 9 9 25 +fill_block_attributes 7 10 8 24 44 +#erase_block 4 13 4 21 + +Controls=() + Buttons=("Red" "Green" "Blue") Index=0 +Colors=( $((0x${red})) $((0x${green})) $((0x${blue})) ) + + +tmp=`stty -g` +stty -echo + +update_color() +{ + _color=${1} + echo -n "]4;${_color};#$(hexbyte ${Colors[0]})$(hexbyte ${Colors[1]})$(hexbyte ${Colors[2]})" + write_string "$(hexbyte ${Colors[0]})$(hexbyte ${Colors[1]})$(hexbyte ${Colors[2]})" 4 20 +} + while true do read_key codes @@ -45,11 +90,32 @@ do unhighlight_button ${Buttons[${OldIndex}]} highlight_button ${Buttons[${Index}]} fi + elif [ "${sym}" == "Up" ] + then + #echo "${Colors[${Index}]}:$(hexbyte ${Colors[${Index}]} )" + if (( ${Colors[${Index}]} < 255 )) + then + Colors[${Index}]=$(( ${Colors[${Index}]} + 1 )) + fi + update_color ${color} + elif [ "${sym}" == "Down" ] + then + #echo "${Colors[${Index}]}:$(hexbyte ${Colors[${Index}]} )" + if (( ${Colors[${Index}]} > 0 )) + then + Colors[${Index}]=$(( ${Colors[${Index}]} - 1 )) + fi + update_color ${color} elif [ "${sym}" == "Escape" ] then - #erase_block 11 1 15 32 break + elif [ "${sym}" == "0" ] + then + Colors[${Index}]=0 + update_color ${color} fi done -restore_block 11 1 15 32 -sleep 10 + +stty ${tmp} +#sleep 0.10 +echo -n '[?25h' diff --git a/lib/vulgar.sh b/lib/vulgar.sh index 1656ec9..0f37e3f 100755 --- a/lib/vulgar.sh +++ b/lib/vulgar.sh @@ -21,7 +21,8 @@ send_csi_command() shift 1 - echo -n "[${1}" + echo -n "" + echo -n "[${1}" # First one no ';' prefix.... #echo -n "${1}" @@ -44,12 +45,22 @@ send_csi_command() save_block() { - send_csi_command '$v' $* 1 $(( 25 + $1 )) $(( 25 + $2 )) $(( 25 + $3 )) $(( 25 + $4 )) 1 + _T=$1 + _L=$2 + _B=$3 + _R=$4 + send_csi_command '$v' $_T $_L $_B $_R 1 1 140 1 } restore_block() { - send_csi_command '$v' $(( 25 + $1 )) $(( 25 + $2 )) $(( 25 + $3 )) $(( 25 + $4 )) 1 $* 1 + _T=$1 + _L=$2 + _B=$3 + _R=$4 + _W=$(( ${_R} - ${_L} + 1 )) + _H=$(( ${_B} - ${_T} + 1 )) + send_csi_command '$v' 1 140 $_H $(( 140 + $_W - 1 )) 1 $_T $_L 1 } ##### END DECCRA Block ############## @@ -97,6 +108,7 @@ write_string() _row=$1 shift 1 _col=$1 + shift 1 while [ -n "$_str" ] do _ch=${_str:0:1} @@ -151,47 +163,40 @@ build_box() read_key() { seq="" - read -sn 1 key + read -sN 1 key code=`printf "%x" "'$key"` - seq=${code} + seq=K${code} while read -t 0 do - read -sn 1 key + read -sN 1 key code=`printf "%x" "'$key"` - seq="${seq} ${code}" + seq="${seq}${code}" done eval $1=\"${seq}\" } +declare -A _keycodes +_keycodes["K9"]="Tab" +_keycodes["K20"]="Space" +_keycodes["K1b"]="Escape" +_keycodes["K1b5b41"]="Up" +_keycodes["K1b5b42"]="Down" +_keycodes["K1b5b44"]="Left" +_keycodes["K1b5b43"]="Right" +_keycodes["K30"]="0" + translate_key() { - _codes=${1} - case "${codes}" in - "1b 5b 41") - echo "Up" - ;; - - "1b 5b 42") - echo "Down" - ;; - - "1b 5b 44") - echo "Left" - ;; - - "1b 5b 43") - echo "Right" - ;; - - "1b") - echo "Escape" - ;; - - *) - echo "${codes}" - ;; - esac + if [[ ! -v _keycodes[$1] ]]; + then + echo "MISS ($1)" > /dev/fd/2 + echo "$1" + else + _key="${_keycodes[${1}]}" + #echo "HIT (${_key})" > /dev/fd/2 + echo ${_key} + fi } @@ -322,3 +327,61 @@ get_terminal_size() eval $1=\"${_rows}\" eval $2=\"${_cols}\" } + +#parse_color "${hex}" red green blue + +declare -A _hexdigits +_hexdigits["0"]="0" +_hexdigits["1"]="1" +_hexdigits["2"]="2" +_hexdigits["3"]="3" +_hexdigits["4"]="4" +_hexdigits["5"]="5" +_hexdigits["6"]="6" +_hexdigits["7"]="7" +_hexdigits["8"]="8" +_hexdigits["9"]="9" +_hexdigits["10"]="a" +_hexdigits["11"]="b" +_hexdigits["12"]="c" +_hexdigits["13"]="d" +_hexdigits["14"]="e" +_hexdigits["15"]="f" + +hexbyte() +{ + echo -n "${_hexdigits[$((${1}/16))]}${_hexdigits[$((${1}%16))]}" +} + +get_color_hex() +{ + _color=$1 + _tmp=`stty -g` + stty -echo + echo -n ']4;'"${_color}"';?' + read -sN 1 _ch # We know we'll get an Esc back, so drop it + read -sN 1 _ch + _hex="" + while [ "${_ch}" != '' ] + do + _hex="${_hex}${_ch}" + read -sN 1 _ch + done + stty ${_tmp} + + #echo "Hex Pre: ${_hex}" + _hex=${_hex/]4;${_color};rgb:/} + #echo "Hex: ${_hex}" + _red=${_hex:0:2} + _green=${_hex:5:2} + _blue=${_hex:10:2} + + #echo "R: ${_red}" + #echo "G: ${_green}" + #echo "B: ${_blue}" + + eval $2=\"${_red}\" + eval $3=\"${_green}\" + eval $4=\"${_blue}\" +} +