Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeOam/b87c23bfe5585f2e89f5 to your computer and use it in GitHub Desktop.
Save JeOam/b87c23bfe5585f2e89f5 to your computer and use it in GitHub Desktop.
为 UIColor 类增加 RandomColor 方法
//.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 方法了。
@JeOam
Copy link
Author

JeOam commented Jun 5, 2014

via: here

@JeOam
Copy link
Author

JeOam commented Jun 11, 2014

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