Created
November 4, 2022 17:49
-
-
Save MickhailP/c9cdd53efdc2d34ab623df4fce8eabf7 to your computer and use it in GitHub Desktop.
HexadecimalColor
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
import SwiftUI | |
extension Color { | |
init?(hex: String) { | |
var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines) | |
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "") | |
var rgb: UInt64 = 0 | |
var red: CGFloat = 0.0 | |
var green: CGFloat = 0.0 | |
var blue: CGFloat = 0.0 | |
var alfa: CGFloat = 1.0 | |
let length = hexSanitized.count | |
guard Scanner(string: hexSanitized).scanHexInt64(&rgb) else { return nil } | |
if length == 6 { | |
red = CGFloat((rgb & 0xFF0000) >> 16) / 255.0 | |
green = CGFloat((rgb & 0x00FF00) >> 8) / 255.0 | |
blue = CGFloat(rgb & 0x0000FF) / 255.0 | |
} else if length == 8 { | |
red = CGFloat((rgb & 0xFF000000) >> 24) / 255.0 | |
green = CGFloat((rgb & 0x00FF0000) >> 16) / 255.0 | |
blue = CGFloat((rgb & 0x0000FF00) >> 8) / 255.0 | |
alfa = CGFloat(rgb & 0x000000FF) / 255.0 | |
} else { | |
return nil | |
} | |
self.init(red: red, green: green, blue: blue, opacity: alfa) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment