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.

Answers


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)

Need Your Help

How to set Minimum Order Quantiy (MOQ) for a category in magento?

magento

How to add Minimum Qty Allowed in Shopping Cart for a category.

Getting values of RSS Feed row item with a TextView in Android

android android-intent rss

I'm trying to build an Android app that displays a couple of RSS feeds, and I want to display the RSS feed with just titles and dates, and then have the user click on them to view the full story. A...