Created
February 3, 2011 07:06
-
-
Save newacct/809151 to your computer and use it in GitHub Desktop.
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 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 = [ colour[1,2].hex, colour[3,2].hex, colour[5,2].hex ] | |
# 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("%02X", colour_shade.to_i) | |
end | |
new_colour | |
end | |
File.open('shades.html',"w") do |htmlfile| | |
html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" + | |
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" + | |
"\n" + | |
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" + | |
" <head>\n" + | |
" <title>Shades</title>\n" + | |
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n" + | |
" </head>\n" + | |
" <body>\n" + | |
" <h1>Shades</h1>\n" + | |
" <p>\n" | |
scale = 10 | |
for i in 1..scale | |
colour = "#437F32" | |
html << "<a href=\"#\"><span style='background-color: " + colour_shade(colour,i) + "; width: 500px; display: block;'>colour_shade('" + colour + "'," + i.to_s + ") - " + colour_shade(colour,i) + "</span></a><br />\n" | |
end | |
html << " </p>\n" + | |
" </body>\n" + | |
"</html>" | |
htmlfile.puts html | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment