Skip to content

Instantly share code, notes, and snippets.

@kamontat
Last active April 2, 2020 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kamontat/717f75e6b87606940017adf385274044 to your computer and use it in GitHub Desktop.
Save kamontat/717f75e6b87606940017adf385274044 to your computer and use it in GitHub Desktop.
COLOR CONSTANTS (BASH)

Develop

  1. clone this project by git clone https://gist.github.com/717f75e6b87606940017adf385274044.git color
  2. run cd color
  3. write the code as what you want
    • color_constants.sh - new version of constant (using tput)
    • color_raw_constants.sh - old version of constant (using raw text to assign)
      • some terminal app, might miss the color.
    • install.sh - install color script

Run

  • run by make install root runable make computer can run install.bash
    1. run chmod 755 ./install.bash
    2. run ./install.sh
  • run by source ./install.sh

Instant Import

use source command to import this project

  1. This easiest way is download this project and import link this source ./color_constants
  2. (don't work on bash3)
source <(curl -sL -N https://gist.githubusercontent.com/kamontat/717f75e6b87606940017adf385274044/raw/e94f7f1092e9b81dbe5d34845813af9158260c99/color_constants.sh)
  1. (work in all commandline)
source /dev/stdin <<< "$(curl -sL -N https://gist.githubusercontent.com/kamontat/717f75e6b87606940017adf385274044/raw/e94f7f1092e9b81dbe5d34845813af9158260c99/color_constants.sh)"

result / expected

  1. make all file are root readable (755 / +x)
    • easier by run install.sh

How to know constants?

  1. Saparator is _
  2. All is UPPERCASE
color Constants Code
Black BLACK
Red RED
Green GREEN
Yellow YELLOW
Blue BLUE
Pink PINK        
Cyan CYAN
White WHITE

Special Charactor

  1. BOLD Bold Text
  2. UNDERLINE Underlined Text
  3. REVERSE Reverse video Text (support some app / OS)
  4. BLINK Blinking Text (support some app / OS)
  5. INVISIBLE Invisible Text (support some app / OS)
  6. STANDOUT Standout Mode (support some app / OS)
Special Constants Code
Bold BO
Underline UL
Reverse RV
Blink BL
Standout Mode SM
Invisible IV

Reset Variable

Example Usage

More

  • This project have 1 file call tester, that will log all possible color from 0:0 => 110:110 so that many of line, Careful to use it!
#!/bin/bash
# -----------------------------
# doc type
# -----------------------------
# bold Start bold text
# smul Start underlined text
# rmul End underlined text
# rev Start reverse video
# blink Start blinking text
# invis Start invisible text
# smso Start "standout" mode
# rmso End "standout" mode
# sgr0 Turn off all attributes
# setaf <value> Set foreground color
# setab <value> Set background color
HELP="
usage: source color_constant.sh
-------------------------------
test: ./color_constant.sh test
-------------------------------
create by Kamontat Chantrachirathumrong
since 09/06/60-16:09 (dd/mm/yy-mm:ss)
version 1.0
"
if [[ $1 == "help" || $1 == "h" ]]; then
echo "$HELP"
exit 0
fi
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
else
echo "not support tput, use color_raw_constant.sh instead."
exit 2
fi
# print color bit layout
# echo "$(tput longname)"
# echo "you command line have $ncolors colors"
# echo " mean your both C_FG_XXX and C_BG_XXX"
# echo " XXX = 1 - $ncolors"
DEFAULT="C_"
FORGROUND="FG_"
BACKGROUND="BG_"
# temp variable
temp=
# extra word
C_BO="$(tput bold)"
C_DI="$(tput dim)"
C_UL="$(tput smul)"
C_RV="$(tput rev)"
C_SM="$(tput smso)"
C_BL="$(tput blink)"
C_IV="$(tput invis)"
# reset variable
C_RE_AL="$(tput sgr0)"
C_RE_SM="$(tput rmso)"
C_RE_UL="$(tput rmul)"
# example
# programmer=Ines
# declare $programmer="nice gal"
# echo $Ines # echos nice gal
# declare forground color
for (( i=0; i<=ncolors; i++ )); do
temp="$DEFAULT$FORGROUND$i"
# echo "$temp"
declare $temp="$(tput setaf $i)"
done
# declare background color
for (( i=0; i<=ncolors; i++ )); do
temp="$DEFAULT$BACKGROUND$i"
declare $temp="$(tput setab $i)"
done
# tester
if [[ $1 == "test" ]]; then
echo "${C_FG_3}front color 3 ${C_RE_AL}"
echo "${C_FG_1}front color 1 ${C_RE_AL}"
echo "${C_FG_7}front color 7 ${C_RE_AL}"
echo "${C_FG_2}front color 2 ${C_RE_AL}"
echo "${C_BG_4}back color 4 ${C_RE_AL}"
echo "${C_BG_5}back color 5 ${C_RE_AL}"
echo "${C_BO}${C_FG_5}BOLD+F5 ${C_RE_AL}"
echo "${C_BO}${C_FG_7}${C_BG_3}Bold+F7+B3${C_RE_AL}"
echo "${C_BL}${C_UL}${C_FG_1}Blink+Underline+F1${C_RE_UL} disable underline${C_RE_AL}"
fi
#!/bin/bash
# constants project: https://gist.github.com/717f75e6b87606940017adf385274044.git
# creator: Kamontat Chantrachirathumrong
# Reset
RESET='\033[0m' # Text Reset
# Special Colors
C='\033[0;2m' # Default color setting
CB='\033[0;1m' # Default color setting and bright a little bit
# Regular Colors
BLACK='\033[0;30m' # Black
RED='\033[0;31m' # Red
GREEN='\033[0;32m' # Green
YELLOW='\033[0;33m' # Yellow
BLUE='\033[0;34m' # Blue
PINK='\033[0;35m' # Pink
CYAN='\033[0;36m' # Cyan
WHITE='\033[0;37m' # White
# Bold
B_BLACK='\033[1;30m' # Black
B_RED='\033[1;31m' # Red
B_GREEN='\033[1;32m' # Green
B_YELLOW='\033[1;33m' # Yellow
B_BLUE='\033[1;34m' # Blue
B_PINK='\033[1;35m' # Pink
B_CYAN='\033[1;36m' # Cyan
B_WHITE='\033[1;37m' # White
# Underline
U_BLACK='\033[4;30m' # Black
U_RED='\033[4;31m' # Red
U_GREEN='\033[4;32m' # Green
U_YELLOW='\033[4;33m' # Yellow
U_BLUE='\033[4;34m' # Blue
U_PINK='\033[4;35m' # Pink
U_CYAN='\033[4;36m' # Cyan
U_WHITE='\033[4;37m' # White
# Background
BG_BLACK='\033[40m' # Black
BG_RED='\033[41m' # Red
BG_GREEN='\033[42m' # Green
BG_YELLOW='\033[43m' # Yellow
BG_BLUE='\033[44m' # Blue
BG_PINK='\033[45m' # Pink
BG_CYAN='\033[46m' # Cyan
BG_WHITE='\033[47m' # White
# High Intensity
HI_BLACK='\033[0;90m' # Black
HI_RED='\033[0;91m' # Red
HI_GREEN='\033[0;92m' # Green
HI_YELLOW='\033[0;93m' # Yellow
HI_BLUE='\033[0;94m' # Blue
HI_PINK='\033[0;95m' # Pink
HI_CYAN='\033[0;96m' # Cyan
HI_WHITE='\033[0;97m' # White
# Bold High Intensity
B_HI_BLACK='\033[1;90m' # Black
B_HI_RED='\033[1;91m' # Red
B_HI_GREEN='\033[1;92m' # Green
B_HI_YELLOW='\033[1;93m' # Yellow
B_HI_BLUE='\033[1;94m' # Blue
B_HI_PINK='\033[1;95m' # Pink
B_HI_CYAN='\033[1;96m' # Cyan
B_HI_WHITE='\033[1;97m' # White
# High Intensity backgrounds
BG_HI_BLACK='\033[0;100m' # Black
BG_HI_RED='\033[0;101m' # Red
BG_HI_GREEN='\033[0;102m' # Green
BG_HI_YELLOW='\033[0;103m' # Yellow
BG_HI_BLUE='\033[0;104m' # Blue
BG_HI_PINK='\033[0;105m' # Pink
BG_HI_CYAN='\033[0;106m' # Cyan
BG_HI_WHITE='\033[0;107m' # White
# Blink
BLINK_BLACK='\033[5;30m' # Black
BLINK_RED='\033[5;31m' # Red
BLINK_GREEN='\033[5;32m' # Green
BLINK_YELLOW='\033[5;33m' # Yellow
BLINK_BLUE='\033[5;34m' # Blue
BLINK_PINK='\033[5;35m' # Pink
BLINK_CYAN='\033[5;36m' # Cyan
BLINK_WHITE='\033[5;37m' # White
#!/bin/bash
cd "$(dirname "$0")"
chmod 755 ./color_constants.sh # new version
chmod 755 ./color_raw_constants.sh # old version
source ./color_constants.sh
exit_code=$?
if [ $exit_code -eq 2 ]; then
exit 2
elif [ $exit_code -ne 0 ]; then
echo "install wrong with some reason"
exit $exit_code
fi
printf "\nDo you want to minify folder[Y|n]? "
read -n 1 ans
echo ""
if [[ $ans == "Y" ]]; then
rm -rf ./README.md ./install.sh
fi
printf "${C_BG_5}${C_FG_7}complete!$C_RE\n"
@kamontat
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment