#!/bin/bash
#	$Id: GMT_RGBchart.sh 11490 2013-05-16 06:26:21Z pwessel $
#
# Plots a page of all 555 unique named colors
# Usage: GMT_RGBchart.sh <size>
# where <size> is the page size. Use either: ledger, a4, or letter
# This produces the file GMT_RGBchart_<size>.ps

. gmt_shell_functions.sh

gmt_init_tmpdir

SIZE=$1
COL=16
ROW=35

if [ "X"$SIZE"" = "Xletter" ] ; then
	WIDTH=10.5
	HEIGHT=8.0
	ORIENT=landscape
elif [ "X"$SIZE"" = "Xa4" ] ; then
	WIDTH=11.2
	HEIGHT=7.8
else
	SIZE=tabloid
	WIDTH=16.5
	HEIGHT=10.5
fi

ps=GMT_RGBchart_$SIZE.ps
allinfo=$GMT_TMPDIR/allinfo.tmp
cpt=$GMT_TMPDIR/lookup.cpt
rects=$GMT_TMPDIR/rects.tmp
whitetags=$GMT_TMPDIR/whitetags.tmp
blacktags=$GMT_TMPDIR/blacktags.tmp
labels=$GMT_TMPDIR/labels.tmp
gmt gmtset PS_MEDIA $SIZE PS_PAGE_ORIENTATION landscape

rectheight=0.56
W=`gmt gmtmath -Q $WIDTH $COL DIV 0.95 MUL =`
H=`gmt gmtmath -Q $HEIGHT $ROW DIV $rectheight MUL =`
textheight=`gmt gmtmath -Q 1 $rectheight SUB =`
fontsize=`gmt gmtmath -Q $HEIGHT $ROW DIV $rectheight MUL 0.6 MUL 72 MUL =`
fontsizeL=`gmt gmtmath -Q $HEIGHT $ROW DIV $textheight MUL 0.7 MUL 72 MUL =`

# Produce $allinfo from color and name files
egrep -v "^#|grey" "${GMT_SOURCE_DIR}"/src/Colors.txt | $AWK -v COL=$COL -v ROW=$ROW \
	'BEGIN{col=0;row=0}{if(col==0&&row<2){col++};if ($1 == $2 && $2 == $3) {printf "%s", $1} else {printf "%s/%s/%s", $1, $2,
	$3};printf " %g %s %g %g\n",0.299*$1+0.587*$2+0.114*$3,$4,col,row;col++;if(col==COL){col=0;row++}}' > $allinfo

# Produce temp files from $allinfo
$AWK '{printf "%g %s %g %s\n", NR-0.5, $1, NR+0.5, $1}' $allinfo > $cpt
$AWK -v h=$rectheight -v W=$W -v H=$H  '{printf "%g %g %g %g %g\n",$4+0.5,$5+1-0.5*h,NR,W,H}' $allinfo > $rects
$AWK -v h=$rectheight -v fs=$fontsize  '{if ($2 <= 127) printf "%g %g %gp,1 %s\n",$4+0.5,$5+1-0.5*h,fs,$1}' $allinfo > $whitetags
$AWK -v h=$rectheight -v fs=$fontsize  '{if ($2 > 127) printf "%g %g %gp,1 %s\n",$4+0.5,$5+1-0.5*h,fs,$1}' $allinfo > $blacktags
$AWK -v h=$textheight -v fs=$fontsizeL '{printf "%g %g %gp,1 @#%s@#\n",$4+0.5,$5+0.6*h,fs,$3}' $allinfo > $labels

# Plot all tiles and texts
gmt psxy -R0/$COL/0/$ROW -JX$WIDTH/-$HEIGHT -X0.25i -Y0.25i -B0 -C$cpt -Sri -W $rects -K > $ps
gmt pstext -R -J -O -K $labels -F+f --FONT=black >> $ps
gmt pstext -R -J -O -K $blacktags -F+f --FONT=black >> $ps
gmt pstext -R -J -O -K $whitetags -F+f --FONT=white >> $ps

# Put logo in top left corner
scale=`gmt gmtmath -Q $WIDTH $COL DIV 0.95 MUL 2 DIV =`
xoff=`gmt gmtmath -Q $WIDTH $COL DIV 0.05 MUL 2 DIV =`
yoff=`gmt gmtmath -Q $HEIGHT $ROW DIV $ROW 1 SUB MUL $scale 2 DIV SUB =`
gmtlogo $xoff $yoff $scale >> $ps

H=`gmt gmtmath -Q $HEIGHT $ROW DIV =`
gmt pslegend -O -Dx0/0/$WIDTH/$H/BL >> $ps <<END
L $fontsizeL 1 R Values are R/G/B. Names are case-insensitive.
L $fontsizeL 1 R Optionally, use GREY instead of GRAY.
END

gmt_remove_tmpdir
