views:

292

answers:

4

How do I generate a random color hexadecimal in Objective-C ?

I need a color hexdecimal , I don't need a random color. It is complicated ...

A: 

There is an article on cocoadev (including code examples) on writing a screensaver - with random colors:

The MYYN
A: 

You can use the standard C library routine rand() in your Objective-C application. From there, then, you'll want to call it three times to get random values for each of the red, green, and blue channels of your random color. You'll want to mod (%) the value by the maximum value the channel can have- typically 256. From there you can construct your NSColor appropriately. So your code might look something like:

int red = rand() % 255;
int green = rand() % 255;
int blue = rand() % 255;
NSColor* myColor = [NSColor colorWithCalibratedRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];

Because NSColor takes floats instead of integers a better approach would be to divide the random values by RAND_MAX right from the start:

float rand_max = RAND_MAX;
float red = rand() / rand_max;
float green = rand() / rand_max;
float blue = rand() / rand_max;
NSColor* myColor = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1.0];

This latter code will not limit the number of colors to a 24-bit spectrum.

fbrereto
Yeah, but then you'll only have the RGB values - it's not converted to hexadecimal values and concatenated (making it a hexadecimal colour code), which the OP obviously needs it to be.
Sune Rasmussen
Yep; I posted my answer before he clarified exactly what he was looking for.
fbrereto
Use random() not rand(). The low 12 bits of rand() go through a cyclic pattern. Also, `rand() % 255` gives you a number in the range 0 - 254.
JeremyP
+6  A: 

I think should work for you. Arc4random() is far better in the way of performance & ... accuracy than rand(). Rand() also needs to be seeded before use.

// 16777215 is FFFFFF
NSInteger *baseInt = arc4random() % 16777216;
NSString *hex = [NSString stringWithFormat:@"%06X", baseInt];

Edit: Edited based on comment regarding formatting.

Matt S
Thank you so much
Nick Brooks
Almost correct, but you need `%06X`, otherwise you'll get colors like `#AB`.
KennyTM
Format the string as you need to include the # or not.
Matt S
+1 I know that 16777215 == 0xFFFFFF, but it might be nicer to do `arc4random() % 0xFFFFFF`, just so that it's a bit more obvious where this number is coming from.
Dave DeLong
@Dave: `arc4random() % 0xFFFFFF` will give integers in the range 0 to `0xFFFFFE`.
KennyTM
Agree with Kenny, you should use `arc4random() % 0x1000000`
JeremyP
As regards using `arc4random()`, it is a better pseudo random number generator than `rand()` or `random()` but in a unit test situation, the fact that you have to set a seed is actually an advantage because it means you can generate the exact same sequence of numbers repeatedly.
JeremyP
If this answer is satisfactory please close this issue by accepting. Thank you.
Matt S
@KennyTM whoops, that's what I meant
Dave DeLong
A: 

Just for a short note:

Depending on your needs, it's possible that you should consider to write a not-so-random color generator. (For example when want to set up "rules" for the visibility of the generated colors.) So it's possible that you need to get the color code by limiting the random-generated values in HSB.

Scorchio