I have some ascii banners saved to plain text files that I use for different things and have used various ways to randomize the colors, but I haven’t come up with a way to give every character a different color yet. I’m thinking there’s probably a way to do an array to make it happen, but can’t seem to quite get there – I’m aware of things like toilet and lolcat but I’m not able to use those currently (using centos). Here’s what I’ve done so far:
change each file to a random color:
echo -e "e[38;5;$(shuf -i 1-255 -n 1)m$(<ascii/gc)e[0m"
you can do this with $RANDOM too:
echo -e "e[38;5;$(( $RANDOM % 256 ))m$(<ascii/gc)e[0m"
change each line in the file to a different color:
while IFS= read -r line; do
echo -e "e[38;5;$(shuf -i 1-255 -n 1)m${line}e[0m";
done<ascii/gc
These files use multiple rows like so:
$$$$$$ $$ $$$$$$ $$ $$ $$
$$ __$$ $$ | $$ __$$ $$ | __| $$ |
$$ / __| $$$$$$ $$$$$$ $$$$$$$ | $$ / __|$$$$$$$ $$$$$$ $$ $$$$$$$ $$$$$$ $$ |
$$ |$$$$ $$ __$$ $$ __$$ $$ __$$ | $$ | $$ __$$ $$ __$$ $$ |$$ _____|$$ __$$ $$ |
$$ |_$$ |$$ / $$ |$$ / $$ |$$ / $$ | $$ | $$ | $$ |$$ / $$ |$$ |$$ / $$$$$$$$ |__|
$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ | $$ | $$ $$ | $$ |$$ | $$ |$$ |$$ | $$ ____|
$$$$$$ |$$$$$$ |$$$$$$ |$$$$$$$ | $$$$$$ |$$ | $$ |$$$$$$ |$$ |$$$$$$$ $$$$$$$ $$
______/ ______/ ______/ _______| ______/ __| __| ______/ __| _______| _______|__|
Thanks in advance!
3
Answers
You can simply read character by character instead of line by line:
This is faster and more robust with printf and RANDOM:
My interpretation was printing each block letter in a different color. It required adding a separator to the input file (It looks the same, but I actually inserted zero-width space characters).
Then an
awk
one-liner can put color markings before and after each field, separated by the ZWS:'xe2x80x8b'
. It was necessary to use an external seed to get different colors at each run.