Created
March 1, 2017 06:22
-
-
Save takopom/8fee27dd76a269dadf829d779ea2dc81 to your computer and use it in GitHub Desktop.
CGColorのenumを作ってみる
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
enum Colors { | |
case red | |
case pink | |
case yellow | |
} | |
extension CGColor { | |
class func color(hex: UInt, alpha: CGFloat) -> CGColor { | |
return CGColor.init(red: CGFloat((hex & 0xff0000) >> 16) / 255, | |
green: CGFloat((hex & 0x00ff00) >> 8) / 255, | |
blue: CGFloat(hex & 0x0000ff) / 255, | |
alpha: alpha) | |
} | |
} | |
extension Colors: RawRepresentable { | |
typealias RawValue = CGColor | |
static let Red = CGColor.color(hex: 0xF26B7A, alpha: 1) | |
static let Pink = CGColor.color(hex: 0xFF63A1, alpha: 1) | |
static let Yellow = CGColor.color(hex: 0xFDEBA9, alpha: 1) | |
init?(rawValue: CGColor) { | |
switch rawValue { | |
case Colors.Red: | |
self = .red | |
case Colors.Pink: | |
self = .pink | |
case Colors.Yellow: | |
self = .yellow | |
default: | |
self = .yellow | |
} | |
} | |
var rawValue: CGColor { | |
switch self { | |
case .red: | |
return Colors.Red | |
case .pink: | |
return Colors.Pink | |
case .yellow: | |
return Colors.Yellow | |
default: | |
return Colors.Yellow | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment