#!/usr/bin/perl -w
# algorithm according to the desaturate function in the gimp
# sourcetree 
# quick and dirty desaturation for postscript files
# reads PS or EPS file from STDIN
# searches for patterns doing 


$rgbre='^(\/c\w*\d+)\s+{\s*([0-9.]+)\s+([0-9.]+)\s+([0-9.]+)\s+(\w+)\s*}\s+(\w+.*)$';

while ($s=<STDIN>){
    if($s=~/$rgbre/){
	$colorname=$1;
	@rgbin=($2,$3,$4);
	$colorcommand=$5;
	$bindcommand=$6;
	@srgb = sort @rgbin;
	$smin = shift @srgb;
	$smax = pop @srgb;
	$lightness = ($smax + $smin) / 2;
	@rgbout = ($lightness,$lightness,$lightness);
	$s="$colorname {".join(" ",@rgbout)." $colorcommand} $bindcommand\n";
    }
    print $s;
}






