Skip to content

Instantly share code, notes, and snippets.

@hechen
Last active August 29, 2015 14:18
Show Gist options
  • Save hechen/20b702b962ef29604ee6 to your computer and use it in GitHub Desktop.
Save hechen/20b702b962ef29604ee6 to your computer and use it in GitHub Desktop.
Generate random Color using category based UIColor
//
// UIColor+RandomColor.h
// ViewImage
//
// Created by hechen on 15/4/8.
// Copyright (c) 2015年 hechen. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIColor(RandomColor)
+(UIColor *) randomColor;
@end
//
// UIColor+RandomColor.m
// ViewImage
//
// Created by hechen on 15/4/8.
// Copyright (c) 2015年 hechen. All rights reserved.
//
#import "UIColor+RandomColor.h"
@implementation UIColor(RandomColor)
+ (UIColor *)randomColor{
CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
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