Background
Simplifying the background, given an indicator x, is normal when it is smaller, and more dangerous when it is larger. For a more intuitive display, the front-end display wants to use color to highlight the value--the color is green when it is smaller, the red is displayed when it is larger, and the transition is made with some yellow.
Using Google search, did not find the appropriate code. As a laughable, dewdrop is going to make a ~ ...
Analysis
Colors can be represented by RGB values in HTML-green is RGB (0,255,0), Red is RGB (255,0,0), and yellow is RGB (255,255,0). To achieve the gradient effect, you need to design a function f:
F (0) = RGB (0,255,0); F (relatively large value) = RGB (255,0,0); F (median value) = RGB (255,255,0);
Results
function seems to be very complex, but the adjustment for a while out, the effect is good.
Sun code:
/** * Generate gradients, from green to red gradient * @param int $x indicator value * @param int $threshold gradient threshold, $x equal to this value is just yellow * @param float $bri Ghtness brightness, from 0 to 1 * @return string RGB color values */function generategradientcolor ($x, $threshold, $brightness = 1) { return S printf (' RGB (%d,%d, 0) ', intval (min (255, ($x * 1.0/$threshold * 255)) * $brightness), intval (max (0, Min (255, 2 -$x * 1.0/$threshold) * 255)) * $brightness));}
Effect
The resulting gradient color effect is as follows (threshold = 25):
The PHP code that generates the above effect is attached:
<style> div{ display:block; font-size:10px; height:1em; } </style><?phpfor ($i = 1; $i < $i + +) { echo strtr ("<div style= ' width: {width}em; $i </div> ", Array ( ' {width} ' = $i, ' {color} ' = = Generategradientcolor ($i))) ;}? >
Another gradient color generation algorithm