Advertisement
dsuveges

Untitled

Mar 31st, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. # helper function to generate color gradient:
  2. def hex_to_RGB(hex):
  3.     ''' "#FFFFFF" -> [255,255,255] '''
  4.     # Pass 16 to the integer function for change of base
  5.     return [int(hex[i:i+2], 16) for i in range(1,6,2)]
  6.  
  7. # helper function to generate color gradient:
  8. def linear_gradient(start_hex, finish_hex="#FFFFFF", n=307):
  9.     ''' returns a gradient list of (n) colors between
  10.        two hex colors. start_hex and finish_hex
  11.        should be the full six-digit color string,
  12.        inlcuding the number sign ("#FFFFFF") '''
  13.  
  14.     # Starting and ending colors in RGB form
  15.     s = hex_to_RGB(start_hex)
  16.     f = hex_to_RGB(finish_hex)
  17.  
  18.     # Initilize a list of the output colors with the starting color
  19.     RGB_list = ["rgb(%s, %s, %s)" % (s[0], s[1], s[2])]
  20.  
  21.     # Calcuate a color at each evenly spaced value of t from 1 to n
  22.     for t in range(1, n):
  23.         # Interpolate RGB vector for color at the current value of t
  24.         curr_vector = [int(s[j] + (float(t)/(n-1))*(f[j]-s[j])) for j in range(3)]
  25.         # Add it to our list of output colors
  26.         RGB_list.append("rgb(%s, %s, %s)" % (curr_vector[0], curr_vector[1], curr_vector[2]))
  27.  
  28.     RGB_list.reverse()
  29.     return RGB_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement