Skip to content

Instantly share code, notes, and snippets.

@takopom
Created March 1, 2017 06:22
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 takopom/8fee27dd76a269dadf829d779ea2dc81 to your computer and use it in GitHub Desktop.
Save takopom/8fee27dd76a269dadf829d779ea2dc81 to your computer and use it in GitHub Desktop.
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