Last active
January 19, 2023 06:48
-
-
Save unacceptable/c4583e81af7fcd26fa9767ad78b12c7b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
set -e | |
# ANSI color table that relates to RGB (NOT true RGB) | |
# Written by: Robert J. | |
# _/ _/ _/ _/ _/ | |
# _/_/_/_/_/ _/ _/_/_/ _/_/_/ _/_/_/ _/_/_/ | |
# _/ _/ _/ _/ _/ _/ _/ _/_/ _/ _/ | |
# _/_/_/_/_/ _/ _/ _/ _/ _/_/ _/ _/ | |
# _/ _/ _/ _/_/_/ _/_/_/ _/_/_/ _/ _/ | |
####################################### | |
### Main Functions #################### | |
####################################### | |
main_function(){ | |
[[ $1 == "--grey" || $1 == "--gray" ]] && | |
greydisptable; | |
[[ $1 == "--table" ]] && | |
disptable; | |
[[ $1 == "--full-table" ]] && | |
predisptable && | |
disptable && | |
printf "\n" && | |
greydisptable | |
helpcheck "$@"; | |
die "" 0; | |
}; | |
####################################### | |
### Generic Functions ################# | |
####################################### | |
helpcheck(){ | |
for arg in "$@"; do | |
[[ "$arg" == "--help" || "$arg" == "-h" ]] && | |
disphelp; | |
die "" 0 --nosep | |
done | |
if [[ -z "${1}" ]]; then | |
disphelp && | |
die "Invalid Syntax" 1; | |
else | |
return 0; | |
fi | |
}; | |
disphelp(){ | |
print "Command Documentation" green --nosep | |
print "Usage Examples:" yellow; | |
printf "\t%s --help | -h \n\t# Displays this table for reference\n\n" "${0##*/}" | |
printf "\t%s --table \n\t# Outputs table of 216 escaped colors with approx RGB\n\n" "${0##*/}" | |
printf "\t%s --full-table \n\t# Outputs all 255 colors\n\n" "${0##*/}" | |
printf "\t%s --grey | --gray \n\t# Outputs greyscale color codes\n" "${0##*/}" | |
print "Options:" yellow; | |
printf "\t--help | -h \t# Displays this table for reference\n" | |
printf "\t--table \t# Outputs table of 216 escaped colors with approx RGB\n" | |
printf "\t--full-table \t# Outputs all 255 colors\n" | |
printf "\t--grey | --gray \t# Outputs greyscale color codes\n" | |
} | |
print(){ | |
# opted against $_ and $! for this usage | |
last="${!#}" | |
output="$1"; | |
size="${#output}"; | |
# Colors calculated from ansi-color script | |
########################################## | |
case "$2" in | |
red) | |
color="196" | |
;; | |
orange) | |
color="202" | |
;; | |
yellow) | |
color="226" | |
;; | |
green) | |
color="46" | |
;; | |
blue) | |
color="27" | |
;; | |
indigo) | |
color="21" | |
;; | |
violet) | |
color="54" | |
;; | |
grey) | |
color="8" | |
;; | |
*) | |
color="8" | |
;; | |
esac | |
printf -v sep "%${size}s" "-"; | |
[[ "$last" == "--nosep" ]] && { | |
printf "\e[38;5;%dm%s\e[0m\n" "$color" "$output"; | |
return 0; | |
}; | |
printf "\n\e[38;5;%dm%s\e[0m\n%s\n" "$color" "$output" "${sep// /-}"; | |
}; | |
die(){ | |
message="${1}" | |
exit_code="${2}" | |
[[ -z "$exit_code" ]] && { | |
exit_code=1; | |
}; | |
# determine if exit code is an integer | |
[[ -z ${exit_code//[0-9]/} ]] || { | |
print "Invalid exit code specified in script: $exit_code\n" yellow --nosep | |
exit_code=255; | |
}; | |
[[ "$exit_code" -gt 0 ]] && { | |
print "Error Encountered:" "red"; | |
}; | |
print "${message}" "yellow" "--nosep"; | |
exit "$exit_code"; | |
}; | |
####################################### | |
### Program Specific Functions ######## | |
####################################### | |
disptable(){ | |
print "More complex color codes:" | |
# Set variables to 0 | |
r=0 | |
g=0 | |
b=0 | |
# Initiate while loop | |
while : ; | |
do | |
# Starting with red (RGB) | |
for ((r=0;r<6;r++)); | |
do | |
# Perform color calculations | |
color=$((16+36*r+6*g+b)); | |
# Print Color with RGB structure tabbing at the completion | |
printf "\e[38;5;%dm%s,%s,%s;\e[0m color code: %s \t" "$color" "$r" "$g" "$b" "$color"; | |
done; | |
# Increase blue by one increment | |
((b++)) && { | |
# Reset blue to zero if it equals 6 | |
[[ $b = 6 ]] && b=0 && { | |
# Increase green by one increment until it equals 6 at which point, break the while loop | |
((g++)) && [[ $g = 6 ]] && break; | |
} | |
} | |
# perform a line return after the first set of red is completed | |
printf "\n" | |
done; | |
} | |
predisptable(){ | |
print "Simple color codes:" | |
for c in {1..15}; do | |
printf "\e[38;5;%dmx,x,x;\e[0m color code: %s\n" "$c" "$c"; | |
done | |
} | |
greydisptable(){ | |
print "Greyscale color codes:" | |
for c in {232..255}; do | |
printf "\e[38;5;%dmx,x,x;\e[0m color code: %s\n" "$c" "$c"; | |
done | |
} | |
####################################### | |
### Execution ######################### | |
####################################### | |
main_function "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just a simple bash script that I whipped up to help determine the perfect ANSI Color code for other scripts: