首页 > 代码库 > iOS 生成随机颜色(UIColor)

iOS 生成随机颜色(UIColor)


#import <UIKit/UIKit.h>


@interface UIColor (RandomColor)

+(UIColor *) randomColor;

@end



#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 [UIColorcolorWithHue:hue saturation:saturationbrightness:brightness alpha:1];

}

@end



将随机生成的颜色绘制成图片。