2009-08-16 200 views
1

我有一個想法,以編程方式生成匹配的配色方案,但是我需要能夠生成一個給定一組兩種顏色(十六進制或RGB值)的線性漸變。生成顏色漸變

任何人都可以提供(僞)代碼或指向正確的方向來完成此任務嗎?

編輯:我忘了提,但我還需要指定(或不知道)的步驟梯度色彩A拍攝到顏色B.

+0

下面是一篇文章 - [http://www.herethere.net/~samson/php/color_gradient/?cbegin=FF0000&cend=FFFFFF&steps=16](http: (http://codingforums.com/showthread.php?t=79463](http://codingforums.com/) showthread.phpΔT= 79463) – adatapost 2009-08-16 03:26:40

回答

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