Skip to content

Instantly share code, notes, and snippets.

@JoeWoodward
Created October 26, 2011 21:40
Show Gist options
  • Save JoeWoodward/1317977 to your computer and use it in GitHub Desktop.
Save JoeWoodward/1317977 to your computer and use it in GitHub Desktop.
module ArticlesHelper
def tag_percentage(count)
total_tags = 0.0
Article.all_tag_counts.each do |tag|
total_tags += tag.count
end
return tag_percent = (count / total_tags * 100).ceil
end
def hex_calculator(percentage, maxr, maxg, maxb, r, g, b)
red = (r * percentage) + maxr
green = (g * percentage) + maxg
blue = (b * percentage) + maxb
hex = "#%02x%02x%02x" % [red, green, blue]
return hex
end
def tag_popularity_as_hex(count)
#most saturated colour possible, used for a highly used tag
max_colour = [255, 0, 0]
# least saturated colour possible, used for tags that are rarely used
min_colour = [0, 0, 0]
# percentage of times the tag has been used
tag_percent = tag_percentage(count)
# i = 0 to 3 for spaces in array, correlates to R, G & B.
for i in 0..2
# we find the distance between each value, i.e. between 100% and 1%
range = min_colour[i] - max_colour[i]
# then find 1% of the range
end
one_red = range[0] / 100
one_green = range[1] / 100
one_blue = range[2] / 100
# we then multiply the one percent of the range by the tag percentage and add the original value back in
hex = hex_calculator(tag_percent, max_colour[0], max_colour[1], max_colour[2], one_red, one_green, one_blue)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment