-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix in-app messages overlay color ignored from message payload (#…
…843)
- Loading branch information
1 parent
3e513bd
commit bbc708d
Showing
5 changed files
with
141 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import UIKit | ||
|
||
extension UIColor { | ||
static func fromHex(_ hex: String?) -> UIColor? { | ||
guard let hex = hex else { return nil } | ||
|
||
let cleanHex = hex.hasPrefix("#") ? String(hex.dropFirst()) : hex | ||
|
||
// Validate hex string length | ||
guard cleanHex.count == 6 || cleanHex.count == 8 else { | ||
return nil | ||
} | ||
|
||
let correctHex = correctColorFormatIfNeeded(cleanHex) | ||
|
||
var rgbValue: UInt64 = 0 | ||
guard Scanner(string: correctHex).scanHexInt64(&rgbValue) else { | ||
return nil | ||
} | ||
|
||
// Extract color components | ||
let red = CGFloat((rgbValue >> 16) & 0xFF) / 255.0 | ||
let green = CGFloat((rgbValue >> 8) & 0xFF) / 255.0 | ||
let blue = CGFloat(rgbValue & 0xFF) / 255.0 | ||
let alpha = cleanHex.count == 8 | ||
? CGFloat((rgbValue >> 24) & 0xFF) / 255.0 | ||
: 1.0 | ||
|
||
return UIColor(red: red, green: green, blue: blue, alpha: alpha) | ||
} | ||
|
||
private static func correctColorFormatIfNeeded(_ color: String) -> String { | ||
guard color.count == 8 else { return color } | ||
|
||
let red = color[color.index(color.startIndex, offsetBy: 0) ..< color.index(color.startIndex, offsetBy: 2)] | ||
let green = color[color.index(color.startIndex, offsetBy: 2) ..< color.index(color.startIndex, offsetBy: 4)] | ||
let blue = color[color.index(color.startIndex, offsetBy: 4) ..< color.index(color.startIndex, offsetBy: 6)] | ||
let alpha = color[color.index(color.startIndex, offsetBy: 6) ..< color.endIndex] | ||
|
||
return "\(alpha)\(red)\(green)\(blue)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
@testable import CioMessagingInApp | ||
|
||
import UIKit | ||
import XCTest | ||
|
||
class UIColorFromHexTests: UnitTest { | ||
func test_parseColor_givenInputColorIsNull_expectNullAsResult() { | ||
let result = UIColor.fromHex(nil) | ||
|
||
XCTAssertNil(result) | ||
} | ||
|
||
func test_parseColor_givenInputColorIsEmpty_expectNullAsResult() { | ||
let result = UIColor.fromHex("") | ||
|
||
XCTAssertNil(result) | ||
} | ||
|
||
func test_parseColor_givenInputColorHasUnexpectedCharCount_expectNullAsResult() { | ||
// Only colors with 6 or 8 chars are accepted | ||
XCTAssertNil(UIColor.fromHex("#")) | ||
XCTAssertNil(UIColor.fromHex("#FF11F")) | ||
XCTAssertNil(UIColor.fromHex("#FF11FF1")) | ||
} | ||
|
||
func test_parseColor_givenInputColorWithNonHexChars_expectNullResult() { | ||
XCTAssertNil(UIColor.fromHex("#MMXXMMYY")) | ||
} | ||
|
||
func test_parseColor_givenValidInputColorWithoutAlpha_expectCorrectResult() { | ||
let result = UIColor.fromHex("#007AFF") // R:0, G:122, B:255 | ||
|
||
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0 | ||
result?.getRed(&red, green: &green, blue: &blue, alpha: &alpha) | ||
|
||
XCTAssertEqual(red, 0.0, accuracy: 0.01) | ||
XCTAssertEqual(green, 0.48, accuracy: 0.01) | ||
XCTAssertEqual(blue, 1.00, accuracy: 0.01) | ||
XCTAssertEqual(alpha, 1.0, "Alpha should be 1.0 for 6-character hex.") | ||
} | ||
|
||
func test_parseColor_givenValidInputColorWithoutHashAndWithoutAlpha_expectCorrectResult() { | ||
let result = UIColor.fromHex("007AFF") // R:0, G:122, B:255 | ||
|
||
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0 | ||
result?.getRed(&red, green: &green, blue: &blue, alpha: &alpha) | ||
|
||
XCTAssertEqual(red, 0.0, accuracy: 0.01) | ||
XCTAssertEqual(green, 0.48, accuracy: 0.01) | ||
XCTAssertEqual(blue, 1.00, accuracy: 0.01) | ||
XCTAssertEqual(alpha, 1.0, "Alpha should be 1.0 for 6-character hex.") | ||
} | ||
|
||
func test_parseColor_givenValidInputColorWithAlpha_expectCorrectResult() { | ||
let result = UIColor.fromHex("#007AFF80") // R:0, G:122, B:255, Alpha:50% | ||
|
||
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0 | ||
result?.getRed(&red, green: &green, blue: &blue, alpha: &alpha) | ||
|
||
XCTAssertEqual(red, 0.0, accuracy: 0.01) | ||
XCTAssertEqual(green, 0.48, accuracy: 0.01) | ||
XCTAssertEqual(blue, 1.00, accuracy: 0.01) | ||
XCTAssertEqual(alpha, 0.5, accuracy: 0.01) | ||
} | ||
|
||
func test_parseColor_givenValidInputColorWithoutHashAndWithAlpha_expectCorrectResult() { | ||
let result = UIColor.fromHex("007AFF80") // R:0, G:122, B:255, Alpha:50% | ||
|
||
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0 | ||
result?.getRed(&red, green: &green, blue: &blue, alpha: &alpha) | ||
|
||
XCTAssertEqual(red, 0.0, accuracy: 0.01) | ||
XCTAssertEqual(green, 0.48, accuracy: 0.01) | ||
XCTAssertEqual(blue, 1.00, accuracy: 0.01) | ||
XCTAssertEqual(alpha, 0.5, accuracy: 0.01) | ||
} | ||
} |