Skip to content

Commit

Permalink
chore(color): fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Jan 2, 2024
1 parent 214a30b commit 35d8343
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
7 changes: 5 additions & 2 deletions Sources/DSKit/SwiftUI/Color+Hex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import SwiftUI

extension Color {
init(hex: String) {
let (r, g, b, a) = hexToRGB(hex: hex)
init?(hex: String) {
guard let (r, g, b, a) = hexToRGB(hex: hex) else {
return nil
}

self.init(.sRGB, red: Double(r), green: Double(g), blue: Double(b), opacity: Double(a))
}
}
2 changes: 1 addition & 1 deletion Sources/DSKit/UIKit/String+HtmlAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension String {
"html *" +
"{" +
"font-size: \(size)px !important;" +
"color: \(color.hexValue) !important;" +
"color: \(color.hex) !important;" +
"font-family: \(fontFamily), San Francisco !important;" +
"}</style> \(self)"

Expand Down
8 changes: 5 additions & 3 deletions Sources/DSKit/UIKit/UIColor/UIColor+Hex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import UIKit

extension UIColor {
convenience init(hex: String) {
let (r, g, b, a) = hexToRGB(hex: hex)
convenience init?(hex: String) {
guard let (r, g, b, a) = hexToRGB(hex: hex) else {
return nil
}
self.init(red: r, green: g, blue: b, alpha: a)
}

var hexValue: String {
var hex: String {
guard let components = cgColor.components, components.count >= 3 else {
return ""
}
Expand Down
12 changes: 10 additions & 2 deletions Sources/DSKit/Utils/HexToRGB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
import Foundation
import CoreFoundation

internal func hexToRGB(hex: String) -> (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
internal func hexToRGB(hex: String) -> (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? {
let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)

// Check if the hex string is valid
let regex = "^[0-9a-fA-F]{3}$|^[0-9a-fA-F]{6}$|^[0-9a-fA-F]{8}$"
let test = NSPredicate(format:"SELF MATCHES %@", regex)
if !test.evaluate(with: hex) {
return nil
}

var int = UInt64()
Scanner(string: hex).scanHexInt64(&int)

Expand All @@ -22,7 +30,7 @@ internal func hexToRGB(hex: String) -> (red: CGFloat, green: CGFloat, blue: CGFl
case 8: // RRGGBBAA (32-bit)
(r, g, b, a) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
(a, r, g, b) = (1, 1, 1, 0)
return nil
}

return (CGFloat(r)/255, CGFloat(g)/255, CGFloat(b)/255, CGFloat(a)/255)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Color+Hex.swift
// HexToRGB.swift
// DSKit
//
// Created by David Chavez on 1/2/24.
Expand All @@ -10,26 +10,40 @@ import XCTest

class HexToRGBTests: XCTestCase {
func testHexToRGB() {
let rgb = hexToRGB(hex: "#0C0C1D")
guard let rgb = hexToRGB(hex: "#0C0C1D") else {
XCTFail("Invalid hex color")
return
}
XCTAssertEqual(rgb.red, 12.0/255.0, accuracy: 0.001)
XCTAssertEqual(rgb.green, 12.0/255.0, accuracy: 0.001)
XCTAssertEqual(rgb.blue, 29.0/255.0, accuracy: 0.001)
XCTAssertEqual(rgb.alpha, 1.0, accuracy: 0.001)
}

func testHexToRGBWithShortForm() {
let rgb = hexToRGB(hex: "#0C1")
guard let rgb = hexToRGB(hex: "#0C1") else {
XCTFail("Invalid hex color")
return
}
XCTAssertEqual(rgb.red, 0.0, accuracy: 0.001)
XCTAssertEqual(rgb.green, 204.0/255.0, accuracy: 0.001)
XCTAssertEqual(rgb.blue, 17.0/255.0, accuracy: 0.001)
XCTAssertEqual(rgb.alpha, 1.0, accuracy: 0.001)
}

func testHexToRGBWithAlpha() {
let rgb = hexToRGB(hex: "#0C0C1D80")
guard let rgb = hexToRGB(hex: "#0C0C1D80") else {
XCTFail("Invalid hex color")
return
}
XCTAssertEqual(rgb.red, 12.0/255.0, accuracy: 0.001)
XCTAssertEqual(rgb.green, 12.0/255.0, accuracy: 0.001)
XCTAssertEqual(rgb.blue, 29.0/255.0, accuracy: 0.001)
XCTAssertEqual(rgb.alpha, 128.0/255.0, accuracy: 0.001)
XCTAssertEqual(rgb.alpha, 128.0/255.0, accuracy: 0.001) // assuming alpha of 80 is 0.5
}

func testHexToRGBWithInvalidInput() {
let rgb = hexToRGB(hex: "#0C0G1D") // G is not a valid hexadecimal character
XCTAssertNil(rgb, "The function should return nil for invalid input")
}
}

0 comments on commit 35d8343

Please sign in to comment.