Skip to content

Commit

Permalink
Accessible lightHex and darkHex extension
Browse files Browse the repository at this point in the history
  • Loading branch information
amirsaam committed Jul 28, 2024
1 parent e016e1c commit b3d2f16
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Sources/RadixUI/RadixUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,30 @@ internal func radixColorToSwift(name: String) -> Color {

/// Create `RadixAlphaColors` percise values for `Light` & `Dark` schemes
#if canImport(UIKit)
typealias PlatformColor = UIColor
internal typealias PlatformColor = UIColor
#elseif canImport(AppKit)
typealias PlatformColor = NSColor
internal typealias PlatformColor = NSColor
#endif

extension Color {
init(lightHex: String, darkHex: String) {
public init(lightHex: String, darkHex: String) {
func colorFromHex(_ hex: String) -> PlatformColor {
let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var rgba: UInt64 = 0
Scanner(string: hex).scanHexInt64(&rgba)

let r = CGFloat((rgba & 0xff000000) >> 24) / 255.0
let g = CGFloat((rgba & 0x00ff0000) >> 16) / 255.0
let b = CGFloat((rgba & 0x0000ff00) >> 8) / 255.0
let a = CGFloat(rgba & 0x000000ff) / 255.0

let r, g, b, a: CGFloat
if hex.count == 8 {
r = CGFloat((rgba & 0xff000000) >> 24) / 255.0
g = CGFloat((rgba & 0x00ff0000) >> 16) / 255.0
b = CGFloat((rgba & 0x0000ff00) >> 8) / 255.0
a = CGFloat(rgba & 0x000000ff) / 255.0
} else {
r = CGFloat((rgba & 0xff0000) >> 16) / 255.0
g = CGFloat((rgba & 0x00ff00) >> 8) / 255.0
b = CGFloat(rgba & 0x0000ff) / 255.0
a = 1.0
}
return PlatformColor(red: r, green: g, blue: b, alpha: a)
}

Expand Down

0 comments on commit b3d2f16

Please sign in to comment.