You can do this in roughly two steps.
First get the histogram of the image using RMagick:
http://www.simplesystems.org/RMagick/doc/image1.html#color_histogram
The next step is applying some formula. The one I provide below is one I just came up with off the top of my head. However, there are a few others:
http://en.wikipedia.org/wiki/Contrast_(vision)#Formula
Next compute the average color by multiplying each pixel by it's weight and then dividing by the total number of pixels. Something like this:
color_histogram.each do |pixel, count|
total_red += pixel.red*count
total_blue += pixel.blue*count
total_green += pixel.green*count
end
average_red = total_red / pixel_count average_blue = total_blue / pixel_count average_green = total_green / pixel_count
Once you have your "average color", determine the total distance between each pixel and the average:
color_histogram.each do |pixel, count|
distance_red += Math.abs(average_red - pixel.red)
distance_blue += Math.abs(average_blue - pixel.blue)
distance_green += Math.abs(average_green - pixel.green)
end
The sum distance_red + distance_blue + distance_green
should roughly be equivalent to the "contrast" of the image.