Created
October 14, 2015 13:26
-
-
Save swizzlr/4268ac56f03edfc5d29d to your computer and use it in GitHub Desktop.
Literal Colors from hex values
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
public final class LiteralColor: UIColor, IntegerLiteralConvertible { | |
public typealias IntegerLiteralType = Int | |
public init(integerLiteral value: Int) { | |
let netHex = value | |
let red = (netHex >> 16) & 0xff | |
let green = (netHex >> 8) & 0xff | |
let blue = netHex & 0xff | |
assert(red >= 0 && red <= 255, "Invalid red component") | |
assert(green >= 0 && green <= 255, "Invalid green component") | |
assert(blue >= 0 && blue <= 255, "Invalid blue component") | |
super.init(red: CGFloat(red) / (255.0 as CGFloat), green: CGFloat(green) / (255.0 as CGFloat), blue: CGFloat(blue) / (255.0 as CGFloat), alpha: (1.0 as CGFloat)) | |
} | |
required public init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment