Convert wavelength in nm to RGB
Maybe some day you'll need to convert wavelengthes in nanometers to RGB values.I just needed it for visualizing emission values of flourescent dyes in a lab information system.
I didn't want to make that by hand, picking e.g. 20 values from the color range to let them stand for everything between them, but wanted to have a proper and (at least approximately) exact solution.
The function below is based on an algorithm developed by Dan Bruton I found in the web.
I just transcribed it to PHP, and it works pretty good.
<?php function wavtorgb($w){ $w = intval($w); if(!$w) return false; //color if($w >= 100 AND $w < 380){ $r = 0.91; $g = 0.77; $b = 1.0; }elseif($w >= 380 AND $w < 440){ $r = abs($w - 440) / (440 - 350); $g = 0.0; $b = 1.0; }elseif($w >= 440 AND $w < 490){ $r = 0.0; $g = abs($w - 440) / (490 - 440); $b = 1.0; }elseif($w >= 490 AND $w < 510){ $r = 0.0; $g = 1.0; $b = abs($w - 510) / (510 - 490); }elseif($w >= 510 AND $w < 580){ $r = abs($w - 510) / (580 - 510); $g = 1.0; $b = 0.0; }elseif($w >= 580 AND $w < 645){ $r = 1.0; $g = abs($w - 645) / (645 - 580); $b = 0.0; }elseif($w >= 645 AND $w < 780){ $r = 1.0; $g = 0.0; $g = 0.0; }elseif($w >= 780 AND $w < 1000000){ $r = 1.0; $g = 0.31; $b = 0.31; }else{ $r = 0.0; $g = 0.0; $b = 0.0; } //let the intensity sss fall off near the vision limits if($w >= 380 AND $w < 420){ $sss = 0.3 + 0.7*($w - 350) / (420 - 350); }elseif($w >= 420 AND $w <= 700){ $sss = 1.0; }elseif($w > 700 AND $w <= 780){ $sss = 0.3 + 0.7*(780 - $w) / (780 - 700); }else{ $sss = 1.0; } $sss *= 255; $r = intval($sss * $r); $g = intval($sss * $g); $b = intval($sss * $b); //label if($w >= 100 AND $w < 380){ $label = $w."nm (UV)"; }elseif($w >= 380 AND $w < 780){ $label = $w."nm"; }elseif($w >= 780 AND $w < 1000000){ $label = $w."nm (IR)"; }else{ return false; } $return = array($r, $g, $b, $label); return $return; } ?>