将三种颜色混合在一起
我正在编写一种算法来生成图像。
<伪代码>
color1 = (7, 46, 76)
color2 = (119, 230, 161)
color3 = (148, 129, 134)
for every x, y in image {
// do a crazy calculation to get a value based on x, y
// this number is between 0 and 1
float f = (output of crazy calculation)
// part 1: finding the color between color 1 and color 2
// according to f.
float r = c1.r + (c2.r - c1.r) * f;
float g = c1.g + (c2.g - c1.g) * f;
float b = c1.b + (c2.b - c1.b) * f;
}
现在,这很好。我可以将R,G和B分配给像素,并在这两种颜色之间得到一些东西。问题是,它真的很无聊。
我想做的是将这种颜色与三分之一混合。我一直在阅读的一篇文章说:
“从基于f的简单颜色坡道开始,然后根据q的幅度将颜色混合到第三个颜色, - q是一个vector2,其值在之间0和
。
100 “是color1和color2。不知何故,我需要在color3中混合。
我不知道这篇文章的含义。我知道大小是sqrt(qx^2 + qy^2),但我不知道它如何希望我与(r,g,b)颜色在Color3中根据此值混合。
您如何混合第三颜色?
谢谢, rylan
I'm writing an algorithm to generate images procedurally.
< Pseudocode >
color1 = (7, 46, 76)
color2 = (119, 230, 161)
color3 = (148, 129, 134)
for every x, y in image {
// do a crazy calculation to get a value based on x, y
// this number is between 0 and 1
float f = (output of crazy calculation)
// part 1: finding the color between color 1 and color 2
// according to f.
float r = c1.r + (c2.r - c1.r) * f;
float g = c1.g + (c2.g - c1.g) * f;
float b = c1.b + (c2.b - c1.b) * f;
}
This works great for now. I can assign r, g, and b to a pixel and get something that is between those two colors. The problem is, its really boring.
What I want to do is mix this color with a third. An article I've been reading says to do this:
"start from a simple color ramp based on f, then mix the color to a third one based on the magnitude of q" <- q is a vector2, whose values are between 0 and 100.
^ https://iquilezles.org/articles/warp/
My "Color Ramp" is color1 and color2. Somehow, I need to mix in color3.
I don't know what the article means. I know magnitude is sqrt(qx^2 + qy^2), but I don't know how it wants me to mix in color3 with the (r, g, b) color according to this value.
How would you mix in a third color?
Thanks,
Rylan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
伪代码中的以下引用是根据分数编号线性插值两种颜色。
此操作通常称为“ LERP”。在GLSL中,相同的操作称为
MIX
。我认为这篇文章建议您以混合
C1
和C2
的结果为例,然后根据q ,按照您描述的计算。
从您的伪代码开始,并假设我们在变量
QM
中具有Q
的幅度,则可以像这样写第三颜色的混合将
QM
缩小为0到1之间,或者将其夹在1中最多为1。The following quote from your pseudo code is linearly interpolating two colors together based upon a fractional number.
This operation is called often called “lerp” for short. In GLSL the same operation is called
mix
.I think the article is suggesting that you take the result of mixing
c1
andc2
and then mix that with a third color based on the magnitude ofq
, calculated as you described.Following on from your pseudo code, and assuming that we have the magnitude of
q
in a variableqm
, mixing in the third color could be written like this:You may want to either scale down
qm
so it is between 0 and 1, or clamp it to be at most 1.