生成颜色渐变

时间:2009-08-16 03:23:46

标签: php colors gradient color-scheme linear-gradients

我有一个想法以编程方式生成匹配的颜色方案,但是我需要能够在给定一组两种颜色(Hex或RGB值)的情况下生成线性渐变。

任何人都可以向我提供(伪)代码或指出我正确的方向来完成这项任务吗?

编辑:我忘了提及,但我还需要指定(或知道)渐变从颜色A到颜色B的步数。

1 个答案:

答案 0 :(得分:2)

好的,所以你知道步骤,开始颜色和结束颜色。假设您有每种颜色的RGB值:

   red_diff = end_red - start_red
   green_diff = end_green - start_green
   blue_diff = end_blue - start_blue

   #Note: This is all integer division
   red_step = red_diff / num_steps 
   green_step = green_diff / num_steps
   blue_step = blue_diff / num_steps

   current_red = start_red
   current_geen = start_green
   current_blue = start_blue

   while current_red != end_red and current_green != end_green and current_blue != end_blue:
       current_red += red_step
       current_green += green_step
       current_blue += blue_step
       # print color