Here's a quick function that we use in some of our Ruby on Rails applications to print out a colored number that indicates a comparison with another number. Basically, you pass in a value(the number you're comparing) and a maximum(the number you're comparing
value against), and it will return a color that indicates its amount in comparison to the maximum. We use this to compare quantities in inventory systems, file comparison percentages, and other fun stuff.
Here's the code, all you have to do is place it in app/helpers/application_helper.rb
def colored_comparison(value = 0, maximum = 0, options = {:direction => "down", :display => "plain"}) # compare value against another value(maximum), print a pretty colored number comparison(red = bad, green = good) signifying comparison
colors = {:good => "green", :middle => "orange", :bad => "red", :none => "black"} # color hash
color_key = "none".to_sym # set default as "none"...see color key below
if options[:direction] == "down" # compare from maximum down to 0...70/100 -> good, 10/100 -> bad
if value.to_i >= (maximum.to_i * 0.8) # above half max
color_key = "good" # set as good
elsif (value.to_i <= (maximum.to_i * 0.8)) && value.to_i >= (maximum.to_i * 0.5) # between 50% and 20%
color_key = "middle" # set as middle
elsif (value.to_i <= (maximum.to_i * 0.5)) && value.to_i >= 0 # less than 20%
color_key = "bad" # set as good
else
color_key = "none" # set as none
end
elsif options[:direction] == "up" # compare from 0 up to maximum...70/100 -> bad, 10/100 -> good
if value.to_i <= (maximum.to_i * 0.2) # below half
color_key = "good" # set as good
elsif (value.to_i >= (maximum.to_i * 0.2)) && value.to_i <= (maximum.to_i * 0.5) # between 50% and 70%
color_key = "middle" # set as middle
elsif (value.to_i >= (maximum.to_i * 0.5)) && (value.to_i <= maximum.to_i) # between 70% and 100%
color_key = "bad" # set as good
else
color_key = "none" # set as none
end
end
if options[:display] == "plain" # return plain colored value, ie: 5
message = "#{value}"
elsif options[:display] == "fraction" # return colored fraction, ie: 5/100
message = "#{value}/#{maximum}"
else
message = "There was a problem! Please check the options you passed in."
end
return message
end
Then you can call it anywhere in your views like this:
<%= colored_comparison(10, 100) %> Items Remaining
This should return:
10 Items Remaining
By default, the function assumes that if the value you're comparing is really low(counting in a downward direction), this is a bad thing. Sometimes this isn't a bad thing(If you're counting down to something or trying to get rid of t-shirts, etc.), so you can reverse the direction, by passing in the
:direction option.
<%= colored_comparison(10, 100, options = {:direction => "up", :display => "plain"}) %> Items Remaining
This should return:
10 Items Remaining
You can also set the :display option to return a fraction-looking amount that prints out the number you're comparing against. Here's an example:
<%= colored_comparison(10, 100, options = {:direction => "up", :display => "fraction"}) %> Items Remaining
This should return:
10/100 Items Remaining
You can change the amounts that are being compared easily(see the function above). It should be pretty straightforward, I just thought I'd share. Post a comment if you have any questions!