Reputation: 375
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?
Upvotes: 27
Views: 25791
Reputation: 34983
Removes duplication of the 3 arc4random
lines :)
static func randomColor() -> UIColor {
let random = { CGFloat(arc4random_uniform(255)) / 255.0 }
return UIColor(red: random(), green: random(), blue: random(), alpha: 1)
}
Upvotes: 1
Reputation: 8402
The following works on iOS 12
NSInteger aRedValue = arc4random()%255;
NSInteger aGreenValue = arc4random()%255;
NSInteger aBlueValue = arc4random()%255;
UIColor *randColor = [UIColor colorWithRed:aRedValue/255.0f green:aGreenValue/255.0f blue:aBlueValue/255.0f alpha:1.0f];
Upvotes: 6
Reputation: 2905
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)
}
}
Upvotes: 15
Reputation: 1044
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
Upvotes: 13
Reputation: 794
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];
Upvotes: 9
Reputation: 1342
Here is a snippet:
CGFloat redLevel = rand() / (float) RAND_MAX;
CGFloat greenLevel = rand() / (float) RAND_MAX;
CGFloat blueLevel = rand() / (float) RAND_MAX;
self.view.backgroundColor = [UIColor colorWithRed: redLevel
green: greenLevel
blue: blueLevel
alpha: 1.0];
Upvotes: 1
Reputation: 539685
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;
Upvotes: 27
Reputation: 2607
[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.
Upvotes: 48
Reputation: 21
// RGB
UIColor *randomRGBColor = [[UIColor alloc] initWithRed:arc4random()%256/256.0
green:arc4random()%256/256.0
blue:arc4random()%256/256.0
alpha:1.0];
// HSB
UIColor *randomHSBColor = [[UIColor alloc] initWithHue:arc4random()%256/256.0
saturation:(arc4random()%128/256.0)+0.5
brightness:(arc4random()%128/256.0)+0.5
alpha:1.0];
Upvotes: 2
Reputation: 830
arc4random() % 255 / 255.0
will always be truncated to 0 because arc4random()%255
will be an integer between 0 and 254 inclusive, and dividing by 255.0 and casting to an int will always result in 0. You should save the result as a float instead.
(Also you should use arc4random()%256
if you wish to select randomly from all possible colors.)
Upvotes: 1