关于 NSColor 组件和 ColorSpace

NSColor.whiteComponent.redComponentcyanComponent,hueComponent是一个变量,可以获取各种颜色元素的值,例如增长,但似乎可以确定的是 colorSpace 可以通过 NSColor 获取的组件。

ColorSpaceLocalizedNameComponentsInitializer
deviceGrayDevice Graywhite, alphaNSColor(deviceWhite:alpha:)
genericGrayGeneric Graywhite, alphaNSColor(calibratedWhite:alpha:)
genericGamma22Gray, extendedGenericGamma22GrayGeneric Gray Gamma 2.2 Profilewhite, alphaNSColor(white:alpha:)
NSColor(genericGamma22White:alpha:)
deviceRGBDevice RGBred, green, blue, alpha, hue, saturation, brightnessNSColor(deviceRed:green:blue:alpha:)
genericRGBGeneric RGBred, green, blue, alpha, hue, saturation, brightnessNSColor(calibratedRed:green:blue:alpha:)
sRGB, extendedSRGBsRGB IEC61966-2.1red, green, blue, alpha, hue, saturation, brightnessNSColor(red:green:blue:alpha:)
NSColor(srgbRed:green:blue:alpha:)
displayP3Display P3red, green, blue, alpha, hue, saturation, brightnessNSColor(displayP3Red:green:blue:alpha:)
adobeRGB1998Adobe RGB (1998)red, green, blue, alpha, hue, saturation, brightness
deviceCMYKDevice CMYKcyan, magenta, yellow, black, alphaNSColor(deviceCyan:magenta:yellow:black:alpha:)
genericCMYKGeneric CMYKcyan, magenta, yellow, black, alpha
extension NSColor {
    func printComponents() {
        print("🌈", self.colorSpace.localizedName ?? "unknown")
        print("Number of Components:", self.colorSpace.numberOfColorComponents)
        switch self.colorSpace {
        case .genericGray, .deviceGray, .genericGamma22Gray, .extendedGenericGamma22Gray:
            print("- white", self.whiteComponent)
        case .genericRGB, .deviceRGB, .sRGB, .extendedSRGB, .displayP3, .adobeRGB1998:
            print("- red", self.redComponent)
            print("- green", self.greenComponent)
            print("- blue", self.blueComponent)
            print("- hue:", self.hueComponent)
            print("- saturation:", self.saturationComponent)
            print("- brightness:", self.brightnessComponent)
        case .genericCMYK, .deviceCMYK:
            print("- cyan", self.cyanComponent)
            print("- magenta", self.magentaComponent)
            print("- yellow", self.yellowComponent)
            print("- black", self.blackComponent)
        default:
            break
        }
        print("- alpha:", self.alphaComponent)
        print()
    }
}

要在没有初始化程序的情况下使用 ColorSpace,请.usingColorSpace(NSColorSpace)使用。例子

let deviceGray = NSColor(deviceWhite: 0.2, alpha: 1.0)
deviceGray.printComponents()
deviceGray.usingColorSpace(.sRGB)?.printComponents()
deviceGray.usingColorSpace(.deviceCMYK)?.printComponents()

let deviceRGB = NSColor(deviceRed: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
deviceRGB.printComponents()
deviceRGB.usingColorSpace(.deviceGray)?.printComponents()
deviceRGB.usingColorSpace(.deviceCMYK)?.printComponents()

let deviceCMYK = NSColor(deviceCyan: 0.2, magenta: 0.2, yellow: 0.2, black: 0.2, alpha: 1.0)
deviceCMYK.printComponents()
deviceCMYK.usingColorSpace(.deviceGray)?.printComponents()
deviceCMYK.usingColorSpace(.deviceRGB)?.printComponents()

结果

🌈 Device Gray
Number of Components: 1
- white 0.2
- alpha: 1.0

🌈 sRGB IEC61966-2.1
Number of Components: 3
- red 0.20000000298023224
- green 0.20000000298023224
- blue 0.20000000298023224
- hue: 0.0
- saturation: 0.0
- brightness: 0.20000000298023224
- alpha: 1.0

🌈 Device CMYK
Number of Components: 4
- cyan 0.8039318323135376
- magenta 0.7498183250427246
- yellow 0.7066810727119446
- black 0.37967559695243835
- alpha: 1.0

🌈 Device RGB
Number of Components: 3
- red 0.2
- green 0.2
- blue 0.2
- hue: 0.0
- saturation: 0.0
- brightness: 0.2
- alpha: 1.0

🌈 Device Gray
Number of Components: 1
- white 0.1999967396259308
- alpha: 1.0

🌈 Device CMYK
Number of Components: 4
- cyan 0.8038681149482727
- magenta 0.7498422861099243
- yellow 0.7064858078956604
- black 0.3797006607055664
- alpha: 1.0

🌈 Device CMYK
Number of Components: 4
- cyan 0.2
- magenta 0.2
- yellow 0.2
- black 0.2
- alpha: 1.0

🌈 Device Gray
Number of Components: 1
- white 0.617861270904541
- alpha: 1.0

🌈 Device RGB
Number of Components: 3
- red 0.6390809416770935
- green 0.613300621509552
- blue 0.5913392305374146
- hue: 0.07666737830114123
- saturation: 0.07470370030812351
- brightness: 0.6390809416770935
- alpha: 1.0

灰度、RGB、CMYK 相互转换似乎做得很好。(严格来说,我认为您需要正确指定 ColorSpace)

原文出处:Zenn.dev

原文链接:https://zenn.dev/kyome/articles/05678ad2542ea7

本文观点不代表Dotnet9立场,转载请联系原作者。

发表评论

登录后才能评论