From 8-bit color chapter of Wikipedia's ANSI escape code, and to be quicker, without any loop:
#!/bin/bash shape='XXXXXXXX' arry=(0for 0((i=0; 2{,,}{,,}{,,,}i<256; 0{,,}i++)) ;do eval arry= fg=$(${arry[@]/?/\${shape:&\}}(((i>231&&i<244)||((i<17)&&(i%8<2))||(i>16&&i<232)&&((i-16)%6*11+ printf -v shape '%s\n' "${arry[@]//X/ \\e[48;5;%d;38;5;%%%%dm C %%3d \\e[0m}" declare -ai arry= ({0..255}i-16)/6%6*14+(i-16)/36*10)<58)?7:16)) printf -v shape "$shape" ${arry[@]} printf -v' shape "$shape"\e[48;5;%d;38;5;%dm ${arry[@]}C %3d \e[49;39m' $i $fg $i eval "arry= (${arry[@]/*/\"(&>231\&\&&<244)|| ((&<17i<16||i>231)\&\& && (&%8<2(i+1)%8==0)||(&>16\&\& ) || &<232)\&\&((&-16i>16&&i<232)%6*11+&& (&-16)/6%6*14+(&i-1615)/36*10%6==0)<58?7:16\"})" )) && printf "$shape" ${arry[@]} printf "\n" done Note: On my raspberry, this could by cut'n pasted into a fresh terminal windowscript take approx 1.5 seconds to complete.
Same but without loop:
Same script with some commentsCould be quicker again:
#!/bin/bash # Building a shape of 41 lines with two first lines of 8 fields, followed # by 36 lines of 6 fields, then 3 lines of 8 fields. shape='XXXXXXXX' # an 8 X string arry=(0 0 2{,,}{,,}{,,,} 0{,,}) # 2(x8) + 36(x6) + 3(x8): 41 lines eval arry=(${arry[@]/?/\${shape:&\}}) # 'arry=(XXXXXXXX XXXXXXXX XXXXXX ...' printf -v shape '%s\n' "${arry[@]//X/ \\e[48;5;%d;38;5;%%%%dm C %%3d \\e[0m}" # %d will be ANSI color, then %%3d right aligned label, and %%%%d foreground declare -ai arry=({0..255}) # arry is now an array of 256 integers printf -v shape "$shape" ${arry[@]} # populate colors printf -v shape "$shape" ${arry[@]} # populate labels # Building array of 256 foreground colors eval "arry=(${arry[@]/*/\"(&>231\&\&&<244)||((&<17)\&\&(&%8<2))||(&>16\&\& &<232)\&\&((&-16)%6*11+(&-16)/6%6*14+(&-16)/36*10)<58?7:16\"})" printf "$shape" ${arry[@]} # print out finalised shape with foregrounds Note: this could by cut'n pasted into a fresh terminal window.
Then this version will print same output, but in ~0.6 second on my raspberry pi.
Explained:
- Building a shape of 41 lines with two first lines of 8 fields, followed by 36 lines of 6 fields, then 3 lines of 8 fields.
shape='XXXXXXXX' # some 8 X string arry=(0 0 2{,,}{,,}{,,,} 0{,,}) # 2(x8) + 36(x6) + 3(x8): 41 lines eval arry=(${arry[@]/?/\${shape:&\}}) # 'arry=(XXXXXXXX XXXXXXXX XXXXXX ..' printf -v shape '%s\n' "${arry[@]//X/ \\e[48;5;%d;38;5;%%%%dm C %%3d \\e[0m}"- replace each
Xby a format string for 3 values: - add a newline
\nafter each group of transformedX %dwill be ANSI color resolved first, then%%3dfor right aligned label, and%%%%dfor the foreground color.
- replace each
- Converting
$arryinto an integer array containing values from0to255declare -ai arry=({0..255}) - Populate background colors (
%d)printf -v shape "$shape" ${arry[@]} - Populate labels (
%%d)printf -v shape "$shape" ${arry[@]} - Compute 256 foreground colors into (integer) array
$arryeval "arry=(${arry[@]/*/\"(&>231\&\&&<244)||((&<17)\&\&(&%8<2))||(&>16\&\& &<232)\&\&((&-16)%6*11+(&-16)/6%6*14+(&-16)/36*10)<58?7:16\"})" - Populate finalised shape with foregrounds and print out.
printf "$shape" ${arry[@]}


