[NSColorWell setColor:NSColor]

时间:2013-09-28 17:28:00

标签: objective-c macos nscolor nscolorwell

在我的应用程序中,我在界面中有一些NSColorWells,但是当我调用setColor:NSColor时,颜色不会改变颜色。这是方法:

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

问题可能是什么?

1 个答案:

答案 0 :(得分:3)

TBlue所说的:NSColor的组成部分从0.0到1.0,而不是0到255.(另外,arc4random_uniform()的上限是 less <的结果/ em>而不是结果的最大值是什么。)

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