Skip to content

Instantly share code, notes, and snippets.

@abdelrahmanTD
Last active January 3, 2022 21:35
Show Gist options
  • Save abdelrahmanTD/580cb129c9bcf2934913b859ca372e7e to your computer and use it in GitHub Desktop.
Save abdelrahmanTD/580cb129c9bcf2934913b859ca372e7e to your computer and use it in GitHub Desktop.
Extension Color for converting hex codes
import SwiftUI
public extension Color {
init(hexCode: String) {
let scanner = Scanner(string: hexCode)
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let r = (rgbValue & 0xff0000) >> 16
let g = (rgbValue & 0xff00) >> 8
let b = rgbValue & 0xff
self.init(red: Double(r) / 255, green: Double(g) / 255, blue: Double(b) / 255)
}
}
// Color(hexCode: "ffffff") black color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment