在ASP.Net Web应用程序表中更改颜色

时间:2016-09-01 13:51:28

标签: html asp.net-mvc colors html-table

不是很熟悉这个,但我会试一试,所以我在Visual Studio中有一个ASP.Net mvc web应用程序,在我的一个视图中,查看详细信息我有一个故事,根据呈现的颜色呈现下面。这个刻度只有绿色和红色,我希望它是红色的0%黄色50%和绿色到100%,并且它之间的变化,所以每个说75%将是绿色/黄色。我可以用任何方式改变下面的代码(没有改变太多)来解决这个问题吗? (如果我解释得不好,任何帮助都非常感谢和抱歉)

    float colorPercentage = ((float)status.Status) / 100;
    int red = (int)(255 * (1 - colorPercentage));
    int green = (int)(255 * colorPercentage);

    <tr>
        <td><a href="#@practiceName">@practiceName</a></td>
        <td style="background-color:rgb(@red,@green,0);color:white">@status.Status%</td>
        <td>@status.Comment</td>
    </tr>

1 个答案:

答案 0 :(得分:0)

如果我理解正确你想根据状态值改变颜色吗?你能举个例子吗?

如果状态为45,那么红色和绿色的值是多少?如果状态为75,那么红色和绿色的值是多少?

如果您想要根据状态增加绿色而红色减少,您可以执行以下操作:

green = (255 / (1/colorPercentage)); //green increases as per the colorPercentage's value
red = 255 - green; (if green is 200 you will get red as 55)

如果您将某些预期值与状态示例放在一起,那么解决该问题将会很有帮助。

<强>更新

嗨,请参阅以下代码段,您可以实现类似的功能。      public void printColours(double apercentage)         {             双倍百分比=百分比/ 100;             double yellowPercent =(百分比<0.25)≤0:(百分比&gt; = 0.25&amp;&amp;百分比&lt; = 0.50)?百分比* 2 :((1.0 - 百分比)* 2);

        double red, yellow, green;

        red = 255 * (1.0 - percentage);
        green = 255 * (1.0 * percentage);
        yellow = 255 * yellowPercent;

        System.Diagnostics.Debug.WriteLine(string.Format("Red: {0}, Green: {1} and Yellow: {2} for Percentage of: {3}", red, green, yellow,percentage));
        System.Diagnostics.Debug.WriteLine(yellowPercent);

    }

您可以通过

进行测试
        printColours(10.00);
        printColours(25.00);
        printColours(40.00);
        printColours(50.00);
        printColours(60.00);
        printColours(65.00);
        printColours(75.00);
        printColours(95.00);
        printColours(100.00);

我从这个方法得到以下结果,我认为应该足够了,可以根据需要随意改变。

Red: 229.5, Green: 25.5 and Yellow: 0 for Percentage of: 0.1
Red: 191.25, Green: 63.75 and Yellow: 127.5 for Percentage of: 0.25
Red: 153, Green: 102 and Yellow: 204 for Percentage of: 0.4
Red: 127.5, Green: 127.5 and Yellow: 255 for Percentage of: 0.5
Red: 102, Green: 153 and Yellow: 204 for Percentage of: 0.6
Red: 89.25, Green: 165.75 and Yellow: 178.5 for Percentage of: 0.65       
Red: 63.75, Green: 191.25 and Yellow: 127.5 for Percentage of: 0.75
Red: 12.75, Green: 242.25 and Yellow: 25.5 for Percentage of: 0.95
Red: 0, Green: 255 and Yellow: 0 for Percentage of: 1

希望这有帮助