# Just another 8-bit 256-color helper script

It provides:
- environment variables 
 - for all 4-bit default colors (but as 8-bit code)
 - for escape and end codes

- functions: 
 - 8-bit ```colorgrid``` ([by plasmarob][1]) 
 - a 4-bit ```colornames``` grid 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.)*
 - ```colorhelp``` prints a colorful helptext to remind the user how to use alls this.

### Installation:
I store the script to ```/etc/profile.d/bash_colors.sh``` so that the functions and color-variables are available system-wide. 

To install it just for one user, store it to ~/.bash_colors and add following code to your ```~/.bash_aliases``` file *(which itself should be loaded within ~/.bashrc)*:
```
if [ -f ~/.bash_colors ]; then
 . ~/.bash_colors 
fi

```
### Screenshots
#### colorhelp
[![colorhelp][2]][2]
#### colorgrid
Also invokes colorhelp *(again, credits to plasmarob for the grid)*:
[![colorgrid (also invokes colorhelp)][3]][3]
#### colornames
What's interesting here is that the black isn't black at all. This is because I'm using an intercative remote shell and the client software decides how to interprete the 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.
[![colornames][4]][4]

### bash_colors.sh
```
#!/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;"
COL_BG=$ESC_SEQ"48;5;"

# standard colors
COL_BLACK=$COL_FG"0m"
COL_RED=$COL_FG"1m"
COL_GREEN=$COL_FG"2m"
COL_YELLOW=$COL_FG"3m"
COL_BLUE=$COL_FG"4m"
COL_MAGENTA=$COL_FG"5m"
COL_CYAN=$COL_FG"6m"
COL_WHITE=$COL_FG"7m"

# high intensity colors
COL_BRIGHT_BLACK=$COL_FG"8m"; COL_GRAY=$COL_BRIGHT_BLACK; COL_GREY=$COL_GRAY
COL_BRIGHT_RED=$COL_FG"9m"
COL_BRIGHT_GREEN=$COL_FG"10m"
COL_BRIGHT_YELLOW=$COL_FG"11m"
COL_BRIGHT_BLUE=$COL_FG"12m"
COL_BRIGHT_MAGENTA=$COL_FG"13m"
COL_BRIGHT_CYAN=$COL_FG"14m"
COL_BRIGHT_WHITE=$COL_FG"15m"


# background standard colors
COL_BG_BLACK=$COL_BG"0m"
COL_BG_RED=$COL_BG"1m"
COL_BG_GREEN=$COL_BG"2m"
COL_BG_YELLOW=$COL_BG"3m"
COL_BG_BLUE=$COL_BG"4m"
COL_BG_MAGENTA=$COL_BG"5m"
COL_BG_CYAN=$COL_BG"6m"
COL_BG_WHITE=$COL_BG"7m"

# background high intensity colors
COL_BG_BRIGHT_BLACK=$COL_BG"8m"; COL_BG_GRAY=$COL_BG_BRIGHT_BLACK; COL_BG_GREY=$COL_BG_GRAY
COL_BG_BRIGHT_RED=$COL_BG"9m"
COL_BG_BRIGHT_GREEN=$COL_BG"10m"
COL_BG_BRIGHT_YELLOW=$COL_BG"11m"
COL_BG_BRIGHT_BLUE=$COL_BG"12m"
COL_BG_BRIGHT_MAGENTA=$COL_BG"13m"
COL_BG_BRIGHT_CYAN=$COL_BG"14m"
COL_BG_BRIGHT_WHITE=$COL_BG"15m"


function printcolor() {
 local FG=$1
 local BG=$2

 fg=`eval echo "\$\{COL_$FG\}"`
 bg=`eval echo "\$\{COL_BG_$BG\}"`
 eval echo -en "$fg$bg"

 printf ' %-18s' $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 ""
 #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_%-15s' ${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_RED}\${ESC_SEQ}${COL_RESET} (${COL_MAGENTA}\\\033[${COL_RESET}) and"
 echo -e "add a color code from ${COL_MAGENTA}colorgrid()${COL_RESET}, "
 echo -e "or begin with ${COL_RED}\${COL_(RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN)}${COL_RESET}."
 echo -e "Close the text with $COL_RED\${COL_RESET}${COL_RESET} (${COL_MAGENTA}\\\033[0m${COL_RESET})"
 echo -en "\nExample:\n${COL_YELLOW}echo $COL_RED-e $COL_YELLOW\"$COL_RED"
 echo -en "Look: ${COL_MAGENTA}\${ESC_SEQ}${COL_BLUE}38;5;243m${COL_RED}This is dark grey text${COL_MAGENTA}\${COL_RESET}${COL_RED} and this is normal text."
 echo -e "$COL_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
}
```


 [1]: https://unix.stackexchange.com/questions/124407/what-color-codes-can-i-use-in-my-bash-ps1-prompt/285956#285956
 [2]: https://i.sstatic.net/NGlKO.png
 [3]: https://i.sstatic.net/RJhBu.png
 [4]: https://i.sstatic.net/0c5lH.png