Generate a random UIColor

23,417

Solution 1

Because you have assigned the colour values to int variables. Use float (or CGFloat) instead. Also (as @stackunderflow's said), the remainder must be taken modulo 256 in order to cover the whole range 0.0 ... 1.0:

CGFloat red = arc4random() % 256 / 255.0;
// Or (recommended):
CGFloat red = arc4random_uniform(256) / 255.0;

Solution 2

[UIColor colorWithHue:drand48() saturation:1.0 brightness:1.0 alpha:1.0];

or in Swift:

UIColor(hue: CGFloat(drand48()), saturation: 1, brightness: 1, alpha: 1)

Feel free to randomise or adjust saturation and brightness to your liking.

Solution 3

Here is a swift version, made into a UIColor extension:

extension UIColor {
    class func randomColor(randomAlpha: Bool = false) -> UIColor {
        let redValue = CGFloat(arc4random_uniform(255)) / 255.0;
        let greenValue = CGFloat(arc4random_uniform(255)) / 255.0;
        let blueValue = CGFloat(arc4random_uniform(255)) / 255.0;
        let alphaValue = randomAlpha ? CGFloat(arc4random_uniform(255)) / 255.0 : 1;

        return UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: alphaValue)
    }
}

Solution 4

Swift solution using a class var random:

extension UIColor {
    class var random: UIColor {
        return UIColor(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1), alpha: 1.0)
    }
}

Use just like any other in-built UIColor class variable (.red, .blue, .white, etc.), for example:

view.backgroundColor = .random

Solution 5

try this

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];
Share:
23,417

Related videos on Youtube

Tatiana Mudryak
Author by

Tatiana Mudryak

Updated on March 06, 2020

Comments

  • Tatiana Mudryak
    Tatiana Mudryak about 4 years

    I try to get a random color for UILabel...

    - (UIColor *)randomColor
    {
        int red = arc4random() % 255 / 255.0;
        int green = arc4random() % 255 / 255.0;
        int blue = arc4random() % 255 / 255.0;
        UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
        NSLog(@"%@", color);
        return color;
    }
    

    And use it:

    [mat addAttributes:@{NSForegroundColorAttributeName : [self randomColor]} range:range];
    

    But color is always black. What is wrong?

  • ElegyD
    ElegyD almost 6 years
    Shouldn't it be arc4random_uniform(256) / 255.0? Because arc4random_uniform returns an integer less than upper bound. So in your case r, g or b will never be 1.0 (0xFF)
  • Martin R
    Martin R almost 6 years
    @ElegyD: You are completely right, that's why I had added the final remark "See also stackunderflow's answer about the range of possible values." – I have edited the answer now to avoid any confusion.
  • Tim Sylvester
    Tim Sylvester over 3 years
    Probably doesn't matter for most cases, but % 256 will yield 0...255 inclusive. % 255 will never yield 255 or 1.0.
  • thisIsTheFoxe
    thisIsTheFoxe over 2 years
    Exactly what I was looking for..! However, because it's actually making a new color every time it's called I think it'd be better as a func random() -> UIColor