So I think I figured out a clever way to generate the random colors of the arcs, however I wanted to post it because I wanted to get others opinions. Also, I was looking to see if there was a better way of getting a random float between 0 and 1 inclusive.
If you do not want to know a valid solution (even if it isn’t the most elegant) please stop reading.
How I thought if this was taking the book’s advice and looking up the documentation of UIColor. I found an init method (among others)
- (UIColor *)initWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
The documentation that the variables red, green, blue, and alpha should be a float between 0.0 and 1.0 inclusive. Values below 0.0 are treated as 0.0 and values above 1.0 are treated as 1.0.
Anyhow, I did a google search and I found a way of getting a random float between 0.0 and 1.0
At the top of HypnosisView.m I defined a constant
#define ARC4RANDOM_MAX 0x100000000
Then I moved the setStroke message into the for loop which actually draws the circles and added some code. Here’s my changed for loop:
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)
{
// New code starts here
double red = ((double)arc4random() / ARC4RANDOM_MAX);
double green = ((double)arc4random() / ARC4RANDOM_MAX);
double blue = ((double)arc4random() / ARC4RANDOM_MAX);
double alpha = ((double)arc4random() / ARC4RANDOM_MAX);
[[[UIColor alloc] initWithRed:red green:green blue:blue alpha:alpha] setStroke];
// End of new code
CGContextAddArc(context, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
CGContextStrokePath(context);
}
It actually looks pretty cool (in my opinion). Please let me know if anybody else has an easier way of doing this (especially the random 0.0 - 1.0 part)