将三种颜色混合在一起

发布于 2025-02-04 18:39:08 字数 896 浏览 4 评论 0原文

我正在编写一种算法来生成图像。

<伪代码>


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 技术交流群。

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

发布评论

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

评论(1

坠似风落 2025-02-11 18:39:08

伪代码中的以下引用是根据分数编号线性插值两种颜色。

 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;

此操作通常称为“ LERP”。在GLSL中,相同的操作称为MIX

我认为这篇文章建议您以混合C1C2的结果为例,然后根据q ,按照您描述的计算。

从您的伪代码开始,并假设我们在变量QM中具有Q的幅度,则可以像这样写第三颜色的

 r = r + (c3.r - r) * qm;
 g = g + (c3.g - g) * qm;
 b = b + (c3.b - b) * qm;

混合将QM缩小为0到1之间,或者将其夹在1中最多为1。

The following quote from your pseudo code is linearly interpolating two colors together based upon a fractional number.

 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 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 and c2 and then mix that with a third color based on the magnitude of q, calculated as you described.

Following on from your pseudo code, and assuming that we have the magnitude of q in a variable qm, mixing in the third color could be written like this:

 r = r + (c3.r - r) * qm;
 g = g + (c3.g - g) * qm;
 b = b + (c3.b - b) * qm;

You may want to either scale down qm so it is between 0 and 1, or clamp it to be at most 1.

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