Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.