Going from black to green with limited input factor
How can I make a method that takes in a float from 0-50, and gives out a green color with uniform intensity?
So if the input value is 0, the color is black:
[UIColor colorWithRed:0 green:0 blue:0];
and if the input value is 50, the color is the max green color:
[UIColor colorWithRed:0 green:255.0/255.0 blue:0];
And so if the input factor is somewhere in the middle, then the color should be somewhere in the middle. So going from 0-50, it should go from the darkest green to the lightest green. Should be simple but I'm not sure what I'm missing:
//factor is a float from 0-50 UIColor *greenColor = [UIColor colorWithRed:0 green:factor/255.0 blue:0 alpha:1.0];
This code generates a green color, but doesn't fluctuate much, so you only see about one shade of green.
You need to scale the RGB max value (255) against your own max value (50). And all you need is fairly simple math:
colorVal = 255 * x / 50
where x is the intesity on the scale from 0 to 50. So:
255 * 0 / 50 = 0 255 * 50 / 50 = 255 255 * 25 / 50 = 128 (rounded up from 127.5)
Your range is only from 0.0/255.0 -> 50.0/255.0, you want it to be 0.0 to 1.0; for that you could try factor/50.0.
For one thing, you're making the math too hard. Looks like the [UIColor ... green: ...] value is supposed to range from 0.0 to 1.0, so the value calculation could simply be:
CGFLoat value = factor / 50.0; UIColor *greenColor = [UIColor colorWithRed:0 green:(value) blue:0 alpha:1]
That will give a value of 0.0 when factor = 0 and 1.0 when factor = 50.
Beyond that, though, I'm not sure - maybe whatever actually does the drawing does some quantizing or works with a limited palette?