在 PHP 中生成 RGB 分级颜色的算法
我对在两种给定颜色之间生成“n”种分级颜色的算法很感兴趣,这些颜色在每种颜色之间生成平滑的过渡。
我尝试让两个通道静态,例如 R 和 G,并增量改变 B,但有时两种颜色之间的差异比相邻颜色更难。
我想检查不同的算法并分析它们的弱点和优点。
我编写了这段代码,看起来很合乎逻辑,但是某些颜色之间的转换比其他颜色之间的转换更难(例如,0 和 1 之间比 1 和 2 之间更难):
<?php
$c1 = array(128,175,27); // Color 1
$c2 = array(255,255,140); // Color 2
$nc = 5; // Number of colors to display.
$dc = array(($c2[0]-$c1[0])/($nc-1),($c2[1]-$c1[1])/($nc-1),($c2[2]-$c1[2])/($nc-1)); // Step between colors
for ($i=0;$i<$nc;$i++){
echo '<div style="width:200px;height:50px;background-color:rgb('.round($c1[0]+$dc[0]*$i).','.round($c1[1]+$dc[1]*$i).','.round($c1[2]+$dc[2]*$i).');">'.$i.'</div>'; // Output
}
?>
是否有更好的算法来做到这一点?
我举一个例子:在上面的代码中我使用了 $c1=array(192,5,248);
和 $c2 = array(142,175,240);
和 $nc = 10;
并获得此图像:
的 RGB 值0,1,8 和 9 是:
- 0 = 192,5,248
- 1 = 186,24,247
- 8 = 148,156,241
- 9 = 142,175,240
如果你观察一下,相邻颜色 6,19,1 之间存在差异。但 0 和 1 之间的视觉过渡比 8 和 9 之间的过渡更柔和。对于 HSV 来说也是一样。它是具有某些颜色的东西,其过渡更硬或更软。
I'm interesting in algorithms to generate 'n' graduated colors between two given colors, that generate smooth transitions between each of them.
I tried letting static two channels, for example R and G, and incremental change B, but sometimes the difference between two colors are harder than the neighbors have.
I want to check different algorithms and analyze their weakness and strenghts.
I wrote this code and it seems logic, but the transitions between some colors are harder than between other (e.g.between 0 and 1 is harder than between 1 and 2):
<?php
$c1 = array(128,175,27); // Color 1
$c2 = array(255,255,140); // Color 2
$nc = 5; // Number of colors to display.
$dc = array(($c2[0]-$c1[0])/($nc-1),($c2[1]-$c1[1])/($nc-1),($c2[2]-$c1[2])/($nc-1)); // Step between colors
for ($i=0;$i<$nc;$i++){
echo '<div style="width:200px;height:50px;background-color:rgb('.round($c1[0]+$dc[0]*$i).','.round($c1[1]+$dc[1]*$i).','.round($c1[2]+$dc[2]*$i).');">'.$i.'</div>'; // Output
}
?>
Are there a better algorithm to do this?
I bring an example: In the code above I used $c1=array(192,5,248);
and $c2 = array(142,175,240);
and $nc = 10;
and obtained this image:
The RGB values of 0,1,8 and 9 are:
- 0 = 192,5,248
- 1 = 186,24,247
- 8 = 148,156,241
- 9 = 142,175,240
If you look there is a diference between neighbor colors of 6,19,1. But the visually transition between 0 and 1 is softer than the transition between 8 and 9. And for HSV is the same thing. It is something with some colors that do its transition harder or softer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 HSV 中,生成 n 种颜色,其色调从第一种颜色到第二种颜色线性变化。将每个 HSV 值转换为 RGB。
In HSV, generate n colors whose hue changes linearly from the first color to the second color. Convert each HSV value to RGB.
在下图中,您可以看到我编写的一段代码的输出,该代码用于使用 RGB 和 HSV 分割以相等大小的步长来比较两种颜色之间的过渡:
我发现使用 HSV 的过渡受色调影响,并且取决于颜色之间的距离。如果您选择具有相同色调的两种颜色,很有趣的是,HSV 过渡比 RGB 更清晰,因为您只使用饱和度和明度(黑色),而没有像 RGB 那样添加颜色。
In the following image you can see the output of a piece of code I wrote to compare transitions between two colors using RGB and HSV splitting in equal size steps:
I found the transitions using HSV are afected by Hue and depend of the distance between colors. If you choose two colors with the same hue is interesting to see that HSV transitions are more clear than in RGB, because you are only playing with saturation and value (black) and no adding colors like in RGB.