Skip to content

Commit 2a9867b

Browse files
committed
[CoreGraphics] Make CGColor adopt _ExpressibleByColorLiteral.
The backflips here are because we cannot add initializers to imported CF types; they would be factory initializers. Fixes rdar://problem/32196175.
1 parent 663c747 commit 2a9867b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

stdlib/public/SDK/CoreGraphics/CoreGraphics.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ extension CGColor {
4848
#endif
4949
}
5050

51+
public protocol _CGColorInitTrampoline {
52+
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
53+
}
54+
55+
extension _CGColorInitTrampoline {
56+
public init(colorLiteralRed red: Float, green: Float, blue: Float,
57+
alpha: Float) {
58+
self.init(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue),
59+
alpha: CGFloat(alpha))
60+
}
61+
}
62+
63+
extension CGColor : _CGColorInitTrampoline, _ExpressibleByColorLiteral { }
5164

5265
//===----------------------------------------------------------------------===//
5366
// CGColorSpace

validation-test/stdlib/CoreGraphics-execute.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ CoreGraphicsTests.test("CGColor.components") {
4848
expectEqual(components[3], 1)
4949
}
5050

51+
CoreGraphicsTests.test("CGColor/ExpressibleByColorLiteral") {
52+
let colorLit: CGColor = #colorLiteral(red: 0.25, green: 0.5, blue: 0.75,
53+
alpha: 1.0)
54+
let components = colorLit.components!
55+
expectEqual(components.count, 4)
56+
expectEqual(components[0], 0.25)
57+
expectEqual(components[1], 0.50)
58+
expectEqual(components[2], 0.75)
59+
expectEqual(components[3], 1.0)
60+
}
61+
5162
//===----------------------------------------------------------------------===//
5263
// CGPoint
5364
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)