Skip to content

Instantly share code, notes, and snippets.

@walkerdb
Last active March 29, 2021 21:52
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 walkerdb/405ebb165f5df7bdde0e44b59134a003 to your computer and use it in GitHub Desktop.
Save walkerdb/405ebb165f5df7bdde0e44b59134a003 to your computer and use it in GitHub Desktop.
# this logic is based on the "using out-of-phase sine waves to make rainbows" section described
# here https://krazydad.com/tutorials/makecolors.php, implemented in bash.
# Uses the more modern rgb escape code sequence (ESC[38;2;{red};{green};{blue}m), so will only work on newer terminals
# It's uh, very slow
function print_rainbow {
local text_to_print=$1
local result=""
local frequency
frequency=$(echo "3.14159 * 1.8 / ${#text_to_print}" | bc -l)
for (( i=0; i<${#text_to_print}; i++ )); do
red=$(echo "s(${frequency}*$i + 0) * 127 + 128" | bc -l | cut -d "." -f 1)
green=$(echo "s(${frequency}*$i + 2) * 127 + 128" | bc -l | cut -d "." -f 1)
blue=$(echo "s(${frequency}*$i + 4) * 127 + 128" | bc -l | cut -d "." -f 1)
result="$result\033[38;2;${red};${green};${blue}m${text_to_print:$i:1}\033[0m"
done
echo -e "$result"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment