Skip to content

Instantly share code, notes, and snippets.

@armanddp
Created May 11, 2011 12:13
Show Gist options
  • Save armanddp/966353 to your computer and use it in GitHub Desktop.
Save armanddp/966353 to your computer and use it in GitHub Desktop.
Function to transition between two colors
def gradiate(color1, color2, steps)
[color1].tap do |colors|
red, green, blue = (color1 >> 16), ((color1 >> 8) & 0xFF), (color1 & 0xFF)
red_diff, green_diff, blue_diff = ((color2 >> 16) - red), (((color2 >> 8) & 0xFF) - green), ((color2 & 0xFF) - blue)
(steps += 1).times do |i|
ratio = i.to_f / steps
colors << (((red_diff * ratio) + red).to_i << 16 | ((green_diff * ratio) + green).to_i << 8 | ((blue_diff * ratio) + blue).to_i)
end
colors << color2
end
end
def gradiate(color1, color2, steps)
[color1].tap do |colors|
red, green, blue = (color1 >> 16), ((color1 >> 8) & 0xFF), (color1 & 0xFF)
red_diff, green_diff, blue_diff = ((color2 >> 16) - red), (((color2 >> 8) & 0xFF) - green), ((color2 & 0xFF) - blue)
(steps += 1).times do |i|
ratio = i.to_f / steps
colors << (((red_diff * ratio) + red).to_i << 16 | ((green_diff * ratio) + green).to_i << 8 | ((blue_diff * ratio) + blue).to_i)
end
colors << color2
end
end
File.open('test.html', 'w') do |f|
f.write('<div>')
gradiate(0xFFFFFF, 0xFF0000, 10).each do |c|
f.write("<span style='background-color:##{c.to_s(16).upcase}'>&nbsp;</span>")
end
f.write('</div>')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment