HTML Color Codes: Red to Yellow to Green

前端 未结 13 1244
栀梦
栀梦 2020-12-23 11:16

I would like to come up with as many HEX HTML values to have a smooth color gradient from red to green:

I would like this to be similar to the following: http://ww

13条回答
  •  孤城傲影
    2020-12-23 11:58

    My reason for finding this question was that I was trying to make a colored uptime indicator for a table full of devices that "check-in" hourly. The idea being that it would be red at 0%, transition to yellow at 50%, and be green at 100%. This is of course pretty useless but it was an easy way to make a table look more impressive than it actually was. Given a min, max, and value it returns rgb 0-255 values for the correct color. Assumes valid input.

    function redYellowGreen(min, max, value)
    {
    	var green_max = 220;
    	var red_max = 220;
    	var red = 0;
    	var green = 0;
    	var blue = 0;
    
    	if (value < max/2)
    	{
    		red = red_max;
    		green = Math.round((value/(max/2))*green_max);
    	}
    	else
    	{
    		green = green_max;
    		red = Math.round((1-((value-(max/2))/(max/2)))*red_max);
    	}
    
    	var to_return = new Object();
    	to_return.red = red;
    	to_return.green = green;
    	to_return.blue = blue;
    
    	return to_return;
    }

提交回复
热议问题