Skip to content

Instantly share code, notes, and snippets.

@jjulian
Created May 6, 2013 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjulian/5527841 to your computer and use it in GitHub Desktop.
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".
# 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