Created
July 23, 2013 20:53
-
-
Save zokis/6066073 to your computer and use it in GitHub Desktop.
gradient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_hex(c): | |
r = hex(c[0])[2:].zfill(2)[:2] | |
g = hex(c[1])[2:].zfill(2)[:2] | |
b = hex(c[2])[2:].zfill(2)[:2] | |
return [r, g, b] | |
def get_gradient_color(c1, c2, percent, in_hex=False): | |
r = c1[0] + (percent * (c2[0] - c1[0])) | |
g = c1[1] + (percent * (c2[1] - c1[1])) | |
b = c1[2] + (percent * (c2[2] - c1[2])) | |
# if r > 255: r = 255 | |
# if g > 255: g = 255 | |
# if b > 255: b = 255 | |
# if r < 0: r = 0 | |
# if g < 0: g = 0 | |
# if b < 0: b = 0 | |
if not in_hex: | |
return [int(r), int(g), int(b)] | |
return to_hex([int(r), int(g), int(b)]) | |
def get_gradient_colors(c1, c2, qtd=10, in_hex=False): | |
qtd = float(qtd) | |
colors = [] | |
step = (100. / qtd) if 100 > qtd else qtd / 100. | |
steps = [] | |
x = step | |
for i in range(int(qtd)): | |
steps.append(int(x)) | |
x += step | |
for i in steps: | |
colors.append(get_gradient_color(c1, c2, i/100., in_hex)) | |
return colors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment