🦅

NSColor の Component と ColorSpace について

2021/04/17に公開

NSColor には.whiteComponent.redComponentcyanComponenthueComponentなどさまざまな色の要素の値を取得することができる変数が生えていますが、NSColor のもつ colorSpace によって取得できる component には決まりがあるようです。

ColorSpace LocalizedName Components Initializer
deviceGray Device Gray white, alpha NSColor(deviceWhite:alpha:)
genericGray Generic Gray white, alpha NSColor(calibratedWhite:alpha:)
genericGamma22Gray, extendedGenericGamma22Gray Generic Gray Gamma 2.2 Profile white, alpha NSColor(white:alpha:)
NSColor(genericGamma22White:alpha:)
deviceRGB Device RGB red, green, blue, alpha, hue, saturation, brightness NSColor(deviceRed:green:blue:alpha:)
genericRGB Generic RGB red, green, blue, alpha, hue, saturation, brightness NSColor(calibratedRed:green:blue:alpha:)
sRGB, extendedSRGB sRGB IEC61966-2.1 red, green, blue, alpha, hue, saturation, brightness NSColor(red:green:blue:alpha:)
NSColor(srgbRed:green:blue:alpha:)
displayP3 Display P3 red, green, blue, alpha, hue, saturation, brightness NSColor(displayP3Red:green:blue:alpha:)
adobeRGB1998 Adobe RGB (1998) red, green, blue, alpha, hue, saturation, brightness
deviceCMYK Device CMYK cyan, magenta, yellow, black, alpha NSColor(deviceCyan:magenta:yellow:black:alpha:)
genericCMYK Generic CMYK cyan, magenta, yellow, black, alpha
componentsを確認するextension
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

Gray スケール、RGB、CMYK の相互変換はよしなにやってくれているようです。(厳密には ColorSpace を正しく指定する必要があると思いますが)

Discussion