mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-20 04:03:26 +01:00
UIColorHex fix deprecations
Signed-off-by: Joseph Mattello <mail@joemattiello.com>
This commit is contained in:
committed by
Joe Mattiello
parent
a6559d8bb9
commit
3b824eac96
@@ -20,24 +20,54 @@ public extension UIColor
|
|||||||
let hexString = String.init(format: "%02lX%02lX%02lX", lroundf(Float(r * 255)), lroundf(Float(g * 255)), lroundf(Float(b * 255)))
|
let hexString = String.init(format: "%02lX%02lX%02lX", lroundf(Float(r * 255)), lroundf(Float(g * 255)), lroundf(Float(b * 255)))
|
||||||
return hexString
|
return hexString
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Borrowed from https://stackoverflow.com/a/33397427
|
|
||||||
convenience init?(hexString: String)
|
public extension UIColor {
|
||||||
{
|
convenience init?(hexString: String) {
|
||||||
let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
|
let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
var int = UInt32()
|
let scanner = Scanner(string: hexString)
|
||||||
Scanner(string: hex).scanHexInt32(&int)
|
|
||||||
let a, r, g, b: UInt32
|
if hexString.hasPrefix("#") {
|
||||||
switch hex.count {
|
scanner.scanLocation = 1
|
||||||
|
// TODO: Test if this works to replace the above deprecation @JoeMatt
|
||||||
|
// scanner.currentIndex = .init(utf16Offset: 1, in: hexString)
|
||||||
|
}
|
||||||
|
|
||||||
|
var hexNumber: UInt64 = 0
|
||||||
|
|
||||||
|
guard scanner.scanHexInt64(&hexNumber) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var alpha: UInt64 = 255
|
||||||
|
var red: UInt64 = 0
|
||||||
|
var green: UInt64 = 0
|
||||||
|
var blue: UInt64 = 0
|
||||||
|
|
||||||
|
switch hexString.count {
|
||||||
case 3: // RGB (12-bit)
|
case 3: // RGB (12-bit)
|
||||||
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
|
red = ((hexNumber & 0xF00) >> 8) * 17
|
||||||
|
green = ((hexNumber & 0x0F0) >> 4) * 17
|
||||||
|
blue = (hexNumber & 0x00F) * 17
|
||||||
case 6: // RGB (24-bit)
|
case 6: // RGB (24-bit)
|
||||||
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
|
red = (hexNumber & 0xFF0000) >> 16
|
||||||
|
green = (hexNumber & 0x00FF00) >> 8
|
||||||
|
blue = hexNumber & 0x0000FF
|
||||||
case 8: // ARGB (32-bit)
|
case 8: // ARGB (32-bit)
|
||||||
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
|
alpha = (hexNumber & 0xFF000000) >> 24
|
||||||
|
red = (hexNumber & 0x00FF0000) >> 16
|
||||||
|
green = (hexNumber & 0x0000FF00) >> 8
|
||||||
|
blue = hexNumber & 0x000000FF
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
|
|
||||||
|
self.init(
|
||||||
|
red: CGFloat(red) / 255,
|
||||||
|
green: CGFloat(green) / 255,
|
||||||
|
blue: CGFloat(blue) / 255,
|
||||||
|
alpha: CGFloat(alpha) / 255
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user