Skip to content

Commit 684bddf

Browse files
authored
Merge pull request #9733 from DougGregor/cgcolor-color-literal
[CoreGraphics] Make CGColor adopt _ExpressibleByColorLiteral.
2 parents 2655dd4 + 2a9867b commit 684bddf

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

stdlib/public/SDK/CoreGraphics/CoreGraphics.swift

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import Darwin
1717
// CGAffineTransform
1818
//===----------------------------------------------------------------------===//
1919

20-
extension CGAffineTransform: Equatable {}
21-
public func ==(lhs: CGAffineTransform, rhs: CGAffineTransform) -> Bool {
22-
return lhs.__equalTo(rhs)
20+
extension CGAffineTransform: Equatable {
21+
public static func ==(lhs: CGAffineTransform,
22+
rhs: CGAffineTransform) -> Bool {
23+
return lhs.__equalTo(rhs)
24+
}
2325
}
2426

2527
//===----------------------------------------------------------------------===//
@@ -46,6 +48,19 @@ extension CGColor {
4648
#endif
4749
}
4850

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 { }
4964

5065
//===----------------------------------------------------------------------===//
5166
// CGColorSpace
@@ -228,10 +243,11 @@ extension CGPoint : CustomDebugStringConvertible {
228243
}
229244
}
230245

231-
extension CGPoint : Equatable {}
232-
@_transparent // @fragile
233-
public func == (lhs: CGPoint, rhs: CGPoint) -> Bool {
234-
return lhs.x == rhs.x && lhs.y == rhs.y
246+
extension CGPoint : Equatable {
247+
@_transparent // @fragile
248+
public static func == (lhs: CGPoint, rhs: CGPoint) -> Bool {
249+
return lhs.x == rhs.x && lhs.y == rhs.y
250+
}
235251
}
236252

237253
public extension CGSize {
@@ -279,10 +295,11 @@ extension CGSize : CustomDebugStringConvertible {
279295
}
280296
}
281297

282-
extension CGSize : Equatable {}
283-
@_transparent // @fragile
284-
public func == (lhs: CGSize, rhs: CGSize) -> Bool {
285-
return lhs.width == rhs.width && lhs.height == rhs.height
298+
extension CGSize : Equatable {
299+
@_transparent // @fragile
300+
public static func == (lhs: CGSize, rhs: CGSize) -> Bool {
301+
return lhs.width == rhs.width && lhs.height == rhs.height
302+
}
286303
}
287304

288305
public extension CGVector {
@@ -302,10 +319,11 @@ public extension CGVector {
302319
}
303320
}
304321

305-
extension CGVector : Equatable {}
306-
@_transparent // @fragile
307-
public func == (lhs: CGVector, rhs: CGVector) -> Bool {
308-
return lhs.dx == rhs.dx && lhs.dy == rhs.dy
322+
extension CGVector : Equatable {
323+
@_transparent // @fragile
324+
public static func == (lhs: CGVector, rhs: CGVector) -> Bool {
325+
return lhs.dx == rhs.dx && lhs.dy == rhs.dy
326+
}
309327
}
310328

311329
extension CGVector : CustomDebugStringConvertible {
@@ -380,10 +398,11 @@ extension CGRect : CustomDebugStringConvertible {
380398
}
381399
}
382400

383-
extension CGRect : Equatable {}
384-
@_transparent // @fragile
385-
public func == (lhs: CGRect, rhs: CGRect) -> Bool {
386-
return lhs.equalTo(rhs)
401+
extension CGRect : Equatable {
402+
@_transparent // @fragile
403+
public static func == (lhs: CGRect, rhs: CGRect) -> Bool {
404+
return lhs.equalTo(rhs)
405+
}
387406
}
388407

389408
extension CGAffineTransform {

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)