Created
June 17, 2019 18:54
-
-
Save konnnn/d6333ac733d958b6197e35b501c321df to your computer and use it in GitHub Desktop.
This file contains 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
// Created by Evgeny Konkin on 17.06.2019. | |
extension UIColor { | |
static let themeExample = { () -> UIColor in | |
let color = UIColor(hexString: "#7C7C7C") | |
return color | |
}() | |
static let wGray = UIColor(hexString: "#7C7C7C") | |
static let wDarkGray = UIColor(hexString: "#222222") | |
static let wLightGray = UIColor(hexString: "#E6E6E6") | |
convenience init(hexString: String, alpha: CGFloat = 1.0) { | |
let hexString: String = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) | |
let scanner = Scanner(string: hexString) | |
if (hexString.hasPrefix("#")) { | |
scanner.scanLocation = 1 | |
} | |
var color: UInt32 = 0 | |
scanner.scanHexInt32(&color) | |
let mask = 0x000000FF | |
let r = Int(color >> 16) & mask | |
let g = Int(color >> 8) & mask | |
let b = Int(color) & mask | |
let red = CGFloat(r) / 255.0 | |
let green = CGFloat(g) / 255.0 | |
let blue = CGFloat(b) / 255.0 | |
self.init(red: red, green: green, blue: blue, alpha: alpha) | |
} | |
func toHexString() -> String { | |
var r:CGFloat = 0 | |
var g:CGFloat = 0 | |
var b:CGFloat = 0 | |
var a:CGFloat = 0 | |
getRed(&r, green: &g, blue: &b, alpha: &a) | |
let rgb:Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0 | |
return String(format:"#%06x", rgb) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment