Created
November 14, 2012 05:42
-
-
Save jnf/4070508 to your computer and use it in GitHub Desktop.
Color layering for Stories
This file contains 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
class Story < ActiveRecord::Base | |
... | |
before_save :setColor | |
... | |
private | |
def setColor | |
sc = {} | |
total = 0 | |
if new_record? #for new records, add the associated thing to the totals | |
Scoretype.all.each { |e| sc[e.id] = user.scoreFor(e) } | |
total += user.totalScore.to_f | |
total += thing.scores.sum &:value | |
thing.scores.each { |score| sc[score.scoretype.id] += score.value } | |
else #get the totals for the selected story and older stories only | |
Scoretype.all.each { |e| sc[e.id] = 0.0; } | |
stories.each{ |story| | |
thing = story.thing | |
total += thing.scores.sum &:value | |
thing.scores.each { |score| sc[score.scoretype.id] += score.value } | |
} | |
end | |
scorearrays = sc.sort_by { |k,v| v } #most points goes first | |
r = g = b = 255 #layering against flat white | |
scorearrays.each { |scorearray| | |
q,x,y,z,a = Scoretype.find(scorearray[0]).color.split(/\D+/) #q is useless; I'm lazy. | |
x,y,z = [x,y,z].collect { |c| c.to_f } #MOAR FLOATS | |
#opacity in each channel is the ratio of the category's points to the sum of points | |
o = scorearray[1] / total | |
#MATH | |
r = ((x-r) * o + r).round | |
g = ((y-g) * o + g).round | |
b = ((z-b) * o + b).round | |
} | |
self.color = "rgb(#{r},#{g},#{b})" #Neat, yeah? | |
end | |
... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment