相关疑难解决方法(0)

在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
}
?>
Run Code Online (Sandbox Code Playgroud)

有没有更好的算法来做到这一点?


我带来一个例子:在上面的代码我用$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则是相同的.它是某种颜色的东西,它的过渡更难或更柔和.

php algorithm rgb colors

7
推荐指数
1
解决办法
3164
查看次数

Java:平滑的颜色过渡

我试图做一个health bar,并且可能是原始的,它将开始绿色,并且在失去健康后,你会发现它会变成黄色,然后是橙色,然后是红色......或者相对于它的东西.

我尝试使用此链接中提供的方法:https://stackoverflow.com/questions/19841477/java-smooth-color-transition

从该链接的结果是这样的代码,仅仅从价值100测试为0,但在结束IllegalArgumentException是正常的RedGreen,和我的原因,猜测是被高估255的值.

Color to = Color.red;
Color base = Color.green;
int red = (int)Math.abs((100 * to.getRed()) + ((1 - 100) * base.getRed()));
int green = (int)Math.abs((100 * to.getGreen()) + ((1 - 100) * base.getGreen()));
int blue = (int)Math.abs((100 * to.getBlue()) + ((1 - 100) * base.getBlue()));
setForeground(new Color(red, green, blue));
Run Code Online (Sandbox Code Playgroud)

它没有真正起作用,我完全不知道如何才能达到transition我希望的方式.

所以在我的HealthBar课堂上,我有一个update()方法

public void update() {
    if (getValue() < 10) setForeground(Color.red); …
Run Code Online (Sandbox Code Playgroud)

java animation colors transitions

2
推荐指数
2
解决办法
3970
查看次数

标签 统计

colors ×2

algorithm ×1

animation ×1

java ×1

php ×1

rgb ×1

transitions ×1