Liam Stockhomme
Liam Stockhomme

Reputation: 523

How to generate a random UIColor?

I'm trying to generate a random colour from 8 options. All of the stack overflow posts / tutorials I've found have been ANY random colour. In my prefix.pch I defined 8 different sets of colour definitions this is a single example:

#define cola1       209/255.
#define colb1       0/255.
#define colc1       0/255.
#define cold1       1.0/255.

Defining different colour values for cola1-8, colb1-8, colc1-8, and cold1-8.

Then I set up a random number generator:

int randomNumber; 

randomNumber = arc4random() %8;
randomNumber = randomNumber + 1;
whatRandomNumberIs = randomNumber;

I then tried setting up an [NSString stringWithFormat:@"cola%i", randomNumber]; inside the [UIColor colorWithRed etc]

like this:

[UIColor colorWithRed:[NSString stringWithFormat:@"cola%i", whatRandomNumberIs] green:[NSString stringWithFormat:@"colb%i", whatRandomNumberIs] blue:[NSString stringWithFormat:@"colc%i", whatRandomNumberIs] alpha:[NSString stringWithFormat:@"cold%i", whatRandomNumberIs]];

But then realised you cannot put an NSString in a CGFloat.

So now I'm stuck. How would I go about installing a random number from 1-8 inside the red, green, blue and alpha values without doing an NSString stringWithFormat? Is there another way to return a random UIColor value that is defined because I only want it to be specific colours??

Upvotes: 2

Views: 2110

Answers (4)

rob mayoff
rob mayoff

Reputation: 385540

You are trying to construct a string at runtime and then use it as the name of a macro that was defined at compile-time. That doesn't work. No information about the name of a compile-time macro is available at runtime.

Here is one correct way to choose a random color from a set defined at compile time. Define a method to return a random color, in a category on UIColor:

@interface UIColor (Liam_RandomColor)

+ (UIColor *)Liam_randomColor;

@end

Implement the method to first (one time only) initialize an array of the predefined colors, and second (every time) to return an element of the array at random:

@implementation UIColor (Liam_RandomColor)

+ (UIColor *)Liam_randomColor {
    static dispatch_once_t onceToken;
    static NSArray *colors;
    dispatch_once(&onceToken, ^{
        colors = @[
            [UIColor colorWithRed:209/255.0 green:0 blue:0 alpha:1/255.0],
            [UIColor colorWithRed:50/255.0 green:100/255.0 blue:100/255.0 alpha:1],
            // etc.
        ];
    });

    return colors[arc4random_uniform(colors.count)];
}

@end

Upvotes: 1

Fahim Parkar
Fahim Parkar

Reputation: 31633

Below is what you can do...

In prefix.pch you have as below.

#define colorCombination1 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination2 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination3 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination4 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination5 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination6 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination7 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination8 [UIColor colorWithRed:.... alpha:1.0];

Now you create array of this colors..

NSArray *myColorArray = [[NSArray alloc] initWithObjects:colorCombination1, colorCombination2, colorCombination3, colorCombination4, colorCombination5, colorCombination6, colorCombination7, colorCombination8, nil];

Now you get random number say variable as generatedRandomNumber.

UIColor *myRandomColor = [myColorArray objectAtIndex:generatedRandomNumber%8];

generatedRandomNumber%8 will give you remainder from the generatedRandomNumber.

Hope this is what you want.

Upvotes: 2

Gustav Kores
Gustav Kores

Reputation: 186

You could create a category on UIColor and wrap your predefined colors in a method, something similar to this:

@interface UIColor (myCategory)
+ (UIColor *)randomColorForInt(int n);
@end

@implementation
+ (UIColor *)randomColorForInt(int n) {
    if (n == 0) {
        return [UIColor colorWithRed:cola1 green:colb1 blue:colc1 alpha:cold1]];
    }
    ...
}
@end

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Way you can get random color is by using hue , saturation and brightness

//random color
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];

Upvotes: 2

Related Questions