#!/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
################################################################################

shopt -s expand_aliases;
alias my='declare ' int='my -i ' array='my -a ' map='my -A '


setBackgroundColor() {
    #printf '\x1bPtmux;\x1b\x1b[48;2;%s;%s;%sm' $1 $2 $3
    printf '\x1b[48;2;%s;%s;%sm' $1 $2 $3
}

resetOutput() {
    printf '\x1b[0m\n'
}

setBackBlack() {
	setBackgroundColor 0 0 0
}


# Gives a color $1/255 % along HSV
rainbowColor() {
	int s=256		d=6			hs=(256+d/2)/d
	int i=$1%s	h=i/hs	f=i-43*h 
	int t=f*s/43				q=255-t

	array hout=(	"255 $t 0" "$q 255 0" "0 255 $t" 
								"0 $q 255" "$t 0 255" "255 0 $q")

	printf ${2:+-v $2} "%s" "${hout[h%d]}"
}

title() {
	my txt=$1
	setBackBlack
	printf "$txt"
}


title "\nRed, dim-to-bright in 256 steps:\n"
for i in {0..255}; do
    setBackgroundColor $i 0 0
    printf " "
done

title "\nGreen, dim-to-bright in 256 steps:\n"
for i in {0..255}; do
    setBackgroundColor 0 $i 0
    printf " "
done

title "\nBlue, dim-to-bright in 256 steps:\n"
for i in {0..255}; do
    setBackgroundColor 0 0 $i
    printf " "
done

title "\nRainbow strip\n"
title "(red->orange->yellow->green->turqoise->blue->purple->magenta->red)\n"

my c=""
for i in {0..255}; do
	rainbowColor $i c
  setBackgroundColor "$c"
  printf " "
done
resetOutput
# vim: ts=2 sw=2 ai
