Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
Created February 7, 2013 21:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lambdamusic/4734406 to your computer and use it in GitHub Desktop.
Save lambdamusic/4734406 to your computer and use it in GitHub Desktop.
Python: RGB Color Gradation Function
import string
def make_color_tuple( color ):
"""
turn something like "#000000" into 0,0,0
or "#FFFFFF into "255,255,255"
"""
R = color[1:3]
G = color[3:5]
B = color[5:7]
R = int(R, 16)
G = int(G, 16)
B = int(B, 16)
return R,G,B
def interpolate_tuple( startcolor, goalcolor, steps ):
"""
Take two RGB color sets and mix them over a specified number of steps. Return the list
"""
# white
R = startcolor[0]
G = startcolor[1]
B = startcolor[2]
targetR = goalcolor[0]
targetG = goalcolor[1]
targetB = goalcolor[2]
DiffR = targetR - R
DiffG = targetG - G
DiffB = targetB - B
buffer = []
for i in range(0, steps +1):
iR = R + (DiffR * i / steps)
iG = G + (DiffG * i / steps)
iB = B + (DiffB * i / steps)
hR = string.replace(hex(iR), "0x", "")
hG = string.replace(hex(iG), "0x", "")
hB = string.replace(hex(iB), "0x", "")
if len(hR) == 1:
hR = "0" + hR
if len(hB) == 1:
hB = "0" + hB
if len(hG) == 1:
hG = "0" + hG
color = string.upper("#"+hR+hG+hB)
buffer.append(color)
return buffer
def interpolate( startcolor, goalcolor, steps ):
"""
wrapper for interpolate_tuple that accepts colors as html ("#CCCCC" and such)
"""
start_tuple = make_color_tuple(startcolor)
goal_tuple = make_color_tuple(goalcolor)
return interpolate_tuple(start_tuple, goal_tuple, steps)
def printchart(startcolor, endcolor, steps):
colors = interpolate(startcolor, endcolor, steps)
for color in colors:
print color
# Example... show us 16 values of gradation between these two colors
printchart("#999933", "#6666FF", 16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment