hi, i try to "colorize" some numeric values here. lets say 100 should be red (#FF0000), 0= yellow (#FFFF00) and -100 green (#00FF00), and anything in between shades of red (between 0 and 100) and green (between -100 and 0); anyone with an idea how to do this?
The colour system you describe is made up of three parts, Red, Green and Blue (the primary colours). Each colour is described by it's decimal proportion - so, red is 255 Red, 0 Green, 0 Blue. 255 decimal is FF in hexidecimal (ie. base 16 which probably means nothing to anyone younger than 30!) Anyway, that is why red is ff 00 00. White is achieved by mixing the other colours together - ff ff ff. If you want purple, you'd mix red and blue - ff 00 ff And all other shades can be achieved through varying the quantities.
hu - anyone speaking my language? i know the rgb and hexadecimal system, i just wanted to know how to give a certain number x between a high (red) and low (green) value a proportional color representation. however, i solved it: function get_color($x, $min=-100, $max=100) { if($x<$min) { $x=$min; } if($x>$max) { $x=$max; } $range = abs($min)+abs($max); $red = abs(($max+$x)*255)/$range; $grn = abs(($min+$x)*255)/$range; $r = substr("00".dechex($red), -2); $g = substr("00".dechex($grn), -2); $color = '#'.$r.$g.'00'; return $color; } PHP: ... and then i get the value from my spam-o-meter, connect it to the database and get: (see attachment). EXCELLENT! and the approve/delete buttom for this waiting queue is on the other side of this table
Well pardon me for freely giving my time and trying to help ) Anyway, your solution is overly complicated. If you have values between -100 and 100, want negatives to be red, and positives to be green, utilising the maximum colour range available, all you have to do is multiply by 2.55. function get_color($x, $min=-100, $max=100) { if($x<$min) { $x=$min; } if($x>$max) { $x=$max; } if ($x<0){ $r=dechex($x*-2.55); $g='00'; }else{ $r='00'; $g=dechex($x*2.55); } $color = '#'.$r.$g.'00'; return $color; } PHP:
yeah, in this example. but $min and $max is a var and could also be 50 or 2000, so i have to use abs(($max+$x)*255)/$range; right?