Skip to content

Instantly share code, notes, and snippets.

@tyzbit
Last active August 10, 2020 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyzbit/4883ab0308e7aef687bce4512fdded80 to your computer and use it in GitHub Desktop.
Save tyzbit/4883ab0308e7aef687bce4512fdded80 to your computer and use it in GitHub Desktop.
Colorify
function colorify() {
#number=$(bc <<< "$(echo ${1} | od -An -vtu1 -w100000000 | tr -d ' ') % 28 + 1")
#number=$(bc <<< "ibase=16; $(echo ${1} | md5sum | awk '{print $1}'|tr '[[:lower:]]' '[[:upper:]]')" 2> /dev/null)
# md5sum is acceptable here because we don't need a strong hashing algo at all
# we just want the property of hashing algos that a given input always means the same output
hex=$(echo ${1} | md5sum | awk '{print $1}'|tr '[[:lower:]]' '[[:upper:]]')
# removing characters is done because zsh complains about the size of the number
# even though we do this, there's still more than enough 'randomness'
# doing this also ensures the value is positive which is cool
# you could remove this (:0:-20) in bash, just make the value absolute
number=$(bc <<< "$((16#${hex:0:-20})) % 28 + 1")
# acceptable ranges:
# 31-37
# 41-47
# 90-97
# 100-107
if [[ ${number} -gt 21 ]]; then let number=number+79-76; fi
if [[ ${number} -gt 14 ]]; then let number=number+76-34; fi
if [[ ${number} -gt 7 ]]; then let number=number+34-31; fi
if [[ ${number} -gt 0 ]]; then let number=number+30; fi
# these colors look bad
if [[ ${number} -eq 34 ]] || [[ ${number} -eq 47 ]] || [[ ${number} -eq 102 ]] || [[ ${number} -eq 106 ]]; then let number=number-1; fi
if [[ ${number} -eq 103 ]]; then let number=number+1; fi
#echo "number: ${number}"
echo -e "\e[${number}m${1}\e[0m"
}
@tyzbit
Copy link
Author

tyzbit commented Aug 10, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment