Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def colour_shade(colour,shade,scale=10)
- # default the scale to 100 if it is greater...
- scale = 100 if scale > 100
- # if the shade is out of the range of scale then reset shade to scale...
- if shade > 0
- shade = scale if shade > scale
- else
- shade = scale * -1 if shade < scale * -1
- end
- colour = "##{colour[1,1]*2}#{colour[2,1]*2}#{colour[3,1]*2}" if colour.length == 4
- # Convert the colours to decimal...
- colour_split = [ (eval "0x#{colour[1,2]}"), (eval "0x#{colour[3,2]}"), (eval "0x#{colour[5,2]}") ]
- # Calculate the shade...
- new_colour = "#"
- colour_split.each do |c|
- case
- when shade < 0: colour_shade = (c - ((c / scale) * shade.abs))
- when shade == 0: colour_shade = (c)
- when shade > 0: colour_shade = (c + (((255 - c) / scale) * shade))
- end
- new_colour << sprintf("0%x", colour_shade.to_i).slice(-2..-1).upcase
- end
- new_colour
- end
Add Comment
Please, Sign In to add comment