Sha*_*wal 9 c# wpf colors system.windows.media
我正在开发wpf应用程序.我在C#中拥有Color对象的实例.假设我有一个红色Color对象的实例,即Color c = Color.FromArgb(255,255,0,0)现在假设我有一个值,范围从1到10.所以根据这个值,我想改变'c'对象的颜色.我希望浅红色为1,暗红色为10.浅红色变为暗,因为值从1增加.如何在C#中为wpf应用程序执行此操作?能否请您提供我可以解决上述问题的任何代码或链接?
您可以尝试简单地将红色,绿色和蓝色分量乘以某个系数.
public static Color ChangeLightness(this Color color, float coef)
{
return Color.FromArgb((int)(color.R * coef), (int)(color.G * coef),
(int)(color.B * coef));
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您想使用1到10之间的整数值而不是系数:
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(color.A, (int)(color.R * coef), (int)(color.G * coef),
(int)(color.B * coef));
}
Run Code Online (Sandbox Code Playgroud)
如果您有一定数量的值,那么 Style DataTrigger 怎么样?
https://www.google.co.uk/search?q=c%23+wpf+style+datatrigger
<Button>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding NameOfYourProperty}" Value="0">
<Setter Property="Background"
Value="#FF000000" />
</DataTrigger>
<DataTrigger Binding="{Binding NameOfYourProperty}" Value="1">
<Setter Property="Background"
Value="#FF110000" />
</DataTrigger>
<DataTrigger Binding="{Binding NameOfYourProperty}" Value="2">
<Setter Property="Background"
Value="#FF220000" />
</DataTrigger>
( etc ... )
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Run Code Online (Sandbox Code Playgroud)
然后,如果您需要重用该样式,那么您可以放入<Resources>窗口/用户控件的部分。
| 归档时间: |
|
| 查看次数: |
13632 次 |
| 最近记录: |