#! /bin/zsh # # this file: # https://dataswamp.org/~incal/conf/.zsh/colors # # examples: # https://dataswamp.org/~incal/figures/tty-colors.png # https://dataswamp.org/~incal/conf/.Xresources # # tutorial: # https://dataswamp.org/~incal/cols/www/COLORS set-fg-color () { tput setaf $1 # 0-7 } set-bg-color () { tput setab $1 # 0-7 } set-bold () { tput bold } reset-color() { tput sgr0 } effect-cols () { local colors=${1:-normal} sudo ~/cols/run $colors exec-cols # restart tty after } set-color () { local color_string=$1 local colors=(black red green yellow blue magenta cyan white) local color_index=${colors[(i)$color_string]} local tput_index=$(( $color_index - 1 )) set-fg-color $tput_index } echo-with-color () { local color_string=$1 shift set-color $color_string echo $@ reset-color } test-colors () { local color_number local color local bg_number local bg local color_names=(bl re gr ye bl ma cy wh) echo for color_number in {0..7}; do echo -n " $color_number " set-fg-color $color_number color=$color_names[(( $color_number + 1 ))] echo -n "$color "; tput bold; echo -n $color reset-color for bg_number in {0..7}; do bg=$color_names[(( $bg_number + 1 ))] echo -n " " set-fg-color $color_number set-bg-color $bg_number echo -n $bg tput bold echo -n " " set-fg-color $color_number set-bg-color $bg_number echo -n $bg reset-color done echo done echo } alias cols=test-colors compare-emacs-default () { xrdb ~/.Xresources ~/.Xresources-like-emacs xterm } print-label () { local bg=$1 local fg=$2 shift shift set-bg-color $bg set-fg-color $fg echo -n $@ reset-color } ansi-print () { local hexa=$1 local num=$2 if (( $hexa == 1 )); then printf "%2x " $num else printf "%3d " $num fi } ansi () { local -a cols cols=("black" "red" "green" "yellow" "blue" "magenta" "cyan" "white") local ci=0 local c local hexa=1 reset-color local bg=0 set-bg-color $bg local offs="" echo -n $offs for c in $cols; do set-bg-color $ci ansi-print $hexa $ci ci=$(( $ci + 1 )) done set-bg-color $bg echo echo -n $offs for c in $cols; do set-bg-color $ci ansi-print $hexa $ci ci=$(( $ci + 1 )) done set-bg-color $bg echo '\n' repeat 2 { repeat 6 { repeat 3 { repeat 6 { set-bg-color $ci ansi-print $hexa $ci ci=$(( $ci + 1 )) } set-bg-color $bg echo -n " " } echo } echo } repeat 24 { set-bg-color $ci ansi-print 1 $ci ci=$(( $ci + 1 )) } reset-color echo "\n" } ansiline () { local args=($@) reset-color for b in $args; do set-bg-color $b echo -n " " reset-color done } alias a=ansiline alias e='echo -n " "' ansibox () { local cols=$1 local rows=$2 local sx=$3 local sy=$4 local beg=$5 local i=$beg local iy ix for (( iy=0; $iy < $rows; iy++ )); do for (( ix=0; $ix < $cols; ix++ )); do set-bg-color $i printf "%4d" $i reset-color i=$(( $i + $sx )) done i=$(( $i + $sy )) echo done echo } ansibig () { ansidark ansilight } ansidark () { ansibox 18 6 1 18 16 } ansilight () { ansibox 18 6 1 18 32 } ansibox-dark () { local w=6 local h=6 local sx=1 local sy=30 local beg=16 local n=3 for (( m=0; m < $n; m++ )); do ansibox $w $h $sx $sy $(( $beg + $m * $w )) done } ansibox-light () { local w=6 local h=6 local sx=1 local sy=30 local beg=32 local n=3 for (( m=0; m < $n; m++ )); do ansibox $w $h $sx $sy $(( $beg + $m * $w )) done } zsh-rgb () { local c for r (0 1 2 3 4 5); do for g (0 1 2 3 4 5); do for b (0 1 2 3 4 5); do c=$(( 16 + 6**2 * $r + 6**1 * $g + 6**0 * $b )) print -n -P "%F{$c}Some text in color.%f" print -n " [ansi $c]" ansi-to-rgb $c done done done } zsh-rgb-red () { local c for r (5); do for g (0 1 2 3 4) ; do for b (0 1 2 3 4); do c=$(( 16 + 6**2 * $r + 6**1 * $g + 6**0 * $b )) print -n -P "%F{$c}Some text in color.%f" print -n " [ansi $c]" ansi-to-rgb $c done done done } ansi-to-rgb () { local num=$1 local ir=$(( ( $num - 16 ) / 36 )) local ig=$(( ((( $num - 16 ) % 36 ) / 6) )) local ib=$(( ( $num - 16 ) % 6 )) local r g b [[ 0 < $ir ]] && r=$(( 55 + $ir * 40 )) || r=0 [[ 0 < $ig ]] && g=$(( 55 + $ig * 40 )) || g=0 [[ 0 < $ib ]] && b=$(( 55 + $ib * 40 )) || b=0 printf " rgb = ( %3d %3d %3d ) [%3x %3x %3x]\n" $r $g $b $r $g $b }