Created
          May 11, 2011 12:13 
        
      - 
      
- 
        Save armanddp/966353 to your computer and use it in GitHub Desktop. 
    Function to transition between two colors
  
        
  
    
      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
    
  
  
    
  | 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 | 
  
    
      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
    
  
  
    
  | 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}'> </span>") | |
| end | |
| f.write('</div>') | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment