Create a gist now

Instantly share code, notes, and snippets.

UIColor category for creating a random color.
@interface UIColor (Random)
+ (UIColor *)randomColor;
@end
#import "UIColor+Random.h"
@implementation UIColor (Random)
+ (UIColor *)randomColor
{
// 0.0 to 1.0
CGFloat hue = ( arc4random() % 256 / 256.0 );
// 0.5 to 1.0, away from white
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
// 0.5 to 1.0, away from black.
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
return [UIColor colorWithHue:hue
saturation:saturation
brightness:brightness
alpha:1];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment