Print

Print


Off the cuff, I think you're looking for

f(x) = (x-m) * 255 / (M-m)

where M is the maximum in the input data set, m in the minimum, and x is the number in hand.

-Tod


On Dec 20, 2011, at 10:57 AM, Nate Hill wrote:

> Here's a brain teaser for the mathematically inclined:
> 
> I've got a set of values that I want to scale to the 0-255 range so that I
> can adjust colors in my CSS.
> Say I have the following data: (6, 457, 97, 200, 122).
> I'd like to scale those numbers so that the highest one, 475 = 255.
> and the lowest one, 6 = 0.
> All of the other numbers, 97, 200, and 122 should be scaled proportionally
> to fit within the range.
> 
> This way, when I loop through and hit my CSS {background-color:rgb(255,
> **data**, 255);} each piece of data will generate a different color and
> I'll have the maximum spread in proportionally correct colors from 0-255.
> 
> There's probably some math operation to do this, but I know I paid far too
> little attention in math class as a kid.  "When will I ever need to use
> this stuff in *real life*", I asked the teacher with a sneer.
> 
> If anyone could point me in the right direction I'd appreciate it.
> 
> -- 
> Nate Hill
> [log in to unmask]
> http://www.natehill.net