Last active
August 29, 2015 14:02
-
-
Save JeOam/b87c23bfe5585f2e89f5 to your computer and use it in GitHub Desktop.
为 UIColor 类增加 RandomColor 方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//.h | |
#import <UIKit/UIKit.h> | |
@interface UIColor (RandomColor) | |
+(UIColor *) randomColor; | |
@end | |
//.m | |
#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 | |
//引用这个 category,然后可以直接调用这个 randomColor 方法了。 |
RGB 取色:
[UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1.0];
// or
#define COLOR_WITH_RGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
via: here