.NET中的3种颜色之间的颜色插值 [英] Color Interpolation Between 3 Colors in .NET

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

问题描述

我想根据某个变量的值,从颜色A(我们称之为红色)到颜色C(我们称之为绿色)穿过颜色B(我们称之为黄色)平滑地插入颜色。 p>

如果变量= 100,我想要纯绿色。
如果变量= 50,我想要纯黄色。
如果变量= 0,我想要纯红色。



我理解你可以将每个RGB三元组作为三维空间中的坐标。我正在寻找的是一个快速和脏的线性插值技巧,与.NET颜色类型(ARGB的单独的值等)的特定布局干净地工作。

  class ColorInterpolator {
delegate byte ComponentSelector(Color color);
static ComponentSelector _redSelector = color => color.R;
static ComponentSelector _greenSelector = color => color.G;
static ComponentSelector _blueSelector = color =>颜色B.

public static Color InterpolateBetween(
Color endPoint1,
Color endPoint2,
double lambda){
if(lambda< 0 || lambda> 1){
throw new ArgumentOutOfRangeException(lambda);
}
颜色color = Color.FromRgb(
InterpolateComponent(endPoint1,endPoint2,lambda,_redSelector),
InterpolateComponent(endPoint1,endPoint2,lambda,_greenSelector),
InterpolateComponent(endPoint1,endPoint2,lambda,_blueSelector)
);

返回颜色;
}

静态字节InterpolateComponent(
Color endPoint1,
Color endPoint2,
double lambda,
ComponentSelector selector){
return(byte)(selector(endPoint1)
+(selector(endPoint2) - selector(endPoint1))* lambda);
}
}

如果颜色B不是颜色A和颜色C之间的中点?最简单的方法如下。如果参数(我所说的 lambda )小于 0.5 ,乘以 lambda 乘以2,并返回颜色A和颜色B之间的插值颜色。如果参数大于 0.5 ,则乘以 lambda 减两(这将 [0.5,1] 映射到 [0,1] ),并返回颜色B和颜色C之间的插值颜色。



如果你不喜欢颜色B居住在颜色A和颜色之间的线C,那么你可以使用我刚才描述的修改,在颜色之间做一个分段线性插值。



最后,你没有指定是否要插值所谓的α值(ARGB中的A)。上面的代码很容易修改,以处理这种情况。再添加一个 ComponentSelector 定义为 color => color.A ,使用 InterpolateComponent 插值此值,并使用 http://msdn.microsoft.com/en-us/library/system.drawing.color.fromargb.aspx =nofollow noreferrer> Color.FromArgb


I would like to smoothly interpolate color from Color A (let's call it red) to Color C (let's call it green) going through color B (let's call it yellow), based on the value of a certain variable.

If the variable = 100, I want pure green. If the variable = 50, I want pure yellow. If the variable = 0, I want pure red.

I understand you can treat each RGB triplet as a coordinate in 3-dimensional space. What I'm looking for is a quick-and-dirty linear interpolation trick that works cleanly with the specific layout of the .NET Color type (separate values for ARGB etc).

解决方案

First, you ask for linear interpolation but you don't specify that color B lives on the line between color A and color C; this is necessary. Second, you didn't specify but I am going to make a simplifying assumption that color B is the midpoint of the line between color A and color C; the following code is easily modified if this is not true. Lastly, I changed your assumption that the parameter be an integer between zero and one-hundred to be a double between zero and one. The code is easier to write and easier to understand in the latter case, and can still be used with the former (divide your inputs by one-hundred).

class ColorInterpolator {
    delegate byte ComponentSelector(Color color);
    static ComponentSelector _redSelector = color => color.R;
    static ComponentSelector _greenSelector = color => color.G;
    static ComponentSelector _blueSelector = color => color.B;

    public static Color InterpolateBetween(
        Color endPoint1,
        Color endPoint2,
        double lambda) {
        if (lambda < 0 || lambda > 1) {
            throw new ArgumentOutOfRangeException("lambda");
        }
        Color color = Color.FromRgb(
            InterpolateComponent(endPoint1, endPoint2, lambda, _redSelector),
            InterpolateComponent(endPoint1, endPoint2, lambda, _greenSelector),
            InterpolateComponent(endPoint1, endPoint2, lambda, _blueSelector)
        );

        return color;
    }

    static byte InterpolateComponent(
        Color endPoint1,
        Color endPoint2,
        double lambda,
        ComponentSelector selector) {
        return (byte)(selector(endPoint1)
            + (selector(endPoint2) - selector(endPoint1)) * lambda);
    }
}

How do you modify this if color B is not the midpoint between color A and color C? The easiest way is the following. If the parameter (what I call "lambda") is less than 0.5, multiply lambda by two and return the interpolated color between color A and color B. If the parameter is greater than 0.5, multiply lambda by two and subtract one (this maps [0.5, 1] onto [0, 1]) and return the interpolated color between color B and color C.

If you don't like the requirement that color B live on the line between color A and color C, then you can use exactly the modification that I just described to do a piecewise-linear interpolation between the colors.

Finally, you did not specify if you want to interpolate the so-called alpha value (the 'A' in "ARGB"). The above code is easily modified to handle this situation too. Add one more ComponentSelector defined as color => color.A, use InterpolateComponent to interpolate this value and use the Color.FromArgb(int, int, int, int) overload of Color.FromArgb.

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

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