web-dev-qa-db-fra.com

Générer une UIColor aléatoire

J'essaie d'obtenir des couleurs aléatoires pour 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;
}

Et utilisez-le:

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

Mais la couleur est toujours noire. Qu'est-ce qui ne va pas? 

P.S. Désolé pour mon anglais )

16
Tatiana Mudryak

Parce que vous avez affecté les valeurs de couleur aux variables int. Utilisez plutôt float (Ou CGFloat). Aussi (comme @ stackunder est dit ), le reste doit être pris En modulo 256 pour couvrir toute la plage 0.0 ... 1.0:

CGFloat red = arc4random() % 256 / 255.0;
// Or (recommended):
CGFloat red = arc4random_uniform(256) / 255.0;
22
Martin R
[UIColor colorWithHue:drand48() saturation:1.0 brightness:1.0 alpha:1.0];

ou à Swift:

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

N'hésitez pas à randomiser ou à ajuster la saturation et la luminosité à votre goût.

33
kkodev

essaye ça 

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];
6
Jasmin

Voici une version de Swift, transformée en une extension UIColor:

extension UIColor {

    class func randomColor(randomAlpha randomApha: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 = randomApha ? CGFloat(arc4random_uniform(255)) / 255.0 : 1;

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

    }
}
5
Jbryson

vous pouvez utiliser cette façon,

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];


5
Shankar BS
// 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];
2
se7en

Voici un extrait:

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];
1
Jayachandra A

arc4random() % 255 / 255.0 sera toujours tronqué à 0 car arc4random()%255 sera un entier compris entre 0 et 254 inclus; diviser par 255.0 et transtyper en un entier aura toujours pour résultat 0. Vous devez plutôt enregistrer le résultat sous la forme d'un float.

(Vous devez également utiliser arc4random()%256 si vous souhaitez sélectionner au hasard parmi toutes les couleurs possibles.)

1
stackunderflow

Supprime la duplication des 3 lignes arc4random :)

static func randomColor() -> UIColor {
    let random = {CGFloat(arc4random_uniform(255)) / 255.0}
    return UIColor(red: random(), green: random(), blue: random(), alpha: 1)
}
0
pkamb