views:

290

answers:

2

I had an idea to programmatically generate matching color schemes however I need to be able to generate a linear gradient given a set of two colors (Hex or RGB values).

Can anyone provide me the (pseudo-)code or point me in the right direction to accomplish this task?

EDIT: I forgot to mention, but I also need to specify (or know) the number of steps the gradient takes from color A to color B.

+1  A: 

Okay, so you know the steps, start color and end color. Assuming you have RGB values for each color:

   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
quanticle