#!/bin/sh -e

term_colors=$(infocmp -1 | sed -n "s/^\tcolors#\(.*\),$/\1/p")

color_hash() {
  short_hash=$(echo "$@" | md5sum | cut -b -8)
  echo $((1 + 0x${short_hash} % (term_colors - 1)))
}

if [ "$1" = "-p" ]; then
  shift

  c=$(color_hash "$@")
  printf "\e[38;5;${c}m%b\e[0m\n" "$*"
else
  color_hash "$@"
fi
