简体   繁体   English

生成颜色渐变

[英]Generating Color Gradient

I want to display to my user how many (percent wise) of their forms are compliant with the new standard. 我想向我的用户显示他们的表格中有多少(符合百分比)符合新标准。 The way I want to let them know visually is the percent amount will be colored. 我想让他们直观地知道的方式是百分比颜色。 It will be 0xFF0000 (pure red) for 0% and 0x00FF00 (pure green) at 100%. 对于100%,它将是0xFF0000(纯红色),对于100%,它将是0x00FF00(纯绿色)。 What is the best way to calculate the color for each step along the way? 计算过程中每个步骤的颜色的最佳方法是什么?

Colour space conversion (as suggested by Tony ) will give you the best results. 色彩空间转换(由Tony建议)将为您带来最佳效果。 If however this is beyond the scope of what you are looking for, I suggest a simple algorithm that gets you yellow (0xFFFF00) for 50 %: 但是,如果这超出了您所寻找的范围,我建议使用一种简单的算法,使您黄(0xFFFF00)达到50%:

For values up to 50 % Start with 0xFF0000. 对于高达50%的值,从0xFF0000开始。
Add 0xFF * Percentage / 50 to the green component. 将0xFF *百分比/ 50添加到绿色部分。

For values above 50 % Start with 0xFFFF00. 对于50%以上的值,从0xFFFF00开始。
Subtract 0xFF * Percentage / 50 from the red component. 从红色分量中减去0xFF *百分比/ 50。

The results look good enough for my customers ;-) 结果对我的客户来说足够好;-)

You don't need to calculate it yourself - try using a LinearGradient brush. 您不需要自己计算-尝试使用LinearGradient笔刷。 ( msdn ) 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM