.NET 中 3 种颜色之间的颜色插值
我想根据某个变量的值,将颜色从颜色 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您要求进行线性插值,但没有指定颜色 B 位于颜色 A 和颜色 C 之间的线上; 这是必要的。 其次,你没有指定,但我将做一个简化的假设,即颜色 B 是颜色 A 和颜色 C 之间的线的中点; 如果这不是真的,下面的代码很容易修改。 最后,我将参数为 0 到 100 之间的整数的假设更改为 0 到 1 之间的双精度数。 在后一种情况下,代码更容易编写和理解,并且仍然可以与前一种情况一起使用(将您的输入除以一百)。
如果颜色 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.AInterpolateComponent
插入此值并使用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).
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 than0.5
, multiplylambda
by two and return the interpolated color between color A and color B. If the parameter is greater than0.5
, multiplylambda
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 ascolor => color.A
, useInterpolateComponent
to interpolate this value and use theColor.FromArgb(int, int, int, int)
overload ofColor.FromArgb
.另一种使用高斯分布混合颜色的方法,如下所示(0.0 - 1.0 范围内的任意数量的颜色,以增加混合,增加 sigma_2 值)
更多信息 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)
More information http://en.wikipedia.org/wiki/Normal_distribution
Sample of blending 3 colors: