Friday, 28 November 2014

UIColor generate random color. Using methods colorWithHue: saturation: brightness: alpha: and colorWithRed: green: blue: alpha:


+(UIColor*)getRandomColor{
    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
    UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    return color;
}
+(UIColor*)getRandomColorUsingRGB{
  
    CGFloat redLevel    = rand() / (float) RAND_MAX;
    CGFloat greenLevel  = rand() / (float) RAND_MAX;
    CGFloat blueLevel   = rand() / (float) RAND_MAX;
    
    UIColor *color = [UIColor colorWithRed: redLevel
                                     green: greenLevel
                                      blue: blueLevel
                                     alpha: 1.0];
    return color;
}

No comments:

Post a Comment