Use CGColor and remove PlatformColor
This commit is contained in:
20
Sources/SwiftUIMath/Internal/Helpers/CGColor+HexString.swift
Normal file
20
Sources/SwiftUIMath/Internal/Helpers/CGColor+HexString.swift
Normal file
@@ -0,0 +1,20 @@
|
||||
import CoreGraphics
|
||||
import Foundation
|
||||
|
||||
extension CGColor {
|
||||
static func fromHexString(_ hexString: String) -> CGColor? {
|
||||
guard !hexString.isEmpty, hexString.hasPrefix("#") else { return nil }
|
||||
|
||||
var rgbValue = UInt64(0)
|
||||
let scanner = Scanner(string: hexString)
|
||||
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
|
||||
guard scanner.scanHexInt64(&rgbValue) else { return nil }
|
||||
|
||||
return CGColor(
|
||||
srgbRed: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
|
||||
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
|
||||
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
|
||||
alpha: 1.0
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
#if canImport(UIKit)
|
||||
typealias PlatformColor = UIColor
|
||||
typealias PlatformBezierPath = UIBezierPath
|
||||
#elseif canImport(AppKit)
|
||||
typealias PlatformColor = NSColor
|
||||
typealias PlatformBezierPath = NSBezierPath
|
||||
#endif
|
||||
|
||||
extension PlatformColor {
|
||||
convenience init?(fromHexString hexString: String) {
|
||||
self.init(hexString: hexString)
|
||||
}
|
||||
|
||||
convenience init?(hexString: String) {
|
||||
guard !hexString.isEmpty, hexString.hasPrefix("#") else { return nil }
|
||||
|
||||
var rgbValue = UInt64(0)
|
||||
let scanner = Scanner(string: hexString)
|
||||
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
|
||||
guard scanner.scanHexInt64(&rgbValue) else { return nil }
|
||||
|
||||
self.init(
|
||||
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
|
||||
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
|
||||
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
|
||||
alpha: 1.0
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user