php - Map value to color scale -


i have list of values should plotted map color. plotting map done, need figure out way map value n color represents value.

an example , solution far normalize values based on min , max , assign them hex color 0 lowest , 255 highest. of course limits self grey scale. here code:

$color = ($value / $max) * 255    // (min zero) 

but how if values should go blue red instance? there common libraries or tools can solve this? far haven't been able locate any.

there might libs that. let's short warm general principles. in general have following options:

  1. a predefined color index, e.g. $coloridx=array(0=>'#ffffff',1=>'#ffee00',...);
  2. any algorithm, e.g. linear gradient, iterations based adaption of 3 rgb channels (r = red, g = green, b = blue).
  3. a combination of both, puts result of complex algorithm color index , goes there.

if include algorithms in considerations must understand there no true or false. depends on implement. there might occasions makes sense render variations of green n=0..10 , have red black in beyond n>10. caps , multipliers set accents. things that.

one way of implementing linear gradient be:

function lineargradient($ra,$ga,$ba,$rz,$gz,$bz,$iterationnr) {   $colorindex = array();   for($iterationc=1; $iterationc<=$iterationnr; $iterationc++) {      $iterationdiff = $iterationnr-$iterationc;      $colorindex[] = '#'.         dechex(intval((($ra*$iterationc)+($rz*$iterationdiff))/$iterationnr)).         dechex(intval((($ga*$iterationc)+($gz*$iterationdiff))/$iterationnr)).         dechex(intval((($ba*$iterationc)+($bz*$iterationdiff))/$iterationnr));   }   return $colorindex; }  $colorindex = lineargradient(   100, 0, 0,   // rgb of start color   0, 255, 255, // rgb of end color   256          // number of colors in linear gradient );  $color = $colorindex[$value]; 

i updated code add dechex, feeds on comments.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -