1

如何使用 Objective-C 在 OS X 中创建颜色选择器?

你有一些有效的代码吗?

谢谢

4

2 回答 2

5

使用NSColorWell的标准方式。

如果您想要不同的东西,请查看 GitHub 和CocoaControls

...可能还有更多。

于 2013-07-26T12:22:45.593 回答
2

您可以使用CGDisplayCreateImageForRect获取CGImageRef包含您感兴趣的点的 a 。一旦您拥有 a CGImage,您可以通过将图像渲染到自定义缓冲区或使用NSBitmapImageRep's initWithCGImage然后从中获取颜色值colorAtX:y:。该NSBitmapImageRep方法的“写入堆栈溢出代码窗口”代码如下所示:

NSColor *MyColorAtScreenCoordinate(CGDirectDisplayID displayID, NSInteger x, NSInteger y) {
    CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(x, y, 1, 1));
    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image];
    CGImageRelease(image);
    NSColor *color = [bitmap colorAtX:0 y:0];
    [bitmap release];
}

这可能会返回设备颜色空间中的颜色。在校准的 RGB 颜色空间中返回颜色可能会很有用。

于 2013-07-26T12:21:22.147 回答