Another gradient color generation algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.