Question

In my app I have some NSColorWells in the interface, but when I call setColor:NSColor the color well does not change color. This is the method:

- (IBAction)randomColor:(id)sender {
    int size = 255;
    int R = arc4random_uniform(size);
    int G = arc4random_uniform(size);
    int B = arc4random_uniform(size);
    NSColor *newcolor = [NSColor colorWithCalibratedRed:R green:G blue:B alpha:1.0];
    [self.colorwell setColor:newcolor];
}

What might the problem be?

Was it helpful?

Solution

What TBlue said: The components of an NSColor go from 0.0 to 1.0, not 0 to 255. (Also, the upper bound of arc4random_uniform() is what the result will be less than, not what the max value of the result can be.)

- (IBAction)randomColor:(id)sender {
    int size = 256;
    int R = arc4random_uniform(size);
    int G = arc4random_uniform(size);
    int B = arc4random_uniform(size);
    NSColor *newcolor = [NSColor colorWithCalibratedRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0];
    [self.colorwell setColor:newcolor];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top