# Posted By

magicrebirth on 07/17/11

# Statistics

Viewed 150 times
Favorited by 0 user(s)

# Related snippets

/ Published in: Python  `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    G = startcolor    B = startcolor     targetR = goalcolor    targetG = goalcolor    targetB = goalcolor     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 colorsprintchart("#999933", "#6666FF", 16)` Subscribe to comments