Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init?(argbHexString:) to support the common ARGB format used in Android #971

Merged
merged 3 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
- **Color**:
- Refactored `init(light:dark:)` to remove deployment target version restrictions. [#844](https://github.com/SwifterSwift/SwifterSwift/pull/844) by [VincentSit](https://github.com/vincentsit).
- Use `enum` to declare namespace instead of using `struct`. Thus private initializer is no longer needed. [#927](https://github.com/SwifterSwift/SwifterSwift/pull/927) by [Shiva Huang](https://github.com/ShivaHuang)
- Add `init?(argbHexString:)` to support the common ARGB format used in Android. [#971](https://github.com/SwifterSwift/SwifterSwift/pull/971) by [yonat](https://github.com/yonat)
- **CAGradientLayer**:
- In `init(colors:locations:startPoint:endPoint:type:)` added default values to `startPoint` and `endPoint`. [#864](https://github.com/SwifterSwift/SwifterSwift/pull/864) by [guykogus](https://github.com/guykogus)
- **UITextField**:
Expand Down
27 changes: 27 additions & 0 deletions Sources/SwifterSwift/Shared/ColorExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,33 @@ public extension Color {
self.init(red: red, green: green, blue: blue, transparency: trans)
}

/// SwifterSwift: Create Color from hexadecimal string in the format ARGB (alpha-red-green-blue).
///
/// - Parameters:
/// - argbHexString: hexadecimal string (examples: 7FEDE7F6, 0x7FEDE7F6, #7FEDE7F6, #f0ff, 0xFF0F, ..).
convenience init?(argbHexString: String) {
var string = argbHexString.replacingOccurrences(of: "0x", with: "").replacingOccurrences(of: "#", with: "")

if string.count <= 4 { // convert hex to long format if in short format
var str = ""
for character in string {
str.append(String(repeating: String(character), count: 2))
}
string = str
}

guard let hexValue = Int(string, radix: 16) else { return nil }

let hasAlpha = string.count == 8

let alpha = hasAlpha ? (hexValue >> 24) & 0xFF : 0xFF
let red = (hexValue >> 16) & 0xFF
let green = (hexValue >> 8) & 0xFF
let blue = hexValue & 0xFF

self.init(red: red, green: green, blue: blue, transparency: CGFloat(alpha) / 255)
}

/// SwifterSwift: Create Color from a complementary of a Color (if applicable).
///
/// - Parameter color: color of which opposite color is desired.
Expand Down
30 changes: 30 additions & 0 deletions Tests/SharedTests/ColorExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,36 @@ final class ColorExtensionsTests: XCTestCase {
XCTAssertNotNil(color)
}

func testInitARGB() {
var color = Color(argbHexString: "0xFFFF")
XCTAssertNotNil(color)
XCTAssertEqual(color!.rgbComponents.red, 0xFF)
XCTAssertEqual(color!.rgbComponents.green, 0xFF)
XCTAssertEqual(color!.rgbComponents.blue, 0xFF)
XCTAssertEqual(color!.alpha, 1.0)

color = Color(argbHexString: "#FFFFFFFFF")
XCTAssertNotNil(color)
XCTAssertEqual(color!.rgbComponents.red, 0xFF)
XCTAssertEqual(color!.rgbComponents.green, 0xFF)
XCTAssertEqual(color!.rgbComponents.blue, 0xFF)
XCTAssertEqual(color!.alpha, 1.0)

color = Color(argbHexString: "7F123456")
XCTAssertNotNil(color)
XCTAssertEqual(color!.rgbComponents.red, 0x12)
XCTAssertEqual(color!.rgbComponents.green, 0x34)
XCTAssertEqual(color!.rgbComponents.blue, 0x56)
XCTAssertEqual(color!.alpha, 0.5, accuracy: 0.01)

color = Color(argbHexString: "#9999")
XCTAssertNotNil(color)
XCTAssertEqual(color!.rgbComponents.red, 0x99)
XCTAssertEqual(color!.rgbComponents.green, 0x99)
XCTAssertEqual(color!.rgbComponents.blue, 0x99)
XCTAssertEqual(color!.alpha, 0.6)
}

func testInitWithComponents() {
var red1: CGFloat = 0
var red2: CGFloat = 0
Expand Down