Skip to content

Instantly share code, notes, and snippets.

@yukal
Last active November 4, 2018 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukal/99d7c3f10403271a2b89df4c470cd0b3 to your computer and use it in GitHub Desktop.
Save yukal/99d7c3f10403271a2b89df4c470cd0b3 to your computer and use it in GitHub Desktop.
Bash Colors and Text Style Demonstration
#!/bin/bash
# Bash Color and Style Demonstration
# Author: Yukal Alexander <yukal@email.ua>
# License MIT
# Version: 3.0
#
# UTF-8 encoding table and Unicode characters
# https://www.utf8-chartable.de/unicode-utf8-table.pl?start=9472&unicodeinhtml=dec
# brick="■"
brick="██"
brick2="▓▓"
brick3="▒▒"
brick4="░░"
# empty="✕"
empty="◉"
reset() { printf "\033[m"; }
wrap() { printf " \033[$1\033[0m"; }
color256() {
local color=2;
local str=$brick
if [ -n "$1" ]; then color=$1; fi
if [ -n "$2" ]; then str=$2; fi
printf "\033[38;05;${color}m${str}\033[m";
}
table_c16() {
for color in {0..15}; do
if (($color % 8 == 0)); then echo; fi
color256 $color $1;
if (($color == 6)); then printf " $(color256 8 $1)"; fi
if (($color == 14)); then printf " $(color256 7 $1)"; fi
done
echo ;
}
grayscale() {
printf " $(color256 8 $1) $(color256 7 $1) $(color256 15 $1)";
echo ;
color256 0 $1;
for ((color=232; color<256; color++)); do
color256 $color $1;
done
color256 15 $1;
echo -e "\n";
}
triplex() {
if (( $1 < 10 )); then
out="00"$1;
elif (( $1 < 100 )); then
out="0"$1;
else
out=$1
fi
echo $out
}
table1() {
for (( tab=0; tab<6; tab++ )); do
for (( row=0; row<6; row++ )); do
for (( col=1; col<7; col++ )); do
color=$(( 15+(6*$tab)+($row*36)+$col ))
if [ "$1" == "1" ]; then
color256 $color $(triplex $color)" "
else
color256 $color $1
fi
done
done
echo ;
done
echo ;
}
table2() {
for (( tab=0; tab<6; tab++ )); do
for (( col=1; col<7; col++ )); do
for (( row=0; row<6; row++ )); do
if (( ($color-16) % 6 == 0 )); then
color=$(( 15+(6*$tab)+($row*36)+$col ))
else
color=$(( 15+(6*$tab)+($row*36)+$col ))
fi
if [ "$1" == "1" ]; then
color256 $color $(triplex $color)" "
else
color256 $color $1
fi
done
done
echo ;
done
echo ;
}
rgb_matrix() {
local row1=( 016 052 088 124 160 196 016 017 018 019 020 021 016 017 018 019 020 021 016 232 233 234 235 )
local row2=( 022 058 094 130 166 202 052 053 054 055 056 057 022 023 024 025 026 027 236 237 238 239 240 )
local row3=( 028 064 100 136 172 208 088 089 090 091 092 093 028 029 030 031 032 033 241 242 243 244 245 )
local row4=( 034 070 106 142 178 214 124 125 126 127 128 129 034 035 036 037 038 039 246 247 248 249 250 )
local row5=( 040 076 112 148 184 220 160 161 162 163 164 165 040 041 042 043 044 045 251 252 253 254 255 )
local row6=( 046 082 118 154 190 226 196 197 198 199 200 201 046 047 048 049 050 051 -1 -1 -1 -1 015 )
for (( r=1; r<7; r++ )); do
ARRAYNAME="row${r}"
eval row=\( \${${ARRAYNAME}[@]} \)
# echo "${row[@]} ${ARRAYNAME}[${#row[@]}]"
for (( col=0; col<${#row[@]}; col++ )); do
if [ "$1" == "1" ]; then
if [ "${row[$col]}" == "-1" ]; then
color256 235 "${empty}${empty}${empty} ";
else
color256 "${row[$col]}" "${row[$col]} ";
fi
else
if [ "${row[$col]}" == "-1" ]; then
color256 235 "${empty}${empty}";
else
color256 "${row[$col]}" "$1";
fi
fi
done
echo ;
done
echo ;
}
rgb_matrix2() {
local row1=( 051 045 039 033 027 021 235 240 245 250 255 )
local row2=( 050 044 038 032 026 020 234 239 244 249 254 )
local row3=( 049 043 037 031 025 019 233 238 243 248 253 )
local row4=( 048 042 036 030 024 018 232 237 242 247 252 )
local row5=( 047 041 035 029 023 017 016 236 241 246 251 )
local row6=( 046 040 034 028 022 016 017 018 019 020 021 )
local row7=( 082 076 070 064 058 052 053 054 055 056 057 )
local row8=( 118 112 106 100 094 088 089 090 091 092 093 )
local row9=( 154 148 142 136 130 124 125 126 127 128 129 )
local row10=( 190 184 178 172 166 160 161 162 163 164 165 )
local row11=( 226 220 214 208 202 196 197 198 199 200 201 )
for r in {1..11}; do
ARRAYNAME="row${r}"
eval row=\( \${${ARRAYNAME}[@]} \)
# echo "${row[@]} ${ARRAYNAME}[${#row[@]}]"
for (( col=0; col<${#row[@]}; col++ )); do
if [ "$1" == "1" ]; then
printf "\e[38;05;${row[$col]}m${row[$col]}\033[m ";
else
printf "\e[38;05;${row[$col]}m${1}\033[m";
fi
done
echo ;
done
echo ;
}
bars() {
for style in {100..107}; do
((color=style-70))
printf "\e[${style};${color}m ${style};${color} \033[0m "
if (( (style-3) % 8 == 0)); then echo; fi
done
for mode in {1..2}; do
for color in {30..37}; do
((bg=color+10))
code="${mode};${bg};${color}"
printf "\033[${code}m ${code} \033[0m "
done
echo ;
done
echo ;
for mode in {1,0,2}; do
for color in {30..37}; do
code="${mode};7;${color}"
printf "\033[${code}m ${code} \033[0m "
done
echo ;
done
echo ;
for foreground in {30..37}; do
for background in {40..47}; do
colors="${foreground};${background}"
if (($foreground==$background-10));
then mode=1; else mode=0;
fi
printf "\033[${mode};${colors}m ${colors} \033[0m "
done
echo ;
done
echo ;
}
modes() {
for mode in {1,0,2}; do
for foreground in {30..37}; do
printf "\e[${mode};${foreground}m ${mode};${foreground}\033[0m "
done
echo;
done
echo ;
}
textstyles() {
local info=""
declare -A STYLES=([3]=italic [4]=underline [5]=blink [6]=unknown [7]=inverse [8]=hidden [9]=strike [21]=underline2 [53]=overline)
for s in "${!STYLES[@]}"; do
info=$(color256 238 ${STYLES[$s]})
echo -e " $s \t \033[${s};37m text \033[0m ${info}\n";
done
echo ;
}
reset;
clear && clear;
textstyles
# table_c16 "[T]";
# grayscale "[T]";
table_c16 "TE";
table_c16 "$brick";
echo ;
grayscale "TE";
grayscale "$brick";
echo -e "\n";
modes
bars
echo ;
table1 "$brick"
table1 "1"
table2 "$brick"
table2 "1"
rgb_matrix "$brick"
rgb_matrix "1"
# rgb_matrix2 "$brick"
# rgb_matrix2 "1"
reset;
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment