color-hash.ksh: small improvements

Don't set color and return the index instead. This allows also using it
for background. Sprinkle a couple of '-nr --' for 'print' calls. Add the
fnv1 variant.
This commit is contained in:
Lucas 2023-10-24 19:51:20 +00:00
parent 0d7fd8f76a
commit 19049d2472
3 changed files with 69 additions and 37 deletions

View File

@ -1,6 +1,4 @@
#!/bin/sh #!/bin/sh
/usr/local/bin/sct 3500
xset r rate 300 75 xset r rate 300 75
xsetroot -solid "#000000" xsetroot -solid "#000000"
/usr/local/bin/xli -onroot -gamma 2.2 /etc/X11/xenodm/pixmaps/FreeHugsBSD-bg.png
xconsole -geometry 480x130-0-0 -daemon -notify -verbose -exitOnFail xconsole -geometry 480x130-0-0 -daemon -notify -verbose -exitOnFail

View File

@ -6,9 +6,25 @@ function _color_hash_djb2
while (( ${#s} > 0 )); do while (( ${#s} > 0 )); do
c=0x${s%${s#??}} c=0x${s%${s#??}}
s=${s#??} s=${s#??}
h=$(( ((($h << 5) + $h) + $c) & 0xffff )) h=$(( ((($h << 5) + $h) ^ $c) & 0xffff ))
done done
print $h print -nr -- $h
}
function _color_hash_fnv1
{
local c h s
s=$1
h=${2:-2166136261}
while (( ${#s} > 0 )); do
c=0x${s%${s#??}}
s=${s#??}
h=$(( (($h << 24) + ($h << 8) +
($h << 7) + ($h << 4) + ($h << 1) + $h) & 0xffffffff ))
h=$(( $h ^ $c ))
done
# xor-fold to 16 bits
print -nr -- $(( ($h >> 16) ^ ($h & 0xffff) ))
} }
function _color_hash_fnv1a function _color_hash_fnv1a
@ -24,7 +40,7 @@ function _color_hash_fnv1a
($h << 7) + ($h << 4) + ($h << 1) + $h) & 0xffffffff )) ($h << 7) + ($h << 4) + ($h << 1) + $h) & 0xffffffff ))
done done
# xor-fold to 16 bits # xor-fold to 16 bits
print $(( ($h >> 16) ^ ($h & 0xffff) )) print -nr -- $(( ($h >> 16) ^ ($h & 0xffff) ))
} }
function color_hash function color_hash
@ -36,7 +52,7 @@ function color_hash
while getopts bi: flag; do while getopts bi: flag; do
case $flag in case $flag in
b) bright=true ;; b) bright=true ;;
i) if [[ $OPTARG != @(djb2|fnv1a) ]]; then i) if [[ $OPTARG != @(djb2|fnv1|fnv1a) ]]; then
return 1 return 1
fi fi
impl=_color_hash_$OPTARG impl=_color_hash_$OPTARG
@ -47,7 +63,7 @@ function color_hash
shift $(($OPTIND - 1)) shift $(($OPTIND - 1))
h= h=
s=$(print -nr "$1" | od -A n -t x1 | tr -d '[:space:]*') s=$(print -nr -- "$1" | od -A n -t x1 | tr -d '[:space:]*')
while :; do while :; do
h=$($impl "$s" $h) h=$($impl "$s" $h)
# Avoid modulo bias. # Avoid modulo bias.
@ -63,5 +79,5 @@ function color_hash
n=$(( $h + ($h < 6 ? 1 : 3) )) n=$(( $h + ($h < 6 ? 1 : 3) ))
fi fi
tput setaf $n 0 0 2>/dev/null print -nr -- "$n"
} }

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# env # env
# Written in 2022 by Lucas # Written in 2022-2023 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved # CC0 1.0 Universal/Public domain - No rights reserved
# #
# To the extent possible under law, the author(s) have dedicated all # To the extent possible under law, the author(s) have dedicated all
@ -12,52 +12,70 @@
usage() usage()
{ {
printf "Usage: %s [targetdpi]\n" "${0##*/}" >&2 printf "Usage: %s [-n] [targetdpi]\n" "${0##*/}" >&2
exit 1 exit 1
} }
err()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
exit 1
}
show=false
while getopts n flag; do
case $flag in
n) show=true ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
if [ $# -gt 1 ]; then if [ $# -gt 1 ]; then
usage usage
fi fi
FACESIZES="10.5 6.0 8.0 9.0 10.5 12.0 18.0" FACESIZES="8.0 4.0 6.0 7.0 8.0 9.0 10.0 12.0"
BASEDPI=96
if [ -n "$1" ]; then basedpi=$(xdpyinfo | grep '^ resolution:' | {
targetdpi=$1 min=
elif command -v xdpyinfo >/dev/null 2>&1; then while read -r line; do
targetdpi=$(xdpyinfo | grep '^ resolution:' | { set -- $line
min= dpi_x=${2%%x*}
while read -r line; do dpi_y=${2##*x}
set -- $line dpi_sum=$(( $dpi_x + $dpi_y ))
dpi_x=${2%%x*} if [ -z "$min" ]; then
dpi_y=${2##*x} min=$dpi_sum
dpi_sum=$(( $dpi_x + $dpi_y )) else
if [ -z "$min" ]; then min=$(( $dpi_sum < $min ? $dpi_sum : $dpi ))
min=$dpi_sum
else
min=$(( $dpi_sum < $min ? $dpi_sum : $dpi ))
fi
done
if [ -n "$min" ]; then
echo "$min / 2" | bc -l
fi fi
}) done
if [ -n "$min" ]; then
echo "$min / 2" | bc -l
fi
})
if [ -z "$basedpi" ]; then
err "Can't find DPI"
fi fi
: ${targetdpi:=96}
targetdpi=${1:-96}
for facesize in $FACESIZES; do for facesize in $FACESIZES; do
echo "$BASEDPI * $facesize / $targetdpi" echo "$basedpi * $facesize / $targetdpi"
done | bc -l | { done | bc -l | {
i=0 i=0
printf "Xft.dpi:\t%.1f\n" "$targetdpi" printf "Xft.dpi:\t%.1f\n" "$targetdpi"
while read -r v; do while read -r v; do
if [ $i -eq 0 ]; then if [ $i -eq 0 ]; then
printf "XTerm.VT100.faceSize:\t%.1f\n" "$v" printf "XTerm.VT100.faceSize:\t%f\n" "$v"
else else
printf "XTerm.VT100.faceSize%d:\t%.1f\n" $i "$v" printf "XTerm.VT100.faceSize%d:\t%f\n" $i "$v"
fi fi
i=$(($i + 1)) i=$(($i + 1))
done done
} | xrdb -merge } | if $show; then
cat
else
xrdb -merge
fi