#!/bin/sh

#           m   40m  100m   41m  101m   42m  102m   43m  103m   44m  104m   45m  105m   46m  106m   47m  107m
#     m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   30m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;30m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   90m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;90m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   31m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;31m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   91m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;91m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   32m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;32m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   92m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;92m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   33m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;33m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   93m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;93m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   34m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;34m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   94m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;94m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   35m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;35m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   95m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;95m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   36m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;36m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   96m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;96m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   37m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;37m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
#   97m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 
# 1;97m  ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   ***   *** 

# File:         colortest.textart
# Description:  Colors testing
# Author:       NNB
#               └─ https://github.com/NNBnh
# URL:          https://github.com/NNBnh/nnbs-text-art/blob/main/color/colortest.textart
# License:      GPLv3

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.


# Values
TEXT="${1:- *** }"
SEPARATOR="${2- }"
FOREGROUND_MODES="${3:-normal bold bright both}"
BACKGROUND_MODES="${4:-normal bright}"
FOREGROUND_ORDER="${5:-lable none 0 1 2 3 4 5 6 7}"
BACKGROUND_ORDER="${6:-$FOREGROUND_ORDER}"

case "$FOREGROUND_ORDER" in
	*lable*)
		case "$FOREGROUND_MODES" in
			*bo*) FOREGROUND_MODES_BOLD='true' ;;
		esac

		case "$BACKGROUND_MODES" in
			*bright*) BACKGROUND_MODES_BRIGHT='true' ;;
		esac
	;;
esac

[ "${#TEXT}" -ge '4' ] && case "$BACKGROUND_ORDER" in
	*lable*)
		i='4'

		while [ "$i" -lt "${#TEXT}" ]; do
			PADDING="$PADDING "

			i=$(( i + 1 ))
		done
	;;
esac


# Start
for foreground in $FOREGROUND_ORDER; do
	for foreground_mode in $FOREGROUND_MODES; do
		if [ "$foreground" = 'none' ] || [ "$foreground" = 'lable' ]; then
			[ "$foreground_mode" = 'bright' ] || [ "$foreground_mode" = 'bold' ] || [ "$foreground_mode" = 'both' ] && continue

			foreground_style=''
		else
			case "$foreground_mode" in
				'normal') foreground_style="3$foreground"   ;;
				'bright') foreground_style="9$foreground"   ;;
				'bold')   foreground_style="1;3$foreground" ;;
				'both')   foreground_style="1;9$foreground" ;;
			esac
		fi

		[ "$foreground" = 'lable' ] && [ "${#TEXT}" -lt '4' ] && continue

		[ "$newline" = 'true' ] && print="$print\n"

		for background in $BACKGROUND_ORDER; do
			for background_mode in $BACKGROUND_MODES; do
				if [ "$background" = 'none' ] || [ "$background" = 'lable' ]; then
					[ "$background_mode" = 'bright' ] && continue

					background_style=''
				else
					case "$background_mode" in
						'normal') background_style="4$background"  ;;
						'bright') background_style="10$background" ;;
					esac
				fi

				[ "$separator" = 'true' ] && print="$print$SEPARATOR"

				if [ "$foreground" = 'lable' ] && [ "$background" = 'lable' ]; then
					[ "$FOREGROUND_MODES_BOLD" = 'true' ] && print="$print  "

					print="$print   "
				elif [ "$foreground" = 'lable' ]; then
					[ "$BACKGROUND_MODES_BRIGHT" = 'true' ] && [ "${#background_style}" -lt '3' ] && print="$print "

					[ "$background" = 'none' ] && print="$print  "

					print="$print$PADDING${background_style}m"
				elif [ "$background" = 'lable' ]; then
					[ "$FOREGROUND_MODES_BOLD" = 'true' ] && case "$foreground_mode" in
						*bo*) ;;
						*)
							print="$print  "
						;;
					esac

					[ -z "$foreground_style" ] && print="$print  "

					print="$print${foreground_style}m"
				else
					print="$print\033[0${background_style:+;$background_style}${foreground_style:+;$foreground_style}m$TEXT\033[0m"
				fi

				separator='true'
			done
		done

		separator=''
		newline='true'
	done
done

echo -e "\n$print\n"

exit 0
