2010-06-08 170 views
2

我想向我的用户展示他们的窗体中有多少(明智的)符合新标准。我想让他们知道的方式是百分比数量将被着色。 0%为0xFF0000(纯红色),100%为0x00FF00(纯绿色)。计算每个步骤的颜色的最佳方法是什么?生成颜色渐变

+0

http://stackoverflow.com/questions/668263/algorithm-question-need-to-dynamically-increment-from-00ff00-to-ff0000-超过时间/ 669786#669786 – 2010-06-08 14:43:43

+1

大概新标准与可访问性无关,因为您的一些用户将无法辨别红色和绿色之间的区别。 – 2010-06-08 14:44:44

+0

是啊,绿色<->红色对于色盲患者来说会很难。 – 2010-06-08 14:46:48

回答

3

色彩空间转换(由Tony的建议)会给你最好的结果。 但是,如果这超出了你所寻找的范围,我建议一个简单的算法,让你黄色(0xFFFF00)为50%:

对于高达50%的值从0xFF0000开始。
添加0xFF的*百分比/ 50的绿色成分。

对于上面0xFFFF00 50%开始的值。
减为0xFF *百分比/ 50从红色分量。

结果看起来不够好为我的客户;-)

1

你并不需要自己进行计算 - 尝试使用LinearGradient刷。 (msdn

LinearGradientBrush linGrBrush = new LinearGradientBrush(
    new Point(0, 10), 
    new Point(200, 10), 
    Color.FromArgb(255, 255, 0, 0), // Opaque red 
    Color.FromArgb(255, 0, 0, 255)); // Opaque blue 

Pen pen = new Pen(linGrBrush); 

e.Graphics.DrawLine(pen, 0, 10, 200, 10); 
e.Graphics.FillEllipse(linGrBrush, 0, 30, 200, 100); 
e.Graphics.FillRectangle(linGrBrush, 0, 155, 500, 30);