Skip to content

Commit 2d4ed08

Browse files
authored
Merge pull request SwiftGen#710 from iotize/fix/colors-type-check-time-warning
Colors: Fix long expression type check warning
2 parents cf14b2e + 81a4b3c commit 2d4ed08

13 files changed

+292
-84
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@
104104
* Core Data: the generated code was missing `,` (comma) for fetch requests with multiple arguments.
105105
[David Jennes](https://github.com/djbe)
106106
[#692](https://github.com/SwiftGen/SwiftGen/pull/692)
107+
* Colors: Fix compile time warning when long expression type checking is enabled.
108+
[Ryan Mason-Davies](https://github.com/iotize)
109+
[#704](https://github.com/SwiftGen/SwiftGen/issues/704)
110+
[#710](https://github.com/SwiftGen/SwiftGen/pull/710)
107111

108112
### Internal Changes
109113

Tests/Fixtures/Generated/Colors/swift4/defaults-customName.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,35 @@ internal struct XCTColors {
3535

3636
// MARK: - Implementation Details
3737

38-
// swiftlint:disable operator_usage_whitespace colon
3938
internal extension XCTColor {
4039
convenience init(rgbaValue: UInt32) {
41-
let red: CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
42-
let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
43-
let blue: CGFloat = CGFloat((rgbaValue >> UInt32( 8)) & UInt32(0xff)) / CGFloat(255.0)
44-
let alpha: CGFloat = CGFloat((rgbaValue ) & UInt32(0xff)) / CGFloat(255.0)
40+
let components = RGBAComponents(rgbaValue: rgbaValue).normalized
41+
self.init(red: components[0], green: components[1], blue: components[2], alpha: components[3])
42+
}
43+
}
44+
45+
private struct RGBAComponents {
46+
let rgbaValue: UInt32
47+
48+
private var shifts: [UInt32] {
49+
[
50+
rgbaValue >> 24, // red
51+
rgbaValue >> 16, // green
52+
rgbaValue >> 8, // blue
53+
rgbaValue // alpha
54+
]
55+
}
56+
57+
private var components: [CGFloat] {
58+
shifts.map {
59+
CGFloat($0 & 0xff)
60+
}
61+
}
4562

46-
self.init(red: red, green: green, blue: blue, alpha: alpha)
63+
var normalized: [CGFloat] {
64+
components.map { $0 / 255.0 }
4765
}
4866
}
49-
// swiftlint:enable operator_usage_whitespace colon
5067

5168
internal extension XCTColor {
5269
convenience init(named color: XCTColors) {

Tests/Fixtures/Generated/Colors/swift4/defaults-forceFileNameEnum.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,35 @@ internal struct ColorName {
3737

3838
// MARK: - Implementation Details
3939

40-
// swiftlint:disable operator_usage_whitespace colon
4140
internal extension Color {
4241
convenience init(rgbaValue: UInt32) {
43-
let red: CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
44-
let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
45-
let blue: CGFloat = CGFloat((rgbaValue >> UInt32( 8)) & UInt32(0xff)) / CGFloat(255.0)
46-
let alpha: CGFloat = CGFloat((rgbaValue ) & UInt32(0xff)) / CGFloat(255.0)
42+
let components = RGBAComponents(rgbaValue: rgbaValue).normalized
43+
self.init(red: components[0], green: components[1], blue: components[2], alpha: components[3])
44+
}
45+
}
46+
47+
private struct RGBAComponents {
48+
let rgbaValue: UInt32
49+
50+
private var shifts: [UInt32] {
51+
[
52+
rgbaValue >> 24, // red
53+
rgbaValue >> 16, // green
54+
rgbaValue >> 8, // blue
55+
rgbaValue // alpha
56+
]
57+
}
58+
59+
private var components: [CGFloat] {
60+
shifts.map {
61+
CGFloat($0 & 0xff)
62+
}
63+
}
4764

48-
self.init(red: red, green: green, blue: blue, alpha: alpha)
65+
var normalized: [CGFloat] {
66+
components.map { $0 / 255.0 }
4967
}
5068
}
51-
// swiftlint:enable operator_usage_whitespace colon
5269

5370
internal extension Color {
5471
convenience init(named color: ColorName) {

Tests/Fixtures/Generated/Colors/swift4/defaults-publicAccess.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,35 @@ public struct ColorName {
3535

3636
// MARK: - Implementation Details
3737

38-
// swiftlint:disable operator_usage_whitespace colon
3938
internal extension Color {
4039
convenience init(rgbaValue: UInt32) {
41-
let red: CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
42-
let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
43-
let blue: CGFloat = CGFloat((rgbaValue >> UInt32( 8)) & UInt32(0xff)) / CGFloat(255.0)
44-
let alpha: CGFloat = CGFloat((rgbaValue ) & UInt32(0xff)) / CGFloat(255.0)
40+
let components = RGBAComponents(rgbaValue: rgbaValue).normalized
41+
self.init(red: components[0], green: components[1], blue: components[2], alpha: components[3])
42+
}
43+
}
44+
45+
private struct RGBAComponents {
46+
let rgbaValue: UInt32
47+
48+
private var shifts: [UInt32] {
49+
[
50+
rgbaValue >> 24, // red
51+
rgbaValue >> 16, // green
52+
rgbaValue >> 8, // blue
53+
rgbaValue // alpha
54+
]
55+
}
56+
57+
private var components: [CGFloat] {
58+
shifts.map {
59+
CGFloat($0 & 0xff)
60+
}
61+
}
4562

46-
self.init(red: red, green: green, blue: blue, alpha: alpha)
63+
var normalized: [CGFloat] {
64+
components.map { $0 / 255.0 }
4765
}
4866
}
49-
// swiftlint:enable operator_usage_whitespace colon
5067

5168
public extension Color {
5269
convenience init(named color: ColorName) {

Tests/Fixtures/Generated/Colors/swift4/defaults.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,35 @@ internal struct ColorName {
3535

3636
// MARK: - Implementation Details
3737

38-
// swiftlint:disable operator_usage_whitespace colon
3938
internal extension Color {
4039
convenience init(rgbaValue: UInt32) {
41-
let red: CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
42-
let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
43-
let blue: CGFloat = CGFloat((rgbaValue >> UInt32( 8)) & UInt32(0xff)) / CGFloat(255.0)
44-
let alpha: CGFloat = CGFloat((rgbaValue ) & UInt32(0xff)) / CGFloat(255.0)
40+
let components = RGBAComponents(rgbaValue: rgbaValue).normalized
41+
self.init(red: components[0], green: components[1], blue: components[2], alpha: components[3])
42+
}
43+
}
44+
45+
private struct RGBAComponents {
46+
let rgbaValue: UInt32
47+
48+
private var shifts: [UInt32] {
49+
[
50+
rgbaValue >> 24, // red
51+
rgbaValue >> 16, // green
52+
rgbaValue >> 8, // blue
53+
rgbaValue // alpha
54+
]
55+
}
56+
57+
private var components: [CGFloat] {
58+
shifts.map {
59+
CGFloat($0 & 0xff)
60+
}
61+
}
4562

46-
self.init(red: red, green: green, blue: blue, alpha: alpha)
63+
var normalized: [CGFloat] {
64+
components.map { $0 / 255.0 }
4765
}
4866
}
49-
// swiftlint:enable operator_usage_whitespace colon
5067

5168
internal extension Color {
5269
convenience init(named color: ColorName) {

Tests/Fixtures/Generated/Colors/swift4/multiple.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,35 @@ internal struct ColorName {
6060

6161
// MARK: - Implementation Details
6262

63-
// swiftlint:disable operator_usage_whitespace colon
6463
internal extension Color {
6564
convenience init(rgbaValue: UInt32) {
66-
let red: CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
67-
let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
68-
let blue: CGFloat = CGFloat((rgbaValue >> UInt32( 8)) & UInt32(0xff)) / CGFloat(255.0)
69-
let alpha: CGFloat = CGFloat((rgbaValue ) & UInt32(0xff)) / CGFloat(255.0)
65+
let components = RGBAComponents(rgbaValue: rgbaValue).normalized
66+
self.init(red: components[0], green: components[1], blue: components[2], alpha: components[3])
67+
}
68+
}
69+
70+
private struct RGBAComponents {
71+
let rgbaValue: UInt32
72+
73+
private var shifts: [UInt32] {
74+
[
75+
rgbaValue >> 24, // red
76+
rgbaValue >> 16, // green
77+
rgbaValue >> 8, // blue
78+
rgbaValue // alpha
79+
]
80+
}
81+
82+
private var components: [CGFloat] {
83+
shifts.map {
84+
CGFloat($0 & 0xff)
85+
}
86+
}
7087

71-
self.init(red: red, green: green, blue: blue, alpha: alpha)
88+
var normalized: [CGFloat] {
89+
components.map { $0 / 255.0 }
7290
}
7391
}
74-
// swiftlint:enable operator_usage_whitespace colon
7592

7693
internal extension Color {
7794
convenience init(named color: ColorName) {

Tests/Fixtures/Generated/Colors/swift5/defaults-customName.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,35 @@ internal struct XCTColors {
3535

3636
// MARK: - Implementation Details
3737

38-
// swiftlint:disable operator_usage_whitespace colon
3938
internal extension XCTColor {
4039
convenience init(rgbaValue: UInt32) {
41-
let red: CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
42-
let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
43-
let blue: CGFloat = CGFloat((rgbaValue >> UInt32( 8)) & UInt32(0xff)) / CGFloat(255.0)
44-
let alpha: CGFloat = CGFloat((rgbaValue ) & UInt32(0xff)) / CGFloat(255.0)
40+
let components = RGBAComponents(rgbaValue: rgbaValue).normalized
41+
self.init(red: components[0], green: components[1], blue: components[2], alpha: components[3])
42+
}
43+
}
44+
45+
private struct RGBAComponents {
46+
let rgbaValue: UInt32
47+
48+
private var shifts: [UInt32] {
49+
[
50+
rgbaValue >> 24, // red
51+
rgbaValue >> 16, // green
52+
rgbaValue >> 8, // blue
53+
rgbaValue // alpha
54+
]
55+
}
56+
57+
private var components: [CGFloat] {
58+
shifts.map {
59+
CGFloat($0 & 0xff)
60+
}
61+
}
4562

46-
self.init(red: red, green: green, blue: blue, alpha: alpha)
63+
var normalized: [CGFloat] {
64+
components.map { $0 / 255.0 }
4765
}
4866
}
49-
// swiftlint:enable operator_usage_whitespace colon
5067

5168
internal extension XCTColor {
5269
convenience init(named color: XCTColors) {

Tests/Fixtures/Generated/Colors/swift5/defaults-forceFileNameEnum.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,35 @@ internal struct ColorName {
3737

3838
// MARK: - Implementation Details
3939

40-
// swiftlint:disable operator_usage_whitespace colon
4140
internal extension Color {
4241
convenience init(rgbaValue: UInt32) {
43-
let red: CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
44-
let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
45-
let blue: CGFloat = CGFloat((rgbaValue >> UInt32( 8)) & UInt32(0xff)) / CGFloat(255.0)
46-
let alpha: CGFloat = CGFloat((rgbaValue ) & UInt32(0xff)) / CGFloat(255.0)
42+
let components = RGBAComponents(rgbaValue: rgbaValue).normalized
43+
self.init(red: components[0], green: components[1], blue: components[2], alpha: components[3])
44+
}
45+
}
46+
47+
private struct RGBAComponents {
48+
let rgbaValue: UInt32
49+
50+
private var shifts: [UInt32] {
51+
[
52+
rgbaValue >> 24, // red
53+
rgbaValue >> 16, // green
54+
rgbaValue >> 8, // blue
55+
rgbaValue // alpha
56+
]
57+
}
58+
59+
private var components: [CGFloat] {
60+
shifts.map {
61+
CGFloat($0 & 0xff)
62+
}
63+
}
4764

48-
self.init(red: red, green: green, blue: blue, alpha: alpha)
65+
var normalized: [CGFloat] {
66+
components.map { $0 / 255.0 }
4967
}
5068
}
51-
// swiftlint:enable operator_usage_whitespace colon
5269

5370
internal extension Color {
5471
convenience init(named color: ColorName) {

Tests/Fixtures/Generated/Colors/swift5/defaults-publicAccess.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,35 @@ public struct ColorName {
3535

3636
// MARK: - Implementation Details
3737

38-
// swiftlint:disable operator_usage_whitespace colon
3938
internal extension Color {
4039
convenience init(rgbaValue: UInt32) {
41-
let red: CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
42-
let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
43-
let blue: CGFloat = CGFloat((rgbaValue >> UInt32( 8)) & UInt32(0xff)) / CGFloat(255.0)
44-
let alpha: CGFloat = CGFloat((rgbaValue ) & UInt32(0xff)) / CGFloat(255.0)
40+
let components = RGBAComponents(rgbaValue: rgbaValue).normalized
41+
self.init(red: components[0], green: components[1], blue: components[2], alpha: components[3])
42+
}
43+
}
44+
45+
private struct RGBAComponents {
46+
let rgbaValue: UInt32
47+
48+
private var shifts: [UInt32] {
49+
[
50+
rgbaValue >> 24, // red
51+
rgbaValue >> 16, // green
52+
rgbaValue >> 8, // blue
53+
rgbaValue // alpha
54+
]
55+
}
56+
57+
private var components: [CGFloat] {
58+
shifts.map {
59+
CGFloat($0 & 0xff)
60+
}
61+
}
4562

46-
self.init(red: red, green: green, blue: blue, alpha: alpha)
63+
var normalized: [CGFloat] {
64+
components.map { $0 / 255.0 }
4765
}
4866
}
49-
// swiftlint:enable operator_usage_whitespace colon
5067

5168
public extension Color {
5269
convenience init(named color: ColorName) {

Tests/Fixtures/Generated/Colors/swift5/defaults.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,35 @@ internal struct ColorName {
3535

3636
// MARK: - Implementation Details
3737

38-
// swiftlint:disable operator_usage_whitespace colon
3938
internal extension Color {
4039
convenience init(rgbaValue: UInt32) {
41-
let red: CGFloat = CGFloat((rgbaValue >> UInt32(24)) & UInt32(0xff)) / CGFloat(255.0)
42-
let green: CGFloat = CGFloat((rgbaValue >> UInt32(16)) & UInt32(0xff)) / CGFloat(255.0)
43-
let blue: CGFloat = CGFloat((rgbaValue >> UInt32( 8)) & UInt32(0xff)) / CGFloat(255.0)
44-
let alpha: CGFloat = CGFloat((rgbaValue ) & UInt32(0xff)) / CGFloat(255.0)
40+
let components = RGBAComponents(rgbaValue: rgbaValue).normalized
41+
self.init(red: components[0], green: components[1], blue: components[2], alpha: components[3])
42+
}
43+
}
44+
45+
private struct RGBAComponents {
46+
let rgbaValue: UInt32
47+
48+
private var shifts: [UInt32] {
49+
[
50+
rgbaValue >> 24, // red
51+
rgbaValue >> 16, // green
52+
rgbaValue >> 8, // blue
53+
rgbaValue // alpha
54+
]
55+
}
56+
57+
private var components: [CGFloat] {
58+
shifts.map {
59+
CGFloat($0 & 0xff)
60+
}
61+
}
4562

46-
self.init(red: red, green: green, blue: blue, alpha: alpha)
63+
var normalized: [CGFloat] {
64+
components.map { $0 / 255.0 }
4765
}
4866
}
49-
// swiftlint:enable operator_usage_whitespace colon
5067

5168
internal extension Color {
5269
convenience init(named color: ColorName) {

0 commit comments

Comments
 (0)