如何使基于WPF中值特定的颜色变亮或变暗? [英] How to make specific color darken or lighten based on value in wpf?
问题描述
我开发WPF应用程序。我有颜色对象在C#中的实例。假设我有红色的对象实例即颜色C = Color.FromArgb(255,255,0,0)
现在假设我有一个值范围为1〜10。因此在此基础上的价值我想改变'C'对象的颜色。我要为1淡红色,暗红色10.淡红色变成暗从1值增大我怎样才能做到这一点在C#中的WPF应用程序?能否请您给我提供任何代码或链接,通过它我可以解决上述问题?
I am developing wpf application. I am having the instance of Color object in C#. Suppose I have instance of red Color object i.e. Color c = Color.FromArgb(255,255,0,0)
Now suppose that I have one value which ranges from 1 to 10. So based on this value I want to change the color of the 'c' object. I want light red for 1 and dark red for 10. The light red becomes the dark as the value increases from 1. How can I do this in C# for wpf application ? Can you please provide me any code or link through which I can resolve the above issue ?
推荐答案
您可以尝试简单地乘以红,绿色和蓝色成分被一些系数。
You can try to simply multiply red, green and blue components by some coefficient.
public static Color ChangeLightness(this Color color, float coef)
{
return Color.FromArgb((int)(color.R * coef), (int)(color.G * coef),
(int)(color.B * coef));
}
或者,如果你想使用整数1值10代替系数:
Or, if you'd like to use an integer value from 1 to 10 instead of the coefficient:
private const int MinLightness = 1;
private const int MaxLightness = 10;
private const float MinLightnessCoef = 1f;
private const float MaxLightnessCoef = 0.4f;
public static Color ChangeLightness(this Color color, int lightness)
{
if (lightness < MinLightness)
lightness = MinLightness;
else if (lightness > MaxLightness)
lightness = MaxLightness;
float coef = MinLightnessCoef +
(
(lightness - MinLightness) *
((MaxLightnessCoef - MinLightnessCoef) / (MaxLightness - MinLightness))
);
return Color.FromArgb((int)(color.R * coef), (int)(color.G * coef),
(int)(color.B * coef));
}
这篇关于如何使基于WPF中值特定的颜色变亮或变暗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!