From a50a605469a64896e717e652f540f1454e4c581d Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Thu, 22 Feb 2024 11:55:56 +0100 Subject: [PATCH] feat: added luminance calculation for named SwiftUI colors --- Sources/RainbowSwiftUI/Color+Luminance.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/RainbowSwiftUI/Color+Luminance.swift b/Sources/RainbowSwiftUI/Color+Luminance.swift index 727718a..799c731 100644 --- a/Sources/RainbowSwiftUI/Color+Luminance.swift +++ b/Sources/RainbowSwiftUI/Color+Luminance.swift @@ -1,4 +1,5 @@ import SwiftUI +import UIKit @available(macOS 10.15, *) public extension Color { @@ -9,7 +10,10 @@ public extension Color { /// - Returns: Luminance ("brightness") of color @available(macOS 11.0, iOS 14.0, *) func luminance() -> CGFloat { - guard let components = cgColor?.components, !components.isEmpty else { + // SwiftUI is handling named colors differently, and the property `cgColor` is not always set. + // Therefore, the SwiftUI.Color is converted to UIKit.UIColor + let components = UIColor(self).cgColor.components ?? [] + guard !components.isEmpty else { return 0 } guard components.count >= 3 else {