#!/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);

$cs = $pdf->colorspace(
	-type => 'CalRGB',
	-whitepoint => [ 0.9, 1, 1.1 ],
	-blackpoint => [ 0, 0, 0 ],
	-gamma => [ 2.8, 2.8, 2.8 ],
	-matrix => [
		0.41238, 0.21259, 0.01929,
		0.35757, 0.71519, 0.11919,
		0.1805,  0.07217, 0.95049
	]
);

$cs2 = $pdf->colorspace(
	-type => 'CalRGB',
	-whitepoint => [ 0.85, 1, 1.15 ],
	-blackpoint => [ 0, 0, 0 ],
	-gamma => [ 1.2, 1.2, 1.2 ],
	-matrix => [
		0.41238, 0.21259, 0.01929,
		0.35757, 0.71519, 0.11919,
		0.1805,  0.07217, 0.95049
	]
);

$cs3 = $pdf->colorspace(
	-type => 'CalRGB',
	-whitepoint => [ 0.9, 1, 1.1 ],
	-blackpoint => [ 0, 0, 0 ],
	-gamma => [ 0.8, 0.8, 0.8 ],
	-matrix => [
		0.41238, 0.21259, 0.01929,
		0.35757, 0.71519, 0.11919,
		0.1805,  0.07217, 0.95049
	]
);

$cs4 = $pdf->colorspace(
	-type => 'Lab',
	-whitepoint => [ 0.9, 1, 1.1 ],
	-blackpoint => [ 0, 0, 0 ],
	-gamma => [ 2.2, 2.2, 2.2 ],
	-range => [ -100, 100, -100, 100 ]
);

@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
	#f00 
	#ff0000 
	#fff0000000 
	#ffff000000000 
	%0ff0 
	%00ffff00 
	%000ffffff000 
	%0000ffffffff0000 
	!0ff
	!00ffff
	!000ffffff
	!0000ffffffff
);

$csi = $pdf->colorspace(
	-type => 'Indexed',
	-base => 'DeviceRGB',
	-maxindex => scalar(@cols)-1,
	-whitepoint => [ 0.9, 1, 1.1 ],
	-blackpoint => [ 0, 0, 0 ],
	-gamma => [ 2.2, 2.2, 2.2 ],
	-colors => [
		map { [ map{ $_*255 } namecolor($_) ] } @cols
	]
);


@c = @cols;

while (scalar @c >0) {
	$page = $pdf->page;
	$page->mediabox(595,842);
	$txt=$page->text;
	$gfx=$page->gfx;
	$txt->compress;
	$gfx->compress;
	$txt->fillcolor(0);
	printt($txt,$font,20,100,800,'left=RGB toright=CalRGBs');
	
	foreach my $y (reverse(0..13)) {
	
		last if(scalar @c ==0);
		$col=shift @c;
		$gfx->fillcolorbyname($col);
		$gfx->strokecolor(0);
		$gfx->rect(100,100+$y*50,80,30);
		$gfx->fillstroke;
		printt($txt,$font,8,100,92+$y*50,$col);

		$gfx->fillcolorbyspace($cs,namecolor($col));
		$gfx->strokecolor(0);
		$gfx->rect(200,100+$y*50,80,30);
		$gfx->fillstroke;
		printt($txt,$font,8,200,92+$y*50,"cs:$col");

		$gfx->fillcolorbyspace($cs2,namecolor($col));
		$gfx->strokecolor(0);
		$gfx->rect(300,100+$y*50,80,30);
		$gfx->fillstroke;
		printt($txt,$font,8,300,92+$y*50,"cs2:$col");

		$gfx->fillcolorbyspace($cs3,namecolor($col));
		$gfx->strokecolor(0);
		$gfx->rect(400,100+$y*50,80,30);
		$gfx->fillstroke;
		printt($txt,$font,8,400,92+$y*50,"cs3:$col");

	}
		
}

@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,'left=RGB right=CMYK');
	$gfx=$page->gfx;
	$txt->compress;
	$gfx->compress;
	foreach my $y (reverse(0..13)) {
		foreach my $x (0..4) {
			last if(scalar @c ==0);
			$col=shift @c;
			$gfx->fillcolorbyname($col);
			$gfx->strokecolor(0);
			$gfx->rect(50+$x*100,100+$y*50,40,30);
			$gfx->fillstroke;

			$gfx->fillcolorbyname($col,1);
			$gfx->strokecolor(0);
			$gfx->rect(90+$x*100,100+$y*50,40,30);
			$gfx->fillstroke;

			printt($txt,$font,8,50+$x*100,92+$y*50,$col);

		}
		last if(scalar @c ==0);
	}
}

@c = (0..scalar(@cols)-1);

while (scalar @c >0) {
	$page = $pdf->page;
	$page->mediabox(595,842);
	$txt=$page->text;
	$txt->fillcolor(0);
	printt($txt,$font,20,100,800,'colorindexed');
	$gfx=$page->gfx;
	$txt->compress;
	$gfx->compress;
	foreach my $y (reverse(0..13)) {
		foreach my $x (0..4) {
			last if(scalar @c ==0);
			$col=shift @c;
			$gfx->fillcolorbyspace($csi,$col);
			$gfx->strokecolor(0);
			$gfx->rect(50+$x*100,100+$y*50,80,30);
			$gfx->fillstroke;
			printt($txt,$font,8,50+$x*100,92+$y*50,$col);
		}
		last if(scalar @c ==0);
	}
}

@c=map {$a=$_*10; map { [$a,$_*(100/7)] } (-7..7); } reverse(-10..10);

while (scalar @c >0) {
	$page = $pdf->page;
	$page->mediabox(595,842);
	$txt=$page->text;
	$gfx=$page->gfx;
	$txt->compress;
	$gfx->compress;
	$txt->fillcolor(0);
	printt($txt,$font,20,100,800,'L*a*b colors');
	foreach my $y (reverse(0..14)) {
		last if(scalar @c ==0);
		$col=shift @c;
		$gfx->fillcolorbyspace($cs4,100,@{$col});
		$gfx->strokecolor(0);
		$gfx->rect(50,50+$y*50,80,30);
		$gfx->fillstroke;
		printt($txt,$font,8,50,42+$y*50,sprintf('Lab:100`%d`%d',@{$col}));

		$gfx->fillcolorbyspace($cs4,80,@{$col});
		$gfx->strokecolor(0);
		$gfx->rect(150,50+$y*50,80,30);
		$gfx->fillstroke;
		printt($txt,$font,8,150,42+$y*50,sprintf('Lab:80`%d`%d',@{$col}));

		$gfx->fillcolorbyspace($cs4,60,@{$col});
		$gfx->strokecolor(0);
		$gfx->rect(250,50+$y*50,80,30);
		$gfx->fillstroke;
		printt($txt,$font,8,250,42+$y*50,sprintf('Lab:60`%d`%d',@{$col}));

		$gfx->fillcolorbyspace($cs4,40,@{$col});
		$gfx->strokecolor(0);
		$gfx->rect(350,50+$y*50,80,30);
		$gfx->fillstroke;
		printt($txt,$font,8,350,42+$y*50,sprintf('Lab:40`%d`%d',@{$col}));

		$gfx->fillcolorbyspace($cs4,20,@{$col});
		$gfx->strokecolor(0);
		$gfx->rect(450,50+$y*50,80,30);
		$gfx->fillstroke;
		printt($txt,$font,8,450,42+$y*50,sprintf('Lab:20`%d`%d',@{$col}));

	}
		
}


foreach my $h (0..35) {
	$page = $pdf->page;
	$page->mediabox(595,842);
	$txt=$page->text;
	$gfx=$page->gfx;
	$txt->compress;
	$gfx->compress;
	$txt->fillcolor(0);
	$hue=$h*10;
	printt($txt,$font,20,100,800,"hue,saturation,value (h=$hue)");

	foreach my $s (reverse(0..9)) {
		foreach my $v (0..13) {
			$gfx->fillcolor(HSVtoRGB($hue,(1-$s*0.1),(1-$v*0.05)));
			$gfx->strokecolor(0);
			$gfx->rect(50+(50*$s),100+$v*50,40,30);
			$gfx->fillstroke;
			printt($txt,$font,7,50+(50*$s),92+$v*50,"hsv:$hue/".(1-$s*0.1)."/".(1-$v*0.05));
		}
	}
		
}


foreach my $icc (qw( 
	sRGB.icm 
	CIERGB.icc
)) {
	next if(!-f $icc);

	$cs_icc = $pdf->colorspace(
		-type => 'ICCBased',
		-base => 'DeviceRGB',
		-iccfile => $icc,
		-components => 3,
	);

	@c = @cols;
	$page = $pdf->page;
	$page->mediabox(595,842);
	$txt=$page->text;
	$gfx=$page->gfx;
	$txt->compress;
	$gfx->compress;
	$txt->fillcolor(0);
	$hue=$h*10;
	printt($txt,$font,20,100,800,"ICC Profile : $icc /RGB");
	foreach my $s (0..4) {
		foreach my $v (0..5) {
			my $col=shift @c;
			$gfx->fillcolorbyspace($cs_icc,namecolor($col));
			$gfx->strokecolor(0);
			$gfx->rect(50+(100*$s),100+$v*100,80,80);
			$gfx->fillstroke;
			printt($txt,$font,8,50+(100*$s),92+$v*100,"icc:$col");
		}
	}
		
}

foreach my $icc (qw( 
	ps4CMYK.icc
	ps5CMYK.icc
)) {
	next if(!-f $icc);

	$cs_icc = $pdf->colorspace(
		-type => 'ICCBased',
		-base => 'DeviceCMYK',
		-iccfile => $icc,
		-components => 4,
	);

	@c = @cols;
	$page = $pdf->page;
	$page->mediabox(595,842);
	$txt=$page->text;
	$gfx=$page->gfx;
	$txt->compress;
	$gfx->compress;
	$txt->fillcolor(0);
	$hue=$h*10;
	printt($txt,$font,20,100,800,"ICC Profile : $icc /CMYK");
	foreach my $s (0..4) {
		foreach my $v (0..5) {
			my $col=shift @c;
			$gfx->fillcolorbyspace($cs_icc,RGBasCMYK(namecolor($col)));
			$gfx->strokecolor(0);
			$gfx->rect(50+(100*$s),100+$v*100,80,80);
			$gfx->fillstroke;
			printt($txt,$font,8,50+(100*$s),92+$v*100,"icc:$col");
		}
	}
}


	$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;
		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);	
	}
}
