Skip to content

Instantly share code, notes, and snippets.

@teeler
Created November 2, 2012 22:23
Show Gist options
  • Save teeler/4004711 to your computer and use it in GitHub Desktop.
Save teeler/4004711 to your computer and use it in GitHub Desktop.
color iter
# thanks @craigcitro
import collections
import itertools
RGB = collections.namedtuple('RGB', ['r', 'g', 'b'])
def rgb_to_str(rgb):
return "%02x%02x%02x" % (rgb.r, rgb.g, rgb.b)
def str_to_rgb(s):
r = int(s[:2], base=16)
g = int(s[2:4], base=16)
b = int(s[4:6], base=16)
return RGB(r=r, g=g, b=b)
def coloriter(start_str, end_str, num):
num -= 1
start = str_to_rgb(start_str)
end = str_to_rgb(end_str)
iter_r = (int(start.r + i*((end.r - start.r) / (1.0 * num)))
for i in xrange(num + 1))
iter_g = (int(start.g + i*((end.g - start.g) / (1.0 * num)))
for i in xrange(num + 1))
iter_b = (int(start.b + i*((end.b - start.b) / (1.0 * num)))
for i in xrange(num + 1))
return (RGB(r=r, g=g, b=b)
for r,g,b in itertools.izip(iter_r, iter_g, iter_b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment