Last active
June 9, 2023 13:21
-
-
Save the-moog/0a6281dd284749331516dbfceb3fe5c0 to your computer and use it in GitHub Desktop.
Bash shell scipt that prints lots of colous on an ANSI terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# rainbow | |
# A bash script that prints a nice set of colours on an ANSI terminal | |
# Written as a platform to test capability and conformance, but it's also pretty!! | |
# Written in celebration of Pride Month 2023 and support of LGBTQ+ | |
# J. Morgan (@the-moog on github) | |
# | |
# License: MIT: Free to the community not commercial gain and no warranties applied. | |
# | |
# Inspired by 'colors' that is included in the Cygwin of MobaXTerm | |
# Save this without the .sh extension. Only there as GitHub needs that to identify the syntax. | |
# Usage: | |
# rainbow [':' | ';' | '<text>'] | |
# Default text | |
T='=123ABC=' | |
# Some ANSI colours | |
AF_BLK=$'\e[30m' | |
AB_BLK=$'\e[40m' | |
AF_WHT=$'\e[37m' | |
AB_WHT=$'\e[47m' | |
AF_RED=$'\e[31m' | |
AB_RED=$'\e[41m' | |
AF_GRN=$'\e[32m' | |
AB_GRN=$'\e[42m' | |
AF_BLU=$'\e[34m' | |
AB_BLU=$'\e[44m' | |
AF_YEL=$'\e[33m' | |
AF_MAG=$'\e[35m' | |
AF_CYN=$'\e[36m' | |
AB_YEL=$'\e[43m' | |
AB_MAG=$'\e[45m' | |
AB_CYN=$'\e[46m' | |
A_RST=$'\e[m' | |
# Field separator & CLI | |
if [ "$1" == ':' ] || [ "$1" == ';' ]; then | |
S=$1 | |
else | |
S=';' | |
[ -n "$1" ] && T="$1" | |
fi | |
function pride () { | |
# $1 a string | |
# $2 [starting colour index (0 to 7)] | |
# $3 [fg or bg] | |
# outputs a rainbow coloured version of the string | |
# changes fg or bg, default both | |
# if $2 is omitted or '-' it uses the last color | |
str="$1" | |
[ -z "$last_pride" ] && last_pride=$RANDOM | |
if [ -z "$2" ] || [ "$2" == '-' ]; then | |
sci=$last_pride | |
else | |
sci=$(($2 % 8)) | |
fi | |
if [ "$3" == "bg" ] || [ "$3" == "fg" ]; then | |
only="$3"; | |
else | |
only='' | |
fi | |
for ((i=0;i<${#str};i++)); do | |
fg=$((31+(($sci + $i) % 7))) | |
bg=$((41+(($sci + $i) % 7))) | |
[ "$only" == "fg" ] && bg=$fg | |
[ "$only" == "bg" ] && fg=$bg | |
[ "$only" == '' ] && [ $fg -eq $bg ] && bg=$((41+(($bg - 40)%8))) | |
printf "\e[1;%s;%sm%c" "$fg" "$bg" "${str:$i:1}" | |
done | |
} | |
# Clear screen | |
clear | |
# Screen Geometry | |
COLS=$(tput cols) | |
ROWS=$(tput lines) | |
CW=$(((${COLS}/10) - 3)) | |
PAD=$((($CW - ${#T}) / 2)) | |
[ $PAD -lt 0 ] && $PAD=0 | |
PT=$(printf "% ${PAD}.${PAD}s" ' ') | |
T="${PT}${T}${PT}" | |
function setBGColor24bit () { | |
# Select a 24bit RGB colour | |
# $1, $2, $3 = R,G,B | |
printf "\e[48${S}2${S}%s${S}%s${S}%sm" $1 $2 $3 | |
} | |
declare -a FGLIST | |
declare -a BGLIST | |
function ansiText () { | |
printf "3 bit colors:\n" | |
local BG | |
local FG | |
for FG in 0 {30..37}; do | |
FGLIST+=(${FG}) | |
FGLIST+=(1${S}${FG}) | |
done | |
for BG in 0 {40..47}; do | |
BGLIST+=(${BG}) | |
done | |
# Header row | |
printf "% $((${CW}-4)).$((${CW}-4))s" | |
for BG in "${BGLIST[@]}"; do | |
printf "% $((${CW}+1)).$((${CW}+1))sm" "$BG" | |
done | |
printf "\n" | |
# Text | |
for FG in "${FGLIST[@]}"; do | |
printf "% ${CW}.${CW}sm\e[%sm" "$FG" $FG | |
for BG in ${BGLIST[@]}; do | |
printf "\e[%sm\e[%sm" $FG $BG | |
TXT=$(pride "${T}" - fg) | |
printf " ${TXT}${A_RST} " | |
done | |
printf "\n" | |
done | |
} | |
function pvars () { | |
printf "PAD:$PAD CW:$CW FG:${#FGLIST[@]} BG:${#BGLIST[@]}\n\n" | |
} | |
function bands () { | |
bc=$(((256/$COLS)+1)) | |
printf "24 bit colours:\n" | |
# Red | |
for ((i=0;i<255;i+=$bc)); do | |
setBGColor24bit $i 0 0 | |
printf ' ' | |
done | |
printf "\e[0m\n" | |
# Green | |
for ((i=0;i<255;i+=$bc)); do | |
setBGColor24bit 0 $i 0 | |
printf ' ' | |
done | |
printf "\e[0m\n" | |
# Blue | |
for ((i=0;i<255;i+=$bc)); do | |
setBGColor24bit 0 0 $i | |
printf ' ' | |
done | |
printf "\e[0m\n" | |
# Grey | |
for ((i=0;i<255;i+=$bc)); do | |
setBGColor24bit $i $i $i | |
printf ' ' | |
done | |
printf "\e[0m\n" | |
} | |
function grid () { | |
nr=25 | |
nb=12 | |
bc=$((($COLS)/($nb+2))) | |
printf "24 bit colours:\n" | |
for ((r=0;r<255;r+=$((256/($nr - 1))))); do | |
for ((g=0;g<255;g+=$((256/$nb)))); do | |
for ((b=255;b>0;b-=$((256/$bc)))); do | |
setBGColor24bit $r $g $b | |
printf ' ' | |
done | |
done | |
# end of line, reset | |
printf "\e[0m\n" | |
done | |
} | |
function tests () { | |
echo ":${TXT}${A_RST}:" | |
echo ":$T:" | |
pvars | |
} | |
grid | |
ansiText | |
bands |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment