Skip to content

Commit ba8d2fb

Browse files
committed
fix(uicolor): Fix color to hex and add tests
1 parent 4d49039 commit ba8d2fb

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

Sources/DSKit/UIKit/UIColor/UIColor+Hex.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public extension UIColor {
1616
}
1717

1818
var hex: String {
19-
guard let components = cgColor.components, components.count >= 3 else {
20-
return ""
21-
}
22-
23-
let red = Int(components[0] * 255)
24-
let green = Int(components[1] * 255)
25-
let blue = Int(components[2] * 255)
26-
27-
return String(format: "#%02X%02X%02X", red, green, blue)
19+
var red: CGFloat = 0
20+
var green: CGFloat = 0
21+
var blue: CGFloat = 0
22+
23+
getRed(&red, green: &green, blue: &blue, alpha: nil)
24+
25+
let rgb: Int = (Int)(red*255)<<16 | (Int)(green*255)<<8 | (Int)(blue*255)<<0
26+
27+
return String(format: "#%06x", rgb)
2828
}
2929
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// UIColor+Hex.swift
3+
// DSKit
4+
//
5+
// Created by David Chavez on 3/28/24.
6+
//
7+
8+
import XCTest
9+
@testable import DSKit
10+
11+
final class UIColorHexTests: XCTestCase {
12+
func testColorHex() {
13+
XCTAssertEqual(UIColor.white.hex, "#ffffff")
14+
XCTAssertEqual(UIColor.black.hex, "#000000")
15+
}
16+
}

0 commit comments

Comments
 (0)