Created
February 21, 2026 05:09
-
-
Save StewartLynch/4726068c7ce6b79b754d9774a1125eb1 to your computer and use it in GitHub Desktop.
Color Extension for GiftRegistry App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| //---------------------------------------------- | |
| // Original project: GiftRegistry | |
| // | |
| // Follow me on Mastodon: https://iosdev.space/@StewartLynch | |
| // Follow me on Threads: https://www.threads.net/@stewartlynch | |
| // Follow me on Bluesky: https://bsky.app/profile/stewartlynch.bsky.social | |
| // Follow me on X: https://x.com/StewartLynch | |
| // Follow me on LinkedIn: https://linkedin.com/in/StewartLynch | |
| // Email: slynch@createchsol.com | |
| // Subscribe on YouTube: https://youTube.com/@StewartLynch | |
| // Buy me a ko-fi: https://ko-fi.com/StewartLynch | |
| //---------------------------------------------- | |
| // Copyright © 2026 CreaTECH Solutions (Stewart Lynch). All rights reserved. | |
| import SwiftUI | |
| extension Color { | |
| init?(hex: String) { | |
| guard let uiColor = UIColor(hex: hex) else { return nil } | |
| self.init(uiColor: uiColor) | |
| } | |
| var toHexString: String { | |
| UIColor(self).toHexString(includeAlpha: false) ?? "0000FF" | |
| } | |
| var toHexSttringWithAlpha: String { | |
| UIColor(self).toHexString(includeAlpha: true) ?? "0000FF" | |
| } | |
| var luminance: Double { | |
| // Convert SwiftUI Color to UIColor | |
| let uiColor = UIColor(self) | |
| // Extract RGB values | |
| var red: CGFloat = 0 | |
| var green: CGFloat = 0 | |
| var blue: CGFloat = 0 | |
| uiColor.getRed(&red, green: &green, blue: &blue, alpha: nil) | |
| // Compute luminance. | |
| return 0.2126 * red + 0.7152 * green + 0.0722 * blue | |
| } | |
| var adaptedTextColor: Color { | |
| luminance > 0.5 ? Color.black : Color.white | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment