I have got a custom UIView which is a circle and I want to change the color of the circle based on the value of a property. The value is in the range of 0 to 100 where 100 should be red and 0 should be yellow. Is there a way to calculate the current color based on the property?
Franz
In RGB red is:
{ 1.0, 0.0, 0.0 }
and yellow is:
{ 1.0, 1.0, 0.0 }
so we can see that it's the green channel that needs to be calculated from the input of 0 to 100, however that range needs to be inverted as red is 100 and yellow is 0.
So (untested):
- (void)setCircleColor:(int)value
{
if (value < 0)
value = 0;
else if (value > 100)
value = 100;
CGFloat green 1.0 - ((CGFloat)value / 100.0);
UIColor *color = [UIColor colorWithRed:1.0
green:green
blue:0.0
alpha:1.0];
self.circle.color = color;
}
Note this assumes the property is called circleColor
and that your circle object is accessible from the circle
property and itself has a color
property.