Skip to content

Instantly share code, notes, and snippets.

@swizzlr
Created October 14, 2015 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swizzlr/4268ac56f03edfc5d29d to your computer and use it in GitHub Desktop.
Save swizzlr/4268ac56f03edfc5d29d to your computer and use it in GitHub Desktop.
Literal Colors from hex values
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