NSColor의 Component 및 ColorSpace 정보

17361 단어 SwiftMacOStech
NSColor에는 .whiteComponent·.redComponent·cyanComponent·hueComponent 등 다양한 색상 요소의 값을 얻을 수 있는 변수가 있지만, NSColor가 보유한 color Space에는 규정이 있는 것으로 보인다.
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
그레이 비례자, RGB, CMYK의 상호 변환이 잘 된 것 같다.(엄밀히 말하면 ColorSpace를 올바르게 지정할 필요가 있다고 생각합니다.)

좋은 웹페이지 즐겨찾기