#!/usr/bin/perl -w


sub printt {
	my ($txt,$f,$s,$x,$y,$t)=@_;
	$txt->translate($x,$y);
	$txt->font($f,$s);
	$txt->text($t);
}

use PDF::API2;
use PDF::API2::Util; ## this gets us 'namecolor' and 'RGBasCMYK'

$pdf=PDF::API2->new;

$font=$pdf->corefont('Helvetica',1);

@cols=qw( 
	aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson 
	cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray
	darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray grey green greenyellow 
	honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon
	lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen 
	mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen 
	paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray 
	slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen
);


	$page = $pdf->page;
	$page->mediabox(595,842);
	$txt=$page->text;
	$txt->fillcolor(0);
	printt($txt,$font,20,100,800,'business-colors basic');
	$gfx=$page->gfx;
	$txt->compress;
	$gfx->compress;
	foreach my $y (0..12) {
		my $yy=40+(12-$y)*50;
		foreach my $x (0..5) {
			my $xx=20+$x*90;
			if($x) {
				$gfx->fillcolor($pdf->businesscolor($y,$x));
				$gfx->strokecolor(0);
				$gfx->rect($xx,$yy,80,15);
				$gfx->fillstroke;
				printt($txt,$font,5,$xx,$yy+15,"color=($y,$x)");
				$gfx->fillcolor($pdf->businesscolor($y,-$x));
				$gfx->strokecolor(0);
				$gfx->rect($xx,$yy-15,80,15);
				$gfx->fillstroke;
				printt($txt,$font,5,$xx+40,$yy-20,"color=($y,-$x)");
			} else {
				$gfx->fillcolor($pdf->businesscolor($y));
				$gfx->strokecolor(0);
				$gfx->rect($xx,$yy-15,80,30);
				$gfx->fillstroke;
				printt($txt,$font,5,$xx,$yy-20,"color=($y)");
			}
		}
	}

@c = @cols;

while (scalar @c >0) {
	$page = $pdf->page;
	$page->mediabox(595,842);
	$txt=$page->text;
	$txt->fillcolor(0);
	printt($txt,$font,20,100,800,'business-colors basic');
	$gfx=$page->gfx;
	$txt->compress;
	$gfx->compress;
	foreach my $y (0..18) {
		my $yy=40+(18-$y)*40;
		$col=shift @c;
		foreach my $x (0..5) {
			my $xx=20+$x*90;
			if($x) {
				$gfx->fillcolor($pdf->businesscolor($col,$x));
				$gfx->strokecolor(0);
				$gfx->rect($xx,$yy,80,15);
				$gfx->fillstroke;
				printt($txt,$font,5,$xx,$yy+15,"color=(,$x)");
				$gfx->fillcolor($pdf->businesscolor($col,-$x));
				$gfx->strokecolor(0);
				$gfx->rect($xx,$yy-15,80,15);
				$gfx->fillstroke;
				printt($txt,$font,5,$xx+40,$yy-20,"color=(,-$x)");
			} else {
				$gfx->fillcolor($pdf->businesscolor($col));
				$gfx->strokecolor(0);
				$gfx->rect($xx,$yy-15,80,30);
				$gfx->fillstroke;
				printt($txt,$font,5,$xx,$yy-20,"color=($col)");
			}
		}
		last if(scalar @c ==0);
	}
}


$pdf->saveas("$0.pdf");

$pdf->end();

__END__


sub optimize_as_display_color_CMYK {
	my($c,$m,$y,$k)=@_;
	if($c=~/^[\!\#\%a-zA-Z]+/) {
		($c,$m,$y)=namecolor($c);
	}
	if(defined $k) {
		return($c,$m,$y,$k);
	} elsif(defined $y) {
		return RGBasCMYK($c,$m,$y);	
	} else {
		return RGBasCMYK($c,$c,$c);	
	}
}
