【问题标题】:Display colours algorithmically instead of hard defining values以算法方式显示颜色,而不是硬定义值
【发布时间】:2011-01-05 11:24:29
【问题描述】:

给定以毫秒为单位的 ping,是否有任何方法可以通过算法计算出阴影,我们可以在不硬编码 if 和颜色的情况下进行此 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 值,并在 Low 和 High 之间进行简单的线性插值。看起来不错,我用过这个。
  • HSV 坐标中的插值通常看起来更好一些,即从绿色到红色的过渡通过黄色而不是褐色。

标签: algorithm colors range


【解决方案1】:

颜色是一个多维空间,因此您必须选择如何完成插值。不同寻常的是,仅以 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,浮动,浮动)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多