Add script for doing djb2-based term colorization

This commit is contained in:
Lucas 2021-05-29 14:01:03 +00:00
parent 7110e32037
commit 3ca523b0ee
1 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,41 @@
#!/bin/sh
# env
# Written in 2021 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
djb2()
{
_djb2_s=$1 _djb2_h=${2:-5381}
while [ ${#_djb2_s} -gt 0 ]; do
_djb2_c=$(printf "%u" "'${_djb2_s%${_djb2_s#?}}")
_djb2_s=${_djb2_s#?}
_djb2_h=$(( ((($_djb2_h << 5) + $_djb2_h) ^ $_djb2_c) & 65535 ))
done
printf "%u\n" $_djb2_h
}
hash_colorize()
{
_h=
while :; do
_h=$(djb2 "$1" $_h)
# removes bias in number selection. Taken from
# arc4random_uniform source code.
[ $_h -ge 4 ] && break
done
_h=$(( $_h % 12 ))
tput setaf $(( $_h < 6 ? 1 + $_h : 9 + $_h - 6 )) 0 0
}
for a; do
hash_colorize "$a"
printf "%s\n" "$a"
tput sgr0
done