<?php

if (!function_exists("file_get_contents")) 
{
	function file_get_contents($filename, $use_include_path = 0) {
   		$data = ""; 
		$file = @fopen($filename, "rb", $use_include_path);
		if ($file) 
		{
			while (!feof($file)) $data .= fread($file, 1024);
			fclose($file);
		}
		   return $data;
	}
}

function wav2rgb($Wavelength)
{
	if (!function_exists('adj_scale_color'))
	{
		function adj_scale_color($color,$factor)
		{
			$max=255;
			$gamma=0.8;
	
			if ($color==0)
			{
				$retval = 0;
			}
			else
			{
				$retval = round(pow($color*$factor,$gamma)*$max);
				if ($retval<0) $retval=0;
				if ($retval>$max) $retval=$max;
			}
			return $retval;
		}
	}

	if ($Wavelength >= 380 and $Wavelength < 440)
	{
		$Red   = -($Wavelength - 440) / (440 - 380);
		$Green = 0;
		$Blue  = 1;
	}
	else if ($Wavelength >= 440 and $Wavelength < 490)
	{
		$Red   = 0;
		$Green = ($Wavelength - 440) / (490 - 440);
		$Blue  = 1;
	}
	else if ($Wavelength >= 490 and $Wavelength < 510)
	{
		$Red   = 0;
		$Green = 1;
		$Blue  = -($Wavelength - 510) / (510 - 490);
	}
	else if ($Wavelength >= 510 and $Wavelength < 580)
	{
		$Red   = ($Wavelength - 510) / (580 - 510);
		$Green = 1;
		$Blue  = 0;
	}
	else if ($Wavelength >= 580 and $Wavelength < 645)
	{
		$Red   = 1;
		$Green = -($Wavelength - 645) / (645 - 580);
		$Blue  = 0;
	}
	else if ($Wavelength >= 645 and $Wavelength <= 780)
	{
		$Red   = 1;
		$Green = 0;
		$Blue  = 0;
	}
	else
	{
		$Red   = 0;
		$Green = 0;
		$Blue  = 0;
	}

	// Let the intensity fall off near the vision limits
	

	if ($Wavelength >= 380 and $Wavelength < 420)
	{
		$Factor = 0.3 + 0.7*($Wavelength - 380) / (420 - 380);
	}
	else if ($Wavelength >= 420 and $Wavelength < 701)
	{
		$Factor = 1;
	}
	else if ($Wavelength >= 701 and $Wavelength <= 780)
	{
		$Factor = 0.3 + 0.7*(780 - $Wavelength) / (780 - 700);
	}
	else
	{
		$Factor = 0;
	}
	
	$R = adj_scale_color($Red,   $Factor);
	$G = adj_scale_color($Green, $Factor);
	$B = adj_scale_color($Blue,  $Factor);

//	return array($R, $G, $B);
	return sprintf("%02X%02X%02X",$R,$G,$B); 
}

echo "<html><body style='margin:0px;'>",
	"<div style='position:absolute; top:0px; left:20px;'><pre>",
	htmlspecialchars(file_get_contents('wav_rgb.php')),
	"</pre></div>";

for ($i=380;$i<=780;$i+=2)
{	
	$c=wav2rgb($i);
	echo "<div style='background-color:#$c;overflow:hidden;width:100%;height:10px;'>&nbsp</div>";
}

	echo "</body></html>";

?>