Generating Color Gradients

让人想犯罪 __ 提交于 2019-12-22 12:37:35

问题


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:


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


来源:https://stackoverflow.com/questions/1283391/generating-color-gradients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!