#!/bin/sh
#
#  given a list of files creat a full sorted histogram of all the colors
# and output a colormap of all the colors used. The colormap is in ppm
# format and is sutiable for use with the ``ppmquant'' program.
#
#  Anthony Thyssen          3 Oct 1993     anthony@cit.gu.edu.au
#
progname="`basename $0`"

Usage() {
  echo >&2 "usage: $progname [options..] files... > histogram"
  echo >&2 "   -colormap <file>   use a non-standard colormap file"
  echo >&2 "NOTE: file format (xpm or ppm) depends on the suffix of file"
  exit 1
}

r="
"
b="                                                                      $r"

while [ 1 ]; do
  case  "$1"  in
    -c*)     COLROUT="$2"; shift ;;
    -*)      Usage ;;
    *)       break ;;
  esac
  shift 
done
[ $# -eq 0 ] && Usage

echo " r   g   b      lum     count"
echo "--- --- ---     ---     -----"
exec 9>&1

for i in "$@"; do
  echo >&9 -n "  $i$r"

  name="`expr "//$i" : '.*/\([^/]*\)'`"               # remove path
  suffix="`expr "$name" : '.*\.\([^.]*\)'`"

  case "$suffix" in    # assume that the icon is xpm
    pbm|pgm|ppm|pnm)  ppmhist $i ;;
    xpm) x2p.sed "$i" | xpmtoppm | ppmhist ;;
    xbm) true ;;
    *) false ;;
  esac 2>/dev/null

  if [ $? != 0 ]; then
    echo >&9 -n "${b}"
    echo >&2 "Bad xpm file \"$i\""
    continue
  fi

  echo >&9 -n "${b}"

done |\
nawk '# merge histograms
  $1 == "r" {
      next
  }
  $1 == "---" {
      next
  }
  { color = substr($0, 0, 11)
    lum[color] = $4
    count[color] += $5
  }
  END {
    for ( color in count ) {
      printf "%-11s%8d%10d\n", color, lum[color], count[color]
    }
  }
' |\
sort +4nr |\
sed '# substitute the standard color table
  s/^  0   0   0     /black           /
  s/^ 47  79  79     /dark slate grey /
  s/^112 128 144     /slate grey      /
  s/^190 190 190     /grey            /
  s/^220 220 220     /gainsboro       /
  s/^255 255 255     /white           /

  s/^238 130 238     /violet          /
  s/^255   0 255     /magenta         /
  s/^176  48  96     /#maroon         /
  s/^160  32 240     /purple          /

  s/^178  34  34     /firebrick       /
  s/^255   0   0     /red             /
  s/^255  99  71     /tomato          /
  s/^255 165   0     /orange          /
  s/^255 215   0     /gold            /
  s/^255 255   0     /yellow          /

  s/^255 250 205     /lemon chiffon   /
  s/^255 255 224     /#light yellow   /
  s/^245 222 179     /wheat           /
  s/^210 180 140     /tan             /

  s/^244 164  96     /#sandy brown    /
  s/^205 133  63     /peru            /
  s/^210 105  30     /#chocolate      /
  s/^160  82  45     /sienna          /

  s/^  0 100   0     /#dark green     /
  s/^ 46 139  87     /sea green       /
  s/^ 50 205  50     /lime green      /
  s/^  0 255   0     /green           /
  s/^152 251 152     /pale green      /

  s/^  0   0 128     /navy blue       /
  s/^  0   0 255     /blue            /
  s/^ 30 144 255     /dodger blue     /
  s/^135 206 235     /sky blue        /
  s/^230 230 250     /lavender        /
  s/^  0 255 255     /cyan            /
'
