php - Get color value from numeric values -


i need projet color value. explain, have datas , each data must represented color.

the red color maximum, , blue minimum , green middle value. kind of heatmap.

so, need have function returning right color.

i tried :

    function datatocolor($min, $max, $value)     {         $half = (($min + $max) / 2);          if ($value > $half)         {             $r = (255 * ($value+$min-$half)) / $half;             $g = 255 - $r;             $b = 0;         }          else {             $b = (255 * ($half-$value+$min)) / $half;             $g = 255 - $b;             $r = 0;                  }         $color = array(intval($r), intval($g),  intval($b));         return $color;     } 

but, red , blue, , never green ... tried lot of operations, must stupid don't find right operation ... in advance !

i'm not php expert, far can tell, problem isn't code block. tested algorithm in java sure, , looks correct:

public static void main(string[] args) {     int min = 0;     int max = 10;     int half = (min + max) / 2;      int r, g, b;      // cycling through values completeness' sake.     (int value = 0; value <= 10; value++) {         if (value > half) {         r = (255 * (value + min - half)) / half;         g = 255 - r;         b = 0;     } else {         b = (255 * (half - value + min)) / half;         g = 255 - b;         r = 0;     }     system.out.println("value: " + value + " - " + new color(r, g, b)); } 

the output expect -- pure blue @ minimum, pure green @ middle, , pure red @ maximum:

value: 0 - java.awt.color[r=0,g=0,b=255] value: 1 - java.awt.color[r=0,g=51,b=204] value: 2 - java.awt.color[r=0,g=102,b=153] value: 3 - java.awt.color[r=0,g=153,b=102] value: 4 - java.awt.color[r=0,g=204,b=51] value: 5 - java.awt.color[r=0,g=255,b=0] value: 6 - java.awt.color[r=51,g=204,b=0] value: 7 - java.awt.color[r=102,g=153,b=0] value: 8 - java.awt.color[r=153,g=102,b=0] value: 9 - java.awt.color[r=204,g=51,b=0] value: 10 - java.awt.color[r=255,g=0,b=0] 

based on you've provided, problem seems either in way you're calling function, or in way you're using array returns.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -