Skip to content

Instantly share code, notes, and snippets.

@JoeWoodward
Created October 26, 2011 22:11
Show Gist options
  • Save JoeWoodward/1318045 to your computer and use it in GitHub Desktop.
Save JoeWoodward/1318045 to your computer and use it in GitHub Desktop.
module ArticlesHelper
def tag_popularity_as_hex(tag_count)
# must use floats
# most saturated colour possible, used for a highly used tag
max_r = 255.0
max_g = 0.0
max_b = 0.0
# least saturated colour possible, used for tags that are rarely used
min_r = 255.0
min_g = 255.0
min_b = 255.0
# percentage of times this tag has been used
total_tag_count = 0.0
Article.all_tag_counts.each do |tag|
total_tag_count += tag.count
end
tag_dominance = (tag_count / total_tag_count * 100)
# we find the distance between each value, i.e. between 100% and 1%
range_r = min_r - max_r
range_g = min_g - max_g
range_b = min_b - max_b
# then find 1% of each range
one_r = range_r / 100
one_g = range_g / 100
one_b = range_b / 100
# now we can calculate the rgb values
red = (one_r * tag_dominance) + max_r
green = (one_g * tag_dominance) + max_g
blue = (one_b * tag_dominance) + max_b
# we then multiply the one percent of the range by the tag percentage and add the original value back in
hex = "#%02x%02x%02x" % [red, green, blue]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment