Skip to content

Instantly share code, notes, and snippets.

@r3econ
Last active August 29, 2015 13:57
Show Gist options
  • Save r3econ/9772706 to your computer and use it in GitHub Desktop.
Save r3econ/9772706 to your computer and use it in GitHub Desktop.
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