#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  THEME=
}

show_usage() {
  cat <<- EOF
	usage: ${COMMAND} <theme>

	Test the theme colors contrast to each other.
	Only the most meaningful combinations are tested.

	Options:
	  -h     show this message only
	EOF
}

parse_command_line() {
  while getopts h OPT; do
    case "${OPT}" in
      h) show_usage; exit 0 ;;
      ?) exit_error ;;
    esac
  done

  shift "$((OPTIND - 1))"

  THEME="$1"
}

validate_command_line() {
  if [ -z "${THEME}" ]; then
    exit_error 'missing theme'
  fi

  if [ ! -f "config/colors/base_${THEME}" ]; then
    exit_error "invalid theme '${THEME}'"
  fi
}

# shellcheck disable=SC2120
exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  printf "Try '%s -h' for more information.\n" "${COMMAND}"
  exit 1
} >&2

print_contrast() {
  RGB1=$( base_to_rgb "$1" )
  RGB2=$( base_to_rgb "$2" )
  CONTRAST=$( contrast_ratio "${RGB1}" "${RGB2}" )

  printf '\033[0m'                 # reset
  printf '\033[38;2;%sm' "${RGB1}" # foreground
  printf '\033[48;2;%sm' "${RGB2}" # background
  printf '  %s  ' "$1"             # base 1
  printf '\033[7m'                 # invert background/foreground
  printf '  %s  ' "$2"             # base 2
  printf '\033[0m'                 # reset
  printf '  %4.1f' "${CONTRAST}"   # contrast
  printf '\n'                      # newline
}

base_to_hex() {
  sed -n "s/^| $1 | #\([[:alnum:]]\+\) |.*/\1/p" "config/colors/base_${THEME}"
}

hex_to_rgb() {
  HEX_R=$( echo "$1" | cut -c 1-2 )
  HEX_G=$( echo "$1" | cut -c 3-4 )
  HEX_B=$( echo "$1" | cut -c 5-6 )

  # use ; as a delimiter between the RGB components
  # to be able to directly use the full value inside ANSI escape sequences
  printf "%d;%d;%d\n" "0x${HEX_R}" "0x${HEX_G}" "0x${HEX_B}"
}

base_to_rgb() { hex_to_rgb "$( base_to_hex "$1" )"; }

relative_luminance() {
  R="$1"
  G="$2"
  B="$3"

  tmp_value() {
    VALUE="$1"

    DEC_VALUE=$( echo "${VALUE} / 255" | bc -l )

    if [ "$( echo "${DEC_VALUE} <= 0.03928" | bc -l )" -eq 1 ]; then
      echo "${DEC_VALUE} / 12.92" | bc -l
    else
      # bc doesn't support fractional exponent so we replace it with exp/ln usage
      echo "e(2.4 * l( ( ${DEC_VALUE} + 0.055 ) / 1.055 ) )" | bc -l
    fi
  }

  R=$( tmp_value "${R}" )
  G=$( tmp_value "${G}" )
  B=$( tmp_value "${B}" )

  echo "( 0.2126 * ${R} + 0.7152 * ${G} + 0.0722 * ${B} )" | bc -l
}

contrast_ratio() {
  COLOR_1="$1"
  COLOR_2="$2"

  R1=$( echo "${COLOR_1}" | cut -f 1 -d ';' )
  G1=$( echo "${COLOR_1}" | cut -f 2 -d ';' )
  B1=$( echo "${COLOR_1}" | cut -f 3 -d ';' )

  R2=$( echo "${COLOR_2}" | cut -f 1 -d ';' )
  G2=$( echo "${COLOR_2}" | cut -f 2 -d ';' )
  B2=$( echo "${COLOR_2}" | cut -f 3 -d ';' )

  L1=$( relative_luminance "${R1}" "${G1}" "${B1}" )
  L2=$( relative_luminance "${R2}" "${G2}" "${B2}" )

  if [ "$( echo "${L1} < ${L2}" | bc -l )" -eq 1 ]; then
    L_HIGH="${L2}"
    L_LOW="${L1}"
  else
    L_HIGH="${L1}"
    L_LOW="${L2}"
  fi

  echo "( ${L_HIGH} + 0.05 ) / ( ${L_LOW} + 0.05 )" | bc -l
}

test_contrast() {
  print_contrast base04 base00
  print_contrast base04 base01
  print_contrast base04 base02

  printf '\n'
  print_contrast base05 base01
  print_contrast base05 base02
  print_contrast base05 base03

  printf '\n'
  print_contrast base03 base00
  print_contrast base04 base00
  print_contrast base05 base00
  print_contrast base06 base00
  print_contrast base07 base00
  print_contrast base08 base00
  print_contrast base09 base00
  print_contrast base0A base00
  print_contrast base0B base00
  print_contrast base0C base00
  print_contrast base0D base00
  print_contrast base0E base00
  print_contrast base0F base00

  printf '\n'
  print_contrast base03 base01
  print_contrast base04 base01
  print_contrast base05 base01
  print_contrast base06 base01
  print_contrast base07 base01
  print_contrast base08 base01
  print_contrast base09 base01
  print_contrast base0A base01
  print_contrast base0B base01
  print_contrast base0C base01
  print_contrast base0D base01
  print_contrast base0E base01
  print_contrast base0F base01
}

init_variables
parse_command_line "$@"
validate_command_line

test_contrast
