.NET 中 3 种颜色之间的颜色插值

发布于 2024-07-30 11:02:09 字数 248 浏览 7 评论 0原文

我想根据某个变量的值,将颜色从颜色 A(我们称其为红色)平滑地插值到颜色 C(我们称其为绿色),再经过颜色 B(我们称其为黄色)。

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

我知道您可以将每个 RGB 三元组视为 3 维空间中的坐标。 我正在寻找的是一种快速而肮脏的线性插值技巧,它可以与 .NET Color 类型的特定布局(ARGB 等的单独值)完美配合。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

居里长安 2024-08-06 11:02:09

首先,您要求进行线性插值,但没有指定颜色 B 位于颜色 A 和颜色 C 之间的线上; 这是必要的。 其次,你没有指定,但我将做一个简化的假设,即颜色 B 是颜色 A 和颜色 C 之间的线的中点; 如果这不是真的,下面的代码很容易修改。 最后,我将参数为 0 到 100 之间的整数的假设更改为 0 到 1 之间的双精度数。 在后一种情况下,代码更容易编写和理解,并且仍然可以与前一种情况一起使用(将您的输入除以一百)。

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 之间的中点,如何修改它? 最简单的方法如下。 如果参数(我称之为“lambda”)小于 0.5,则将 lambda 乘以 2,并返回颜色 A 和颜色 A 之间的插值颜色颜色 B。如果参数大于 0.5,则将 lambda 乘以 2 并减一(这会将 [0.5, 1] 映射到 [0, 1]) 并返回颜色 B 和颜色 C 之间的插值颜色。

如果您不喜欢颜色 B 位于颜色 A 和颜色 C 之间的直线上的要求,那么您可以使用正好我刚才描述的修改是在颜色之间进行分段线性插值。

最后,您没有指定是否要插入所谓的 alpha 值(“ARGB”中的“A”)。 上面的代码也可以很容易地修改来处理这种情况。 添加另一个定义为 color => 的 ComponentSelector color.A,使用 InterpolateComponent 插入此值并使用 Color.FromArgb(int, int, int, int) 重载 Color.FromArgb

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.

原谅我要高飞 2024-08-06 11:02:09

另一种使用高斯分布混合颜色的方法,如下所示(0.0 - 1.0 范围内的任意数量的颜色,以增加混合,增加 sigma_2 值)

public static Color InterpolateColor(Color[] colors, double x)
{
    double r = 0.0, g = 0.0, b = 0.0;
    double total = 0.0;
    double step = 1.0 / (double)(colors.Length - 1);
    double mu = 0.0;
    double sigma_2 = 0.035;

    foreach (Color color in colors)
    {                
        total += Math.Exp(-(x - mu) * (x - mu) / (2.0 * sigma_2)) / Math.Sqrt(2.0 * Math.PI * sigma_2);
        mu += step;
    }

    mu = 0.0;
    foreach(Color color in colors)
    {                
        double percent = Math.Exp(-(x - mu) * (x - mu) / (2.0 * sigma_2)) / Math.Sqrt(2.0 * Math.PI * sigma_2);
        mu += step;

        r += color.R * percent / total;
        g += color.G * percent / total;
        b += color.B * percent / total;
    }

    return Color.FromArgb(255, (int)r, (int)g, (int)b);
}

更多信息 http://en.wikipedia.org/wiki/Normal_distribution

混合 3 种颜色的示例:

在此输入图像描述

Another way to blend colors using Gaussian distribution like this (any number of colors for a range of 0.0 - 1.0, to increase blending increase sigma_2 value)

public static Color InterpolateColor(Color[] colors, double x)
{
    double r = 0.0, g = 0.0, b = 0.0;
    double total = 0.0;
    double step = 1.0 / (double)(colors.Length - 1);
    double mu = 0.0;
    double sigma_2 = 0.035;

    foreach (Color color in colors)
    {                
        total += Math.Exp(-(x - mu) * (x - mu) / (2.0 * sigma_2)) / Math.Sqrt(2.0 * Math.PI * sigma_2);
        mu += step;
    }

    mu = 0.0;
    foreach(Color color in colors)
    {                
        double percent = Math.Exp(-(x - mu) * (x - mu) / (2.0 * sigma_2)) / Math.Sqrt(2.0 * Math.PI * sigma_2);
        mu += step;

        r += color.R * percent / total;
        g += color.G * percent / total;
        b += color.B * percent / total;
    }

    return Color.FromArgb(255, (int)r, (int)g, (int)b);
}

More information http://en.wikipedia.org/wiki/Normal_distribution

Sample of blending 3 colors:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文