Skip to content

Commit

Permalink
fix(uicolor): Fix color to hex and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Mar 28, 2024
1 parent 4d49039 commit ba8d2fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Sources/DSKit/UIKit/UIColor/UIColor+Hex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public extension UIColor {
}

var hex: String {
guard let components = cgColor.components, components.count >= 3 else {
return ""
}
let red = Int(components[0] * 255)
let green = Int(components[1] * 255)
let blue = Int(components[2] * 255)
return String(format: "#%02X%02X%02X", red, green, blue)
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0

getRed(&red, green: &green, blue: &blue, alpha: nil)

let rgb: Int = (Int)(red*255)<<16 | (Int)(green*255)<<8 | (Int)(blue*255)<<0

return String(format: "#%06x", rgb)
}
}
16 changes: 16 additions & 0 deletions Tests/DSKitTests/UIKit/UIColor/UIColor+Hex.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// UIColor+Hex.swift
// DSKit
//
// Created by David Chavez on 3/28/24.
//

import XCTest
@testable import DSKit

final class UIColorHexTests: XCTestCase {
func testColorHex() {
XCTAssertEqual(UIColor.white.hex, "#ffffff")
XCTAssertEqual(UIColor.black.hex, "#000000")
}
}

0 comments on commit ba8d2fb

Please sign in to comment.