Skip to content

Commit

Permalink
feat: added luminance calculation for named SwiftUI colors
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime committed Feb 22, 2024
1 parent 754f337 commit a50a605
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/RainbowSwiftUI/Color+Luminance.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import UIKit

@available(macOS 10.15, *)
public extension Color {
Expand All @@ -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 {
Expand Down

0 comments on commit a50a605

Please sign in to comment.