Skip to content

Commit fc3d2cd

Browse files
Mark Pospeselmpospese
authored andcommitted
[Issue-46] Clamp sRGB display values
1 parent 06398c4 commit fc3d2cd

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

Sources/YCoreUI/Extensions/UIKit/UIColor+rgbValue.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ public extension UIColor {
1717
func rgbDisplayString(prefix: String? = nil, isUppercase: Bool = true) -> String {
1818
let comp = rgbaComponents
1919
let format = isUppercase ? "%02X%02X%02X" : "%02x%02x%02x"
20+
let r = Int(round(comp.red * 255))
21+
let g = Int(round(comp.green * 255))
22+
let b = Int(round(comp.blue * 255))
23+
let isRGB = (r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255)
2024
let value = String(
2125
format: format,
22-
Int(round(comp.red * 255)),
23-
Int(round(comp.green * 255)),
24-
Int(round(comp.blue * 255))
26+
min(max(r, 0), 255),
27+
min(max(g, 0), 255),
28+
min(max(b, 0), 255)
2529
)
26-
return "\(prefix ?? "")\(value)"
30+
if !isRGB && YCoreUI.isLoggingEnabled {
31+
YCoreUI.colorLogger.warning("Color \(self) falls outside of the sRGB color space.")
32+
}
33+
return "\(prefix ?? "")\(value)\(isRGB ? "" : "⚠️")"
2734
}
2835
}

Sources/YCoreUI/YCoreUI+Logging.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ public struct YCoreUI {
1818
internal extension YCoreUI {
1919
/// Logger for warnings related to image loading. cf. `ImageAsset` and `SystemImage`
2020
static let imageLogger = Logger(subsystem: "YCoreUI", category: "images")
21+
/// Logger for warnings related to colors. cf `UIColor+rgbValue.swift`
22+
static let colorLogger = Logger(subsystem: "YCoreUI", category: "colors")
2123
}

Tests/YCoreUITests/Extensions/UIKit/UIColor+rgbValueTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import XCTest
10+
@testable import YCoreUI
1011

1112
// Large tuples help us build unit test expectations concisely
1213
// swiftlint:disable large_tuple
@@ -42,4 +43,32 @@ final class UIColorRgbValueTests: XCTestCase {
4243
// We exect each rgb color channel value to be rounded appropriately
4344
XCTAssertEqual(color.rgbDisplayString(), "80BFA6")
4445
}
46+
47+
func testSuffixes() {
48+
testCases.forEach {
49+
XCTAssertFalse($0.color.rgbDisplayString().hasSuffix("⚠️"))
50+
}
51+
52+
let p3Colors = [
53+
UIColor(displayP3Red: 1, green: 0, blue: 0, alpha: 1),
54+
UIColor(displayP3Red: 0, green: 1, blue: 0, alpha: 1),
55+
UIColor(displayP3Red: 0, green: 0, blue: 1, alpha: 1),
56+
UIColor(displayP3Red: 1, green: 1, blue: 0, alpha: 1),
57+
UIColor(displayP3Red: 1, green: 0, blue: 1, alpha: 1),
58+
UIColor(displayP3Red: 0, green: 1, blue: 1, alpha: 1),
59+
UIColor(red: 1.25, green: 0, blue: 0, alpha: 1),
60+
UIColor(red: 0, green: 1.25, blue: 0, alpha: 1),
61+
UIColor(red: 0, green: 0, blue: 1.25, alpha: 1),
62+
UIColor(red: -0.25, green: 0, blue: 0, alpha: 1),
63+
UIColor(red: 0, green: -0.25, blue: 0, alpha: 1),
64+
UIColor(red: 0, green: 0, blue: -0.25, alpha: 1)
65+
]
66+
67+
p3Colors.forEach {
68+
XCTAssertTrue($0.rgbDisplayString().hasSuffix("⚠️"))
69+
YCoreUI.isLoggingEnabled = false
70+
}
71+
72+
YCoreUI.isLoggingEnabled = true
73+
}
4574
}

0 commit comments

Comments
 (0)