WPF颜色插值 [英] WPF Color interpolation

查看:158
本文介绍了WPF颜色插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制一个WPF控件的背景基于调色板,其中每个颜色已分配值(例如红= 0,暗绿= 10,绿色= 20,LightGreen = 30)和用户选择的值25),其将给出所得到的颜色。我希望生成的颜色是2个最接近的颜色值之间的插值(例如,值为25,它应该在绿色和LightGreen之间给出颜色)



我想在WPF中使用现有的LinearGradientBrush;设置GradientStops,偏移并获得指定值的颜色。是否有办法做到这一点,还是应该实现自己的颜色插值函数?



谢谢。

使用LinearGradientBrush听起来像会有一点开销。没有知识。



我假设你的调色板的值为了简单起见可以被10整除。

  public static Color GetColor(int value)
{
int startIndex =(value / 10)* 10;
int endIndex = startIndex + 10;

color startColor = Palette [startIndex];
color endColor = Palette [endIndex];

float weight =(value - startIndex)/(float)(endIndex - startIndex);

return Color.FromArgb(
(int)Math.Round(startColor.R *(1 - weight)+ endColor.R * weight),
(int) Round(startColor.G *(1 - weight)+ endColor.G * weight),
(int)Math.Round(startColor.B *(1- weight)+ endColor.B * weight)

}

如果定义的颜色不能被10整除,发现开始和结束的颜色会更复杂一些。


I am trying to paint a WPF control's background based on a palette where each color has been assigned with values (e.g. Red = 0, DarkGreen = 10, Green = 20,LightGreen =30) and a user-selected value (e.g. 25) that would give the resulting color. I would like the resulting color to be an interpolation between the 2 nearest color values (e.g. for a value of 25 it should give a color between Green and LightGreen)

For that I'm thinking of using the existing LinearGradientBrush in WPF; set the GradientStops, offsets and get the color at a specified value . Is there a way to do this or should I implement my own color interpolation function?

Thanks.

解决方案

Using the LinearGradientBrush sounds like it would have a bit of an overhead. No knowledge though. A color interpolation function isn't that hard to write though.

I'm assuming your palettes have values that are divisible by 10 for simplicity.

public static Color GetColor(int value)
{
    int startIndex = (value/10)*10;
    int endIndex = startIndex + 10;

    Color startColor = Palette[startIndex];
    Color endColor = Palette[endIndex];

    float weight = (value - startIndex)/(float)(endIndex - startIndex);

    return Color.FromArgb(
        (int)Math.Round(startColor.R * (1 - weight) + endColor.R * weight),
        (int)Math.Round(startColor.G * (1 - weight) + endColor.G * weight),
        (int)Math.Round(startColor.B * (1 - weight) + endColor.B * weight));

}

If the defined colors are not divisible by 10 the logic to find the start and end colors will be a bit more complex.

这篇关于WPF颜色插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆