我想根据0到100的值显示颜色。在一端(100),它是纯红色,在另一端(0),是纯绿色。在中间(50),我想让它变成黄色。
并且我希望颜色逐渐从一个渐变到另一个,如在75处,颜色为一半红色和一半黄色等等。
如何编写RGB值以反映这种渐变?谢谢。
并且我希望颜色逐渐从一个渐变到另一个,如在75处,颜色为一半红色和一半黄色等等。
如何编写RGB值以反映这种渐变?谢谢。
我有同样的需求,我刚刚用这个方法解决了:
myColor = new Color(2.0f * x, 2.0f * (1 - x), 0);
解释:
与其关注[0-255]范围,让我们关注颜色分量的[0.0-1.0]范围:QColor(min(255,2*255*(temp)),min(255,2*255*(1-temp)),0)
- laserpython颜色的RGB值为:
在红色和黄色之间,等距离地将绿色通道增加,直到达到255。在黄色和绿色之间,等距离地从红色通道减去。
这里提供了一种非常简单的颜色分量线性插值方法,可能可以满足您的需求。
public Color GetBlendedColor(int percentage)
{
if (percentage < 50)
return Interpolate(Color.Red, Color.Yellow, percentage / 50.0);
return Interpolate(Color.Yellow, Color.Lime, (percentage - 50) / 50.0);
}
private Color Interpolate(Color color1, Color color2, double fraction)
{
double r = Interpolate(color1.R, color2.R, fraction);
double g = Interpolate(color1.G, color2.G, fraction);
double b = Interpolate(color1.B, color2.B, fraction);
return Color.FromArgb((int)Math.Round(r), (int)Math.Round(g), (int)Math.Round(b));
}
private double Interpolate(double d1, double d2, double fraction)
{
return d1 + (d2 - d1) * fraction;
}
d1
和d2
方面帮助很大。 - Christian Ivicevic我不懂C#,所以这个答案只是一个建议性的方法。让x
表示从0
到100
的整数。类似这样的代码应该可以工作:
red = (x > 50 ? 1-2*(x-50)/100.0 : 1.0);
green = (x > 50 ? 1.0 : 2*x/100.0);
blue = 0.0
这个想法是从红色(1.0,0.0,0.0)
开始。然后增加绿色变成黄色:(1.0,1.0,0.0)
。接着减少红色变成绿色:(0.0,1.0,0.0)
。
编辑:下面是C#代码:
static Color GetColorFromRedYellowGreenGradient(double percentage)
{
var red = (percentage > 50 ? 1 - 2 * (percentage - 50) / 100.0 : 1.0) * 255;
var green = (percentage > 50 ? 1.0 : 2 * percentage / 100.0) * 255;
var blue = 0.0;
Color result = Color.FromArgb((int)red, (int)green, (int)blue);
return result;
}
简化的扩展方法;
public static Color Interpolate(this Color source, Color target, double percent)
{
var r = (byte)(source.R + (target.R - source.R) * percent);
var g = (byte)(source.G + (target.G - source.G) * percent);
var b = (byte)(source.B + (target.B - source.B) * percent);
return Color.FromArgb(255, r, g, b);
}
使用方法:
var low = 33.0;
var high = 100.0;
var color = Color.Red.Interpolate(Color.Green, low / high);
在我尝试了一段时间更真实的颜色后,这是我的公式:
public Color GetColorOf(double value, double minValue, double maxValue)
{
if (value == 0 || minValue == maxValue) return Color.White;
var g = (int)(240 * value / maxValue);
var r = (int)(240 * value / minValue);
return (value > 0
? Color.FromArgb(240 - g, 255 - (int)(g * ((255 - 155) / 240.0)), 240 - g)
: Color.FromArgb(255 - (int)(r * ((255 - 230) / 240.0)), 240 - r, 240 - r));
}
let myNewColor = UIColor().greenRedProgress(percent: Int))
extension UIColor{
func greenRedProgress(percent: Int) -> UIColor{
let modVal = CGFloat((Double(percent).truncatingRemainder(dividingBy: 50) / 50) * 255)
if percent <= 0{
return UIColor(red: 1.0, green: 0, blue: 0, alpha: 1)
}else if percent >= 100{
return UIColor(red: 0, green: 1.0, blue: 0, alpha: 1)
}else{
switch percent{
case 1..<50: return UIColor(red: 1.0, green: (modVal/255), blue: 0, alpha: 1)
case 51..<100: return UIColor(red: (255 - modVal)/255, green: 1.0, blue: 0, alpha: 1)
case 50: return UIColor(red: 1.0, green: 1.0, blue: 0, alpha: 1)
default: return UIColor(red: 0, green: 1.0, blue: 0, alpha: 1)
}
}
}}
public Color ColorBasedOnPercent(decimal percent, params Color[] colors)
{
if (colors.Length == 0)
{
//I am using Transparent as my default color if nothing was passed
return Color.Transparent;
}
if (percent > 1)
{
percent = percent / 100;
}
//find the two colors within your list of colors that the percent should fall between
var colorRangeIndex = (colors.Length - 1) * percent;
var minColorIndex = (int)Math.Truncate(colorRangeIndex);
var maxColorIndex = minColorIndex + 1;
var minColor = colors[minColorIndex];
if (maxColorIndex < colors.Length)
{
var maxColor = colors[maxColorIndex];
//get the differences between all the color values for the two colors you are fading between
var aScale = maxColor.A - minColor.A;
var redScale = maxColor.R - minColor.R;
var greenScale = maxColor.G - minColor.G;
var blueScale = maxColor.B - minColor.B;
//the decimal distance of how "far" this color should be from the minColor in the range
var gradientPct = colorRangeIndex - minColorIndex;
//for each piece of the color (ARGB), add a percentage(gradientPct) of the distance between the two colors
int getRGB(int originalRGB, int scale) => (int)Math.Round(originalRGB + (scale * gradientPct));
return Color.FromArgb(getRGB(minColor.A, aScale), getRGB(minColor.R, redScale), getRGB(minColor.G, greenScale), getRGB(minColor.B, blueScale));
}
return minColor;
}