快速-为什么当渲染时NSColor会变得更轻

时间:2022-11-02 16:53:09

I've created NSWindow and made it's background colour absolutely blue (#0000FF). But when the window is rendered, the colour is "lighter" than it should be (#0F3FFB).

我创建了NSWindow并将其背景颜色设置为绝对蓝色(#0000FF)。但是当窗口被渲染时,颜色比它应该的要“浅”(#0F3FFB)。

class LilWindow: NSViewController {
   override func viewDidLoad() {
        self.view.window?.backgroundColor = 
             NSColor.init(red: 0, green: 0, blue: 1, alpha: 1)


}

Does anyone know why it is happening and how to fix this? (screenshot attached)

有人知道为什么会发生这种情况吗?(附截图)

快速-为什么当渲染时NSColor会变得更轻

1 个解决方案

#1


1  

Okay, so after a couple of hours fiddling with code and @KenThomases help, I figured out that if you want your RGB colours to looks correctly on NSImages and NSWindows, you must convert it into NSDeviceRGBColorSpace colorspace. To do this I've written a simple function:

经过几个小时的代码修改和@KenThomases的帮助,我发现如果你想让RGB颜色在NSImages和NSWindows上看起来正确,你必须把它转换成NSDeviceRGBColorSpace。为此,我写了一个简单的函数:

func toScreenColor(color:NSColor) -> NSColor {
  var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0

  color
  .colorUsingColorSpaceName(NSCalibratedRGBColorSpace)!
  .getRed(&red, green: &green, blue: &blue, alpha: &alpha)

  return NSColor(deviceRed: red, green: green, blue: blue, alpha: alpha)
}

#1


1  

Okay, so after a couple of hours fiddling with code and @KenThomases help, I figured out that if you want your RGB colours to looks correctly on NSImages and NSWindows, you must convert it into NSDeviceRGBColorSpace colorspace. To do this I've written a simple function:

经过几个小时的代码修改和@KenThomases的帮助,我发现如果你想让RGB颜色在NSImages和NSWindows上看起来正确,你必须把它转换成NSDeviceRGBColorSpace。为此,我写了一个简单的函数:

func toScreenColor(color:NSColor) -> NSColor {
  var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0

  color
  .colorUsingColorSpaceName(NSCalibratedRGBColorSpace)!
  .getRed(&red, green: &green, blue: &blue, alpha: &alpha)

  return NSColor(deviceRed: red, green: green, blue: blue, alpha: alpha)
}