Skip to content

Commit 4e2e875

Browse files
Mark Pospeselmpospese
authored andcommitted
[Issue-46] Add a separate rgbDebugDisplayString method
1 parent fc3d2cd commit 4e2e875

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ public extension UIColor {
1515
/// - isUppercase: whether the hex values should be upper or lower case
1616
/// - Returns: the formatted hexadecimal string
1717
func rgbDisplayString(prefix: String? = nil, isUppercase: Bool = true) -> String {
18+
_rgbDisplayString(prefix: prefix, isUppercase: isUppercase, isDebug: false)
19+
}
20+
21+
/// Formats a color as an RGB hexadecimal string. Appropriate for debug printing.
22+
/// - Parameters:
23+
/// - prefix: optional prefix to precede the hexadecimal value such as `0x` or `#` (default = nil)
24+
/// - isUppercase: whether the hex values should be upper or lower case
25+
/// - Returns: the formatted hexadecimal string (with an `⚠️` for colors that fall outside of the sRGB color space)
26+
func rgbDebugDisplayString(prefix: String? = nil, isUppercase: Bool = true) -> String {
27+
_rgbDisplayString(prefix: prefix, isUppercase: isUppercase, isDebug: true)
28+
}
29+
30+
private func _rgbDisplayString(prefix: String?, isUppercase: Bool, isDebug: Bool) -> String {
1831
let comp = rgbaComponents
1932
let format = isUppercase ? "%02X%02X%02X" : "%02x%02x%02x"
2033
let r = Int(round(comp.red * 255))
@@ -30,6 +43,6 @@ public extension UIColor {
3043
if !isRGB && YCoreUI.isLoggingEnabled {
3144
YCoreUI.colorLogger.warning("Color \(self) falls outside of the sRGB color space.")
3245
}
33-
return "\(prefix ?? "")\(value)\(isRGB ? "" : "⚠️")"
46+
return "\(prefix ?? "")\(value)\(isDebug && !isRGB ? "⚠️" : "")"
3447
}
3548
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ final class UIColorRgbValueTests: XCTestCase {
4747
func testSuffixes() {
4848
testCases.forEach {
4949
XCTAssertFalse($0.color.rgbDisplayString().hasSuffix("⚠️"))
50+
XCTAssertFalse($0.color.rgbDebugDisplayString().hasSuffix("⚠️"))
51+
XCTAssertEqual($0.color.rgbDisplayString(), $0.color.rgbDebugDisplayString())
5052
}
5153

5254
let p3Colors = [
@@ -65,7 +67,8 @@ final class UIColorRgbValueTests: XCTestCase {
6567
]
6668

6769
p3Colors.forEach {
68-
XCTAssertTrue($0.rgbDisplayString().hasSuffix("⚠️"))
70+
XCTAssertFalse($0.rgbDisplayString().hasSuffix("⚠️"))
71+
XCTAssertTrue($0.rgbDebugDisplayString().hasSuffix("⚠️"))
6972
YCoreUI.isLoggingEnabled = false
7073
}
7174

0 commit comments

Comments
 (0)