Added two more terminal color related scripts.

This commit is contained in:
Doncho N. Gunchev 2019-06-15 16:04:52 +03:00
parent 1400dd98f4
commit abbe65a93a
2 changed files with 168 additions and 0 deletions

100
term_colors/24-bit-color.sh Executable file
View file

@ -0,0 +1,100 @@
#!/bin/bash
# This file was originally taken from iterm2 https://github.com/gnachman/iTerm2/blob/master/tests/24-bit-color.sh
#
# This file echoes a bunch of 24-bit color codes
# to the terminal to demonstrate its functionality.
# The foreground escape sequence is ^[38;2;<r>;<g>;<b>m
# The background escape sequence is ^[48;2;<r>;<g>;<b>m
# <r> <g> <b> range from 0 to 255 inclusive.
# The escape sequence ^[0m returns output to default
setBackgroundColor()
{
#printf '\x1bPtmux;\x1b\x1b[48;2;%s;%s;%sm' $1 $2 $3
printf '\x1b[48;2;%s;%s;%sm' $1 $2 $3
}
resetOutput()
{
echo -en "\x1b[0m\n"
}
# Gives a color $1/255 % along HSV
# Who knows what happens when $1 is outside 0-255
# Echoes "$red $green $blue" where
# $red $green and $blue are integers
# ranging between 0 and 255 inclusive
rainbowColor()
{
let h=$1/43
let f=$1-43*$h
let t=$f*255/43
let q=255-t
if [ $h -eq 0 ]
then
echo "255 $t 0"
elif [ $h -eq 1 ]
then
echo "$q 255 0"
elif [ $h -eq 2 ]
then
echo "0 255 $t"
elif [ $h -eq 3 ]
then
echo "0 $q 255"
elif [ $h -eq 4 ]
then
echo "$t 0 255"
elif [ $h -eq 5 ]
then
echo "255 0 $q"
else
# execution should never reach here
echo "0 0 0"
fi
}
for i in `seq 0 127`; do
setBackgroundColor $i 0 0
echo -en " "
done
resetOutput
for i in `seq 255 -1 128`; do
setBackgroundColor $i 0 0
echo -en " "
done
resetOutput
for i in `seq 0 127`; do
setBackgroundColor 0 $i 0
echo -n " "
done
resetOutput
for i in `seq 255 -1 128`; do
setBackgroundColor 0 $i 0
echo -n " "
done
resetOutput
for i in `seq 0 127`; do
setBackgroundColor 0 0 $i
echo -n " "
done
resetOutput
for i in `seq 255 -1 128`; do
setBackgroundColor 0 0 $i
echo -n " "
done
resetOutput
for i in `seq 0 127`; do
setBackgroundColor `rainbowColor $i`
echo -n " "
done
resetOutput
for i in `seq 255 -1 128`; do
setBackgroundColor `rainbowColor $i`
echo -n " "
done
resetOutput

View file

@ -0,0 +1,68 @@
#!/usr/bin/env bash
trap 'tput sgr0' exit # Clean up even if user hits ^C
function setfg () {
printf '\e[38;5;%dm' $1
}
function setbg () {
printf '\e[48;5;%dm' $1
}
function showcolors() {
# Given an integer, display that many colors
for ((i=0; i<$1; i++))
do
printf '%4d ' $i
setbg $i
tput el
tput sgr0
echo
done
tput sgr0 el
}
### Main starts here
# First, test if terminal supports OSC 4 at all.
printf '\e]4;%d;?\a' 0
read -d $'\a' -s -t 0.1 </dev/tty
if [ -z "$REPLY" ]
then
# OSC 4 not supported, so we'll fall back to terminfo
max=$(tput colors)
else
# OSC 4 is supported, so use it for a binary search
min=0
max=256
while [[ $((min+1)) -lt $max ]]
do
i=$(( (min+max)/2 ))
printf '\e]4;%d;?\a' $i
read -d $'\a' -s -t 0.1 </dev/tty
if [ -z "$REPLY" ]
then
max=$i
else
min=$i
fi
done
fi
# If -v is given, show all the colors
case ${1-none} in
none)
echo $max
;;
-v)
showcolors $max
;;
*)
if [[ "$1" -gt 0 ]]; then
showcolors $1
else
echo $max
fi
;;
esac | less --raw-control-chars --QUIT-AT-EOF --no-init