三色在.NET之间色彩插值 [英] Color Interpolation Between 3 Colors in .NET

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

问题描述

我想平滑地插入颜色从颜色A(姑且称之为红)为颜色C(我们称之为绿色)经过彩色B(姑且称之为黄色),基于一定的变量的值。

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.

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

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

我知道你可以将每个RGB三元组在3维空间中的坐标。我正在寻找的是一个快速和肮脏的线性插值把戏,使用.NET颜色类型的具体布局干净利落的作品(独立值ARGB等)。

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).

推荐答案

首先,你问的线性内插,但你不指定彩色B住在颜色A和颜色C之间的线;这是必要的。其次,你没有具体说明,但我准备做一个简化的假设,彩色B是颜色A和颜色C之间的线的中点;以下code很容易修改,如果这是不正确的。最后,我改变了你假设参数是零和一百个之间的整数是双零和一之间。的code是容易编写和更容易在后一种情况下,了解,并仍可与前者使用(由一百个划分的输入)。

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);
    }
}

您如何修改这个,如果彩色B不是颜色A和颜色C之间的中点?最简单的方法如下。如果参数(我称之为的λ)小于 0.5 ,乘拉姆达由两个并返回色彩A和色彩B之间的插值颜色。如果参数大于 0.5 ,乘拉姆达由两减一(此地图 [0.5,1] [0,1] ),并返回彩色B超和彩色C之间的插值颜色。

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.

如果你不喜欢彩色B住在颜色A和颜色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.

最后,你并没有说明,如果你想插(以ARGB的A),即所谓的α值。上述code很容易修改,以处理这种情况了。再添加一个与ComponentSelector 定义为颜色=&GT; color.A ,使用 InterpolateComponent 来插值此值,并使用的 Col​​or.FromArgb(INT,INT,INT,INT) 中的 Col​​or.FromArgb

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之间色彩插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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