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.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • Here is an article - [http://www.herethere.net/~samson/php/color_gradient/?cbegin=FF0000&cend=FFFFFF&steps=16](http://www.herethere.net/~samson/php/color_gradient/?cbegin=FF0000&cend=FFFFFF&steps=16) [http://codingforums.com/showthread.php?t=79463](http://codingforums.com/showthread.php?t=79463) – KV Prajapati Aug 16 '09 at 03:26

1 Answers1

2

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
  • 4,872
  • 6
  • 32
  • 42