HSL插值
如果我的 HSL 颜色的色调分量以度为单位,我如何在两种色调之间正确插值?使用 (h1 + h2) / 2 似乎并不能在所有情况下产生理想的结果。以下两个示例说明了原因:
令:
红色 = 0°
黄色 = 60°
蓝色 = 240°
(红色 + 黄色)/2 = 30°(橙色)
(黄 + 蓝) / 2 = 150° (蓝绿)
(红色 + 蓝色)/ 2 = 120°(绿色)
如您所见,平均红色和蓝色会产生绿色,而不是紫色/洋红色!然而,如果我们让:
红色 = 360°
黄色 = 60°
蓝色 = 240°
(红色 + 黄色)/2 = 210°(蓝绿色)
(黄 + 蓝) / 2 = 150° (蓝绿)
(红色 + 蓝色) / 2 = 300° (洋红色)
现在,红色和蓝色产生预期的颜色,但红色和黄色产生蓝绿色!我可以通过什么方式插入颜色的色调分量,以便结果与混合实际油漆的预期相匹配?谢谢!
If the hue component of my HSL color is in degrees, how can I correctly interpolate between two hues? Using (h1 + h2) / 2 does not seem to produce desirable results in all cases. Here are two examples that illustrate why:
Let:
red = 0°
yellow = 60°
blue = 240°
(red + yellow) / 2 = 30° (orange)
(yellow + blue) / 2 = 150° (blue green)
(red + blue) / 2 = 120° (green)
As you can see, averaging red and blue produces green instead purple/magenta! However, if we let:
red = 360°
yellow = 60°
blue = 240°
(red + yellow) / 2 = 210° (blue green)
(yellow + blue) / 2 = 150° (blue green)
(red + blue) / 2 = 300° (magenta)
Now, red and blue produce the expected color, but red and yellow produce a blue green color! In what way can I interpolate the hue component of the color so that the result matches what would be expected of mixing actual paint? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你基本上这样做是为了得到我认为你想要的东西,这是一种看起来介于两个输入之间的中间颜色,但你只需要处理这个计算是在一个包裹的圆上完成的事实。 (我假设你并不真正想要“像油漆一样混合”,而只是想要一种中间颜色——即你不希望红色和绿色变成棕色,对吧?)
这是一个Python函数,它将选择两个角度之间的角度在一个圆圈上。它基本上只是尝试两个选项(添加或不添加 360)并选择一个最接近原始角度的选项:
产生:(
我觉得应该有一种更简单的方法来进行此计算,但没有想到我。)
You're basically doing this right to get what I assume you want, which is a color that looks midrange between your two inputs, but you just need to deal with the fact that this calculation is done on a circle which wraps. (I assume that you don't really want "mixing like paint" but just a midrange color -- i.e. you don't want red and green to give brown, right?)
Here's a Python function that will pick the angle between two angles on a circle. It basically just tries the two options (adding 360 or not) and picks the one that gives the angle closest to the original angles:
produces:
(I feel there should be an easier way to do this calculation, but it didn't occur to me.)
对于每个颜色值 x,x
x 360
,可以是x
或x+360
。这样您就有了 4 对颜色值。如果找到最小距离对,则使用该对来插值颜色。它可能会给您带来正确的视觉效果。例如,
红色 = 0° 或 360°
黄色 = 60° 或 420°
蓝色 = 240° 或 600°
红色和黄色的最小距离为 60(红色 0 黄色 60),因此插值应为 30。
黄色和蓝色的最小距离是180(黄60蓝240),所以插值应该是150。
红蓝最小距离为60(红360蓝240),因此插值应为300。
For every color value
x, x < 360
, it could bex
orx+360
. You then have 4 pairs of color values. If you find the smallest distance pair, then use the pair to interpolate the color. It might give you the right visual effect.For example,
red = 0° or 360°
yellow = 60° or 420°
blue = 240° or 600°
the smallest distance of red and yellow is 60(red 0 yellow 60), so the interpolation should be 30.
the smallest distance of yellow and blue is 180(yellow 60 blue 240), so the interpolation should be 150.
the smallest distance of red and blue is 60(red 360 blue 240), so the interpolation should be 300.
您可能不想听到这个,但转换为 RGB,插值并转换回来。
编辑:刚刚看到“需要混合实际油漆”
在这种情况下,您可能想要转换为 CMY 色彩空间而不是 RGB。查看维基百科上的颜色混合。
You probably didn't want to hear this, but convert to RGB, interpolate and convert back.
edit: just saw "would be expected of mixing actual paint"
In that case you probably want to convert to CMY colour space rather than RGB. Have a look at colour mixing on Wikipedia.