<html>
<head>
<style type=text/css>
td.colorchart
{
   width:16px;
   height:12px;
}

</style>
<SCRIPT LANGUAGE="Javascript" SRC="js/ColorPicker2.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
var cp = new ColorPicker('window');
</SCRIPT>
</head>
<body>
<br><br>
<FORM method="post">
Color: <INPUT TYPE="text" NAME="color" value="<? echo $_POST["color"]; ?>" SIZE="20"> &lt; &lt; <A HREF="#" onClick="cp.select(document.forms[0].color,'pick');return false;" NAME="pick" ID="pick">Pick</A>
<br><br><input type="submit" name="GENERATE" value="GENERATE">
</FORM>
<br>
<table>
<?

#
# generate_color_table
# BSD license http://sysctl.org/bsd.php
#

function rgb2hsv($red, $green, $blue) {
   $r = $red   / 255;
   $g = $green / 255;
   $b = $blue  / 255;
        $min = min($r, $g, $b);    
        $max = max($r, $g, $b);    
        $v = $max;
        $delta = $max - $min;
        if ($max != 0) { $s = $delta / $max; }
        else {
            $s = 0;
            $h = -1;
            return array($h, $s, $v);
        }
        if ($delta) {
            if ($r == $max) { $h = ($g - $b) / $delta; }
            else if ($g == $max) { $h = 2 + ($b - $r) / $delta; }
            else { $h = 4 + ($r - $g) / $delta; }
        } else { $h = 0; }
        $h *= 60;
        if ($h < 0) $h += 360;
  return array($h, $s, $v);
}

function hsv2rgb($h, $s, $v) {
        if ($s==0) return array($v*255,$v*255,$v*255);
        $h %= 360;
        $h /= 60;
        $i = floor($h);
        $f = $h - $i;
        $p = $v * (1 - $s);
        $q = $v * (1 - $s * $f);
        $t = $v * (1 - $s * (1 - $f));
        switch($i) {
            case 0:
                $r = $v; $g = $t; $b = $p;
                break;
            case 1:
                $r = $q; $g = $v; $b = $p;
                break;
            case 2:
                $r = $p; $g = $v; $b = $t;
                break;
            case 3:
                $r = $p; $g = $q; $b = $v;
                break;
            case 4:
                $r = $t; $g = $p; $b = $v;
                break;
            default:
                $r = $v; $g = $p; $b = $q;
                break;
        }
  return array($r * 255, $g * 255, $b * 255);
}

function col_to_hex($tab) {
  list($r,$g,$b)=$tab;
  return "#".strtoupper(str_pad(dechex($r),2,"0",STR_PAD_LEFT).str_pad(dechex($g),2,"0",STR_PAD_LEFT).str_pad(dechex($b),2,"0",STR_PAD_LEFT));
}

function generate_color_table($couleur,$count,$mode) {

        $red=hexdec(substr($couleur,1,2));
        $green=hexdec(substr($couleur,3,2));
        $blue=hexdec(substr($couleur,5,2));

        list($hue,$sat,$val)=rgb2hsv($red,$green,$blue);

if($mode=="T") {
        $hue1=$hue+60;
        if($hue1>360) $hue1=abs(360-$hue1);
        $hue2=$hue1+120;
        if($hue2>360) $hue2=abs(360-$hue2);
        $hue3=$hue2+60;
        if($hue3>360) $hue3=abs(360-$hue3);
}
if($mode=="Q") {
        $hue1=$hue+90;
        if($hue1>360) $hue1=abs(360-$hue1);              
        $hue2=$hue1+90;              
        if($hue2>360) $hue2=abs(360-$hue2);              
        $hue3=$hue2+90;               
        if($hue3>360) $hue3=abs(360-$hue3);
}
if($mode=="R") {
        srand((100000*strtok(microtime()," "))+strtok(""));
        $random=rand(40,70);
        $hue1=$hue+$random;
        if($hue1>360) $hue1=abs(360-$hue1);
        $hue2=$hue1+$random;
        if($hue2>360) $hue2=abs(360-$hue2);
        $hue3=$hue2+$random;
        if($hue3>360) $hue3=abs(360-$hue3);
}
if($mode=="V") {
$div=360/$count;
$hold="x";
    for($i=0;$i<$count;$i++) {
        if($i%2==0) {
            $h=$hue+($i*$div);
            if($h>360) $h=abs(360-$h);
        }
        if($i%2==1) {
            $h=180+$h;
            if($h>360) $h=abs(360-$h);
        }
        $ztab[]=col_to_hex(hsv2rgb($h,$sat,$val));
    }
} else {
    $div=ceil($count/4);
    $satdiv=$sat/$div;
    $valdiv=$val/($div*4);
    if($sat>0.7) {
        $satdiv=$sat/$div;
        $valdiv=$val/($div*32);
    }
    if($val<0.5) {
        $valdiv=-$val/$div;
        if($sat<0.55) $satdiv=-((1-$sat)/$div);
    }
    for($i=0;$i<$div;$i++) {
            $tab[]=col_to_hex(hsv2rgb($hue,$sat-($i*$satdiv),$val-($i*$valdiv)));
            $tab[]=col_to_hex(hsv2rgb($hue1,$sat-($i*$satdiv),$val-($i*$valdiv)));
            $tab[]=col_to_hex(hsv2rgb($hue2,$sat-($i*$satdiv),$val-($i*$valdiv)));
            $tab[]=col_to_hex(hsv2rgb($hue3,$sat-($i*$satdiv),$val-($i*$valdiv)));
    }
    for($e=0;$e<$count;$e++) $ztab[]=$tab[$e];
}

return $ztab;
}

########

$max=234;
if($_POST["color"]=="") 
	$color="#CC66FF";
else $color=$_POST["color"];


$tabT=generate_color_table($color,$max,"T");
// $tabQ=generate_color_table($color,$max,"Q");
// $tabR=generate_color_table($color,$max,"R");
// $tabV=generate_color_table($color,$max,"V");

?>
<table border=0 cellspacing=1 cellpadding=0 style="margin-top:0px;margin-bottom:0px;background-color:#444444;">
<?php

for ( $j=0; $j < 13; $j++ ) {
	print "<tr>\n";
	for($i=0; $i < 18; $i++) {
		$colour = $tabT[$j*18 + $i];
		print "<td class='colorchart' bgcolor='$colour' onClick='SelectedColour('$colour')' title='$colour'></td>\n";
	}
	print "</tr>\n";
}

?>
</table>


</table>
</body></html>

