Created
May 6, 2013 20:18
-
-
Save jjulian/5527841 to your computer and use it in GitHub Desktop.
Generate colors that are between a start and end color. It's a gradient. Useful to pass to a Graphite graph as "colorList".
This file contains hidden or 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
# Create a gradient color list. | |
# Shamelessly ported from php code at http://www.herethere.net/~samson/php/color_gradient | |
begin_color = 0xfffde3 | |
end_color = 0xffb715 | |
samples = 30 | |
def interpolate(b, e, step, max) | |
if b < e | |
(((e - b) * (step / max.to_f)) + b).to_i | |
else | |
(((b - e) * (1 - step / max.to_f)) + e).to_i | |
end | |
end | |
def generate(b, e, steps) | |
r0 = (b & 0xff0000) >> 16 | |
g0 = (b & 0x00ff00) >> 8 | |
b0 = (b & 0x0000ff) >> 0 | |
r1 = (e & 0xff0000) >> 16 | |
g1 = (e & 0x00ff00) >> 8 | |
b1 = (e & 0x0000ff) >> 0 | |
(0..steps).map do |step| | |
r = interpolate(r0, r1, step, steps) | |
g = interpolate(g0, g1, step, steps) | |
b = interpolate(b0, b1, step, steps) | |
(r << 8 | g) << 8 | b | |
end | |
end | |
generate(begin_color, end_color, samples).each do |c| | |
hex = "%06X" % c | |
print "#{hex}," | |
end | |
puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment