environment variables
- for all 4-bit default colors (but as 8-bit code).
- for escape and end codes
functions:
- 8-bit
colorgrid(by plasmarob) - a 4-bit
colornamesgrid displaying colors and backrounds with the color names (I have to admit the variables have a prefix which is not displayed. I thought about removing the prefixes later.) colorhelpprints a colorful helptext to remind the user how to use alls this.
- 8-bit
What's interesting here isNote that the 4-bit black isn't necessarily pitch black at all. This is because I'm using an intercative remote shell and the client software decides how to interpreteinterpret the 4-bit colors. In my case, MobaXterm provides some different color palettes and this is the default. That means by configuring your client, you can adjust the color palette for the codes in the range of 0-15 completely. 

#!/bin/bash # Adds some color helpers. # 8-bit 256-color lookup table # 2022, Justus Kenklies # # credits: # based on colorgrid function by plasmarob: # https://unix.stackexchange.com/questions/124407/what-color-codes-can-i-use-in-my-bash-ps1-prompt/285956#285956 # # Escape codes ESC_SEQ="\033[" COL_RESET=$ESC_SEQ"0m" COL_FG=$ESC_SEQ"38;5;"FG=$ESC_SEQ"38;5;" COL_BG=$ESC_SEQ"48;5;"BG=$ESC_SEQ"48;5;" # standard colors COL_BLACK=$COL_FG"0m"BLACK=$FG"0m" COL_RED=$COL_FG"1m"RED=$FG"1m" COL_GREEN=$COL_FG"2m"GREEN=$FG"2m" COL_YELLOW=$COL_FG"3m"YELLOW=$FG"3m" COL_BLUE=$COL_FG"4m"BLUE=$FG"4m" COL_MAGENTA=$COL_FG"5m"MAGENTA=$FG"5m" COL_CYAN=$COL_FG"6m"CYAN=$FG"6m" COL_WHITE=$COL_FG"7m"WHITE=$FG"7m" # high intensity colors COL_BRIGHT_BLACK=$COL_FG"8m";BRIGHT_BLACK=$FG"8m"; COL_GRAY=$COL_BRIGHT_BLACK;GRAY=$BRIGHT_BLACK; COL_GREY=$COL_GRAYGREY=$GRAY COL_BRIGHT_RED=$COL_FG"9m"BRIGHT_RED=$FG"9m" COL_BRIGHT_GREEN=$COL_FG"10m"BRIGHT_GREEN=$FG"10m" COL_BRIGHT_YELLOW=$COL_FG"11m"BRIGHT_YELLOW=$FG"11m" COL_BRIGHT_BLUE=$COL_FG"12m"BRIGHT_BLUE=$FG"12m" COL_BRIGHT_MAGENTA=$COL_FG"13m"BRIGHT_MAGENTA=$FG"13m" COL_BRIGHT_CYAN=$COL_FG"14m"BRIGHT_CYAN=$FG"14m" COL_BRIGHT_WHITE=$COL_FG"15m"BRIGHT_WHITE=$FG"15m" # background standard colors COL_BG_BLACK=$COL_BG"0m"BG_BLACK=$BG"0m" COL_BG_RED=$COL_BG"1m"BG_RED=$BG"1m" COL_BG_GREEN=$COL_BG"2m"BG_GREEN=$BG"2m" COL_BG_YELLOW=$COL_BG"3m"BG_YELLOW=$BG"3m" COL_BG_BLUE=$COL_BG"4m"BG_BLUE=$BG"4m" COL_BG_MAGENTA=$COL_BG"5m"BG_MAGENTA=$BG"5m" COL_BG_CYAN=$COL_BG"6m"BG_CYAN=$BG"6m" COL_BG_WHITE=$COL_BG"7m"BG_WHITE=$BG"7m" # background high intensity colors COL_BG_BRIGHT_BLACK=$COL_BG"8m";BG_BRIGHT_BLACK=$BG"8m"; COL_BG_GRAY=$COL_BG_BRIGHT_BLACK;BG_GRAY=$BG_BRIGHT_BLACK; COL_BG_GREY=$COL_BG_GRAYBG_GREY=$BG_GRAY COL_BG_BRIGHT_RED=$COL_BG"9m"BG_BRIGHT_RED=$BG"9m" COL_BG_BRIGHT_GREEN=$COL_BG"10m"BG_BRIGHT_GREEN=$BG"10m" COL_BG_BRIGHT_YELLOW=$COL_BG"11m"BG_BRIGHT_YELLOW=$BG"11m" COL_BG_BRIGHT_BLUE=$COL_BG"12m"BG_BRIGHT_BLUE=$BG"12m" COL_BG_BRIGHT_MAGENTA=$COL_BG"13m"BG_BRIGHT_MAGENTA=$BG"13m" COL_BG_BRIGHT_CYAN=$COL_BG"14m"BG_BRIGHT_CYAN=$BG"14m" COL_BG_BRIGHT_WHITE=$COL_BG"15m"BG_BRIGHT_WHITE=$BG"15m" function printcolor() { local FG=$1 local BG=$2 fg=`eval echo "\$\{COL_$FG\$FG\}"` bg=`eval echo "\$\{COL_BG_$BG\BG_$BG\}"` eval echo -en "$fg$bg" printf ' %$%-18s'17s' $FG; echo -en "${COL_RESET}" } #lower backgrounds function colornames() { local colors=(BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE) local lowup=() lowup[0]="_" lowup[1]="_BRIGHT_" local fgcolor bgcolor echo """List of color variables. " #table for bgbright in ${lowup[@]} do #echo "writing ${bgbright:1} bg colors" #column header for bg in ${colors[@]} do # echo -en "\$$bg\t" printf ' BG_%$BG_%-15s'14s' ${bgbright:1}$bg; done # line feed: echo "" for fgbright in ${lowup[@]} do #echo "writing ${fgbright:1} fg colors" for fg in ${colors[@]} do fgcolor=${fgbright:1}$fg for bg in ${colors[@]} do bgcolor=${bgbright:1}$bg printcolor $fgcolor $bgcolor done # line feed: echo "" done done echo "" done } function colorhelp() { echo -e "\nTo write colored text, either begin with ${COL_REDRED}\${ESC_SEQ}${COL_RESET} (${COL_MAGENTAMAGENTA}\\\033[${COL_RESET}) and" echo -e "add a color code from ${COL_MAGENTAMAGENTA}colorgrid()${COL_RESET}, " echo -e "or begin with ${COL_REDRED}\${COL_(RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN)}${COL_RESET}." echo -e "Close the text with $COL_RED\$$RED\${COL_RESET}${COL_RESET} (${COL_MAGENTAMAGENTA}\\\033[0m${COL_RESET})" echo -en "\nExample:\n${COL_YELLOWYELLOW}echo $COL_RED$RED-e $COL_YELLOW\"$COL_RED"$YELLOW\"$RED" echo -en "Look: ${COL_MAGENTAMAGENTA}\${ESC_SEQ}${COL_BLUEBLUE}38;5;243m${COL_REDRED}This is dark grey text${COL_MAGENTAMAGENTA}\${COL_RESET}${COL_REDRED} and this is normal text." echo -e "$COL_YELLOW\"$COL_RESET""$YELLOW\"$COL_RESET" echo -e "Look: ${ESC_SEQ}38;5;243mThis is dark gray text${COL_RESET} and this is normal text.\n" } function colorgrid() { iter=16 while [ $iter -lt 52 ]; do second=$[$iter+36] third=$[$second+36] four=$[$third+36] five=$[$four+36] six=$[$five+36] seven=$[$six+36] if [ $seven -gt 250 ];then seven=$[$seven-251]; fi echo -en "\033[38;5;$(echo $iter)m█ " printf "%03d" $iter echo -en " \033[38;5;$(echo $second)m█ " printf "%03d" $second echo -en " \033[38;5;$(echo $third)m█ " printf "%03d" $third echo -en " \033[38;5;$(echo $four)m█ " printf "%03d" $four echo -en " \033[38;5;$(echo $five)m█ " printf "%03d" $five echo -en " \033[38;5;$(echo $six)m█ " printf "%03d" $six echo -en " \033[38;5;$(echo $seven)m█ " printf "%03d" $seven iter=$[$iter+1] printf '\r\n' done echo -e "$COL_RESET" echo "Example for color 153:" echo -e "echo -e \"\033[38;5;153m\\\033[38;5;153mHello World\\\033[0m\033[0m\"" colorhelp } 
