Create a gist now

Instantly share code, notes, and snippets.

CGColorのenumを作ってみる
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