Skip to content

Instantly share code, notes, and snippets.

@eabase
Last active January 16, 2022 11:23
Show Gist options
  • Save eabase/4e0444f790c45b2732aba49d61199e3a to your computer and use it in GitHub Desktop.
Save eabase/4e0444f790c45b2732aba49d61199e3a to your computer and use it in GitHub Desktop.
Bash 24-bit HSV Color & Gray Scale
#!/bin/bash
#----------------------------------------------------------------------
# Author: EABASE
# Date: 2022-01-16
# Version: 1.1
#----------------------------------------------------------------------
# History:
# The original file was taken from iterm2 [1], and later modified
# by EABASE in [2].
#
# Description:
#
# 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 resets the terminal to default.
#
# Usage: Run with unbuffered output using:
# stdbuf -i0 -o0 -e0 ./24-bit-color.sh
#
# References:
# [1] https://github.com/gnachman/iTerm2/blob/master/tests/24-bit-color.sh
# [2] https://gist.github.com/eabase/4e0444f790c45b2732aba49d61199e3a
# [3]
# [4] https://en.wikipedia.org/wiki/HSL_and_HSV
# [5] https://en.wikipedia.org/wiki/List_of_software_palettes
# [6] https://en.wikipedia.org/wiki/Shades_of_gray
# [7] https://www.geeksforgeeks.org/how-to-convert-three-channels-of-colored-image-into-grayscale-image-in-matlab/
# Notes:
#
# rgb2gray()
# 0.2989 *R + 0.5870 *G + 0.1140 *B # [1] NTSC
# 0.2126 *R + 0.7152 *G + 0.0722 *B # [2] luminance signal EY
# 0.2627 *R + 0.6780 *G + 0.0593 *B # [3] UHDTV
#
# [1] BT.601 https://www.itu.int/rec/R-REC-BT.601-7-201103-I/en
# [2] BT.709 https://www.itu.int/rec/R-REC-BT.709-6-201506-I/en
# [3] BT.2020 https://www.itu.int/rec/R-REC-BT.2020-2-201510-I/en
#----------------------------------------------------------------------
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 [0..255]
#----------------------------------------------------------------------
rainbowColor() {
let h=$1/43
let f=$1-43*$h
let t=$f*255/43
let q=255-$t
#echo -e "\n($h $f $t $q)\n" 2>&1
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
}
#------------------------------
# Red
#------------------------------
showRed() {
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
}
#------------------------------
# Green
#------------------------------
showGreen() {
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
}
#------------------------------
# Blue
#------------------------------
showBlue() {
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
}
#------------------------------
# Rainbow Scale
#-----------------------------
showRainbow() {
echo -e "\nThe HSV/HSL Rainbow\n"
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
}
showLongBow() {
echo -e "\nTesting in one line...\n"
for i in `seq 0 255`; do setBackgroundColor `rainbowColor $i`; echo -n " "; done
resetOutput
}
#------------------------------
# Gray Scale
#------------------------------
# Grayscale = R/3 + G/3 + B/3 # average
# Grayscale = 0.299R + 0.587G + 0.114B # weighted
grayScale() {
# Average
let r=$1/3
let g=$2/3
let b=$3/3
echo "$r $g $b"
}
grayScale2() {
# NOT WORKING!
# weighted
let r=$1/3 #$1*0.299
let g=$2*6/10 #$2*0.587
let b=$3/10 #$3*0.114
echo "$r $g $b"
}
showGray() {
echo -e "\nThe Average Grayscale\n"
for i in `seq 0 6 768`; do
#rgb=`rainbowColor $i`
#gry=`grayScale $rgb`
gry=`grayScale $i $i $i`
#echo "($rgb) : ($gry)"
setBackgroundColor $gry
echo -n " "
done
resetOutput
}
#----------------------------------------------------------------------
# Main
#----------------------------------------------------------------------
echo -e "\nThe 24-bit RGB scales (reduced to 8-bit display).\n"
showRed
showGreen
showBlue
showRainbow
showGray
echo -e "\n\nok"
exit 0
#----------------------------------------------------------------------
# EOF
#----------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment