Guest User

Untitled

a guest
Feb 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. def colour_shade(colour,shade,scale=10)
  2. # default the scale to 100 if it is greater...
  3. scale = 100 if scale > 100
  4.  
  5. # if the shade is out of the range of scale then reset shade to scale...
  6. if shade > 0
  7. shade = scale if shade > scale
  8. else
  9. shade = scale * -1 if shade < scale * -1
  10. end
  11.  
  12. colour = "##{colour[1,1]*2}#{colour[2,1]*2}#{colour[3,1]*2}" if colour.length == 4
  13.  
  14. # Convert the colours to decimal...
  15. colour_split = [ (eval "0x#{colour[1,2]}"), (eval "0x#{colour[3,2]}"), (eval "0x#{colour[5,2]}") ]
  16.  
  17. # Calculate the shade...
  18. new_colour = "#"
  19. colour_split.each do |c|
  20. case
  21. when shade < 0: colour_shade = (c - ((c / scale) * shade.abs))
  22. when shade == 0: colour_shade = (c)
  23. when shade > 0: colour_shade = (c + (((255 - c) / scale) * shade))
  24. end
  25. new_colour << sprintf("0%x", colour_shade.to_i).slice(-2..-1).upcase
  26. end
  27. new_colour
  28. end
Add Comment
Please, Sign In to add comment