给定一个以毫秒为单位的 ping,是否有任何方法可以通过算法计算出我们可以在不对 ifs 和颜色进行硬编码的情况下制作此 ping 的阴影? IE 这个功能很好用:
function displayPing(lngThePingTime)
response.Write("<span style=""font-wight:bold;color:")
if(lngThePingTime < 50) then
response.Write("#77ff66")
elseif lngThePingTime < 100 then
response.Write("#22ee00")
elseif lngThePingTime < 150 then
response.Write("#33bb00")
elseif lngThePingTime < 250 then
response.Write("#ffaa00")
elseif lngThePingTime < 400 then
response.Write("#ee6600")
elseif lngThePingTime < 550 then
response.Write("#dd4400")
elseif lngThePingTime < 700 then
response.Write("#dd1100")
elseif lngThePingTime < 1000 then
response.Write("#990000")
else
response.Write("#660000")
end if
response.Write(""">")
response.Write lngThePingTime
response.Write("</span>")
end function
但是有什么方法可以让算法说:
Lowest Colour : #77ff66
Highest Colour: #660000
Cutoff Value: 1500 (any ping higher than this is fixed to highest colour)
这样颜色将是每个色度而不是一组固定的色度?
语言不重要,重要的是方法!
最佳答案
颜色是一个多维空间,因此您必须选择插值的方式。不同寻常的是,仅仅用 RGB 绘制一条“直线”作为尺寸并不能很好地工作。
一种简单的方法是使用 HSB/V/L color space .许多现代编程语言会自动转换成这个空间,例如参见[java Color methods][2]。否则数学不会太难。 wikipedia article explains it .
我遇到过这样的情况,它没有达到我想要的效果 - 例如,我想要一种交通灯样式“从红色到绿色通过琥珀色”。在这种情况下,我使用了以下代码,它“足够好”。如您所见,我利用了对 RGB 和颜色之间关系的一些了解。这不像您想要的那样可配置,但给出了您可以使用的那种方法的示例:
private static int makeTrafficLight(float fraction, int brightness) {
final float orange = 0.4f;
if(fraction < orange) {
int r = brightness;
float f = fraction/orange;
int g = (int)(brightness*(f/2.0f));
int b = 0;
return Color.rgb(r,g,b);
} else {
float f = ((fraction-orange)/(1.0f-orange));
int r = (int)(brightness*(1.0f-f));
int g = (int)(brightness*(f/2.0f+0.5f));
int b = 0;
return Color.rgb(r,g,b);
}
}
[2]: http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Color.html#HSBtoRGB(float , float , float )
关于algorithm - 通过算法显示颜色而不是硬定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4603518/