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

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

问题描述

我想根据某个变量的值将颜色从颜色 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 之间的线的中点;如果这不是真的,下面的代码很容易修改.最后,我将参数是零到一百之间的整数的假设更改为零到一之间的双精度数.后一种情况的代码更易于编写和理解,并且仍然可以与前者一起使用(将您的输入除以一百).

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 之间的中点,您如何修改?最简单的方法如下.如果参数(我称之为lambda")小于 0.5,则将 lambda 乘以 2 并返回颜色 A 和颜色 B. 如果参数大于 0.5,则将 lambda 乘以 2 并减去 1(这将 [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.

最后,您没有指定是否要插入所谓的 alpha 值(ARGB"中的A").上面的代码也很容易修改以处理这种情况.再添加一个 ComponentSelector 定义为 color =>;color.A,使用 InterpolateComponent 插入此值并使用 Color.FromArgb(int, int, int, int) 重载 Color.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 中 3 种颜色之间的颜色插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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