from green to red color depend on percentage

前端 未结 14 1985
北恋
北恋 2020-11-30 20:00

I have a poll system and I want answers for this poll to be colored. For example: If it\'s 10% it would be red, if 40% it would be yellow and if 80% it would be green, so I

相关标签:
14条回答
  • 2020-11-30 20:56

    HSL will work in IE8 using jquery-ui-1.10.4.

    I modified jongo45's answer to accept lightness in the function.

    function getColor(value, lightness) {
        //value from 0 to 1
        var hue = ((value) * 120).toString(10);
        return ["hsl(", hue, ",100%,", lightness, "%)"].join("");
    }
    
    0 讨论(0)
  • 2020-11-30 21:00

    This method works well in this case (percent from 0 to 100):

    function getGreenToRed(percent){
                r = percent<50 ? 255 : Math.floor(255-(percent*2-100)*255/100);
                g = percent>50 ? 255 : Math.floor((percent*2)*255/100);
                return 'rgb('+r+','+g+',0)';
            }
    
    0 讨论(0)
提交回复
热议问题