Algorithm to generate RGB graduated colors in PHP

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

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.

回答1:

In HSV, generate n colors whose hue changes linearly from the first color to the second color. Convert each HSV value to RGB.



回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!