Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Add support for colors with alpha #561

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions ios/Classes/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,27 @@ extension UIColor {
let start = hexString.index(hexString.startIndex, offsetBy: 1)
let hexColor = hexString[start...]

let scanner = Scanner(string: String(hexColor))
var hexNumber: UInt64 = 0

if hexColor.count == 6 {
let scanner = Scanner(string: String(hexColor))
var hexNumber: UInt64 = 0

if scanner.scanHexInt64(&hexNumber) {
r = CGFloat((hexNumber & 0xff0000) >> 16) / 255
g = CGFloat((hexNumber & 0x00ff00) >> 8) / 255
b = CGFloat(hexNumber & 0x0000ff) / 255
a = 255

self.init(red: r, green: g, blue: b, alpha: a)
return
}
}
else if hexColor.count == 8 {
if scanner.scanHexInt64(&hexNumber) {
a = CGFloat((hexNumber & 0xff000000) >> 24) / 255
r = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
g = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
b = CGFloat(hexNumber & 0x000000ff) / 255

self.init(red: r, green: g, blue: b, alpha: a)
return
}
Expand Down