Created
October 6, 2011 02:10
-
-
Save brookemckim/1266318 to your computer and use it in GitHub Desktop.
Calculate a Gradient
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
gradient_percentage = 0.10 | |
# Split into RGB | |
red = backgroundcolor[0,2].hex | |
green = backgroundcolor[2,2].hex | |
blue = backgroundcolor[4,2].hex | |
# Add percentage (could subtract if you want to darker) | |
red += (red * gradient_percentage).to_i | |
green += (green * gradient_percentage).to_i | |
blue += (blue * gradient_percentage).to_i | |
# Convert to base 16 string and pad with extra 0 if needed | |
red = red.to_s(16) | |
green = green.to_s(16) | |
blue = blue.to_s(16) | |
# Pad with extra 0 if necessary | |
red = ('0' + red) if red.size == 1 | |
green = ('0' + green) if green.size == 1 | |
blue = ('0' + blue) if blue.size == 1 | |
# Make sure numbers haven't overflown | |
red = "00" if red.hex < 0 | |
red = "ff" if red.hex > 255 | |
green = "00" if green.hex < 0 | |
green = "ff" if green.hex > 255 | |
blue = "00" if blue.hex < 0 | |
blue = "ff" if blue.hex > 255 | |
gradient = red + green + blue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment