3 种颜色之间的颜色插值
我使用以下方程获得从 colorA 到 colorB 的漂亮颜色渐变,但我不知道如何对 3 种颜色执行相同的操作,因此渐变从 colorA 到 colorB 再到 colorC
colorT = colorA * p + colorB * (1.0 - p);
其中“p”是从 0.0 到 1.0 的百分比
谢谢
I use the following equation to get a nice color gradient from colorA to colorB, but I have no idea how to do the same for 3 colors, so the gradient goes from colorA to colorB to colorC
colorT = colorA * p + colorB * (1.0 - p);
where "p" is the a percentage from 0.0 to 1.0
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
谢谢你的公式。但我必须对其进行一些修改,因为它没有在 3 种颜色之间正确插值(颜色变化有跳跃),
以下是修复方法:
Thanks for the formula. But I had to make some modifications to it, as it didn't interpolate between the 3 colors properly (there was jumps in color change)
Here is the fix for that:
好吧,对于 3 种颜色,您可以使用 p = 0.0 到 2.0 进行相同操作:
或者对其进行缩放,以便您仍然可以使用 p = 0.0 到 1.0:
Well, for 3 colors, you can just to the same with p = 0.0 to 2.0:
Or scale it so you can still use p = 0.0 to 1.0:
可能可以为此构造一个表达式,但最简单的方法是使用一个条件来使用不同的表达式,具体取决于您是在范围的 A - B 部分还是 B - C 部分:
It might be possible to construct a single expression for that, but the simplest is to use a condition to use different expressions depending on whether you are in the A - B part or B - C part of the range:
一种可能的解决方案是通过贝塞尔曲线使用插值:
http://en.wikipedia.org/wiki/B%C3%A9zier_curve
如果您查看特殊情况的二次贝塞尔曲线,您可以看到一个在您的情况下的 3 个点或颜色之间进行插值的公式。
这是线性公式的概括。
编辑:
不过,第二个方法不会得到你的结果,因为中间点从未被触及。
要获得接触所有点(颜色)的平滑曲线,您必须使用样条线 http://en .wikipedia.org/wiki/Spline_interpolation
one possible solution is to use interpolation via Bézier Curve:
http://en.wikipedia.org/wiki/B%C3%A9zier_curve
if you look at the special case Quadratic Bézier Curve, you can see a formula that interpolate between 3 points, or colors in your case.
This is a generalization of your linear formula.
EDIT:
on second though, this method doesn't get your results, as the intermediate point is never touched.
To get a smooth curve that touch all of your points (colors) you have to use a spline http://en.wikipedia.org/wiki/Spline_interpolation
您希望能够创建 3 种颜色但相同的渐变吗?完全相同:完成此渐变后,开始一个新渐变,其中 colorA 是当前 colorB,colorB 是新颜色。附加结果即可完成:
colorA ---- colorB colorB ---- colorC
祝你好运!
You want to be able to create 3 color but equal gradients? Exactly the same: after you're done with this gradient, start a new one where colorA is the current colorB and colorB is the new color. Append the results and you're done:
colorA ---- colorB colorB ---- colorC
Good luck!