diff --git a/Example.swiftpm/ContentView.swift b/Example.swiftpm/ContentView.swift index fa3fb24..da81cb1 100644 --- a/Example.swiftpm/ContentView.swift +++ b/Example.swiftpm/ContentView.swift @@ -18,7 +18,7 @@ class ContentViewController: UIViewController { let colorPickerButton = UIButton(primaryAction: UIAction(title: "noppefoxwolf/ColorPicker", handler: { _ in let vc = ColorPickerViewController() - var configuration = ColorPickerConfiguration.default + let configuration = ColorPickerConfiguration.default configuration.initialColor = .red configuration.initialColorItems = [.init(id: UUID(), color: .red)] vc.configuration = configuration diff --git a/Package.resolved b/Package.resolved index f743ab2..b7ee2e8 100644 --- a/Package.resolved +++ b/Package.resolved @@ -8,15 +8,6 @@ "revision" : "d458564516e5676af9c70b4f4b2a9178294f1bc6", "version" : "5.0.1" } - }, - { - "identity" : "uicolor-hex-swift", - "kind" : "remoteSourceControl", - "location" : "https://github.com/yeahdongcn/UIColor-Hex-Swift", - "state" : { - "revision" : "4e0c1bc57c02fcb19c3b34ec5e6188383a95aa94", - "version" : "5.1.8" - } } ], "version" : 2 diff --git a/Package.swift b/Package.swift index ca0193d..327ef93 100644 --- a/Package.swift +++ b/Package.swift @@ -15,14 +15,12 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/SnapKit/SnapKit", from: "5.0.1"), - .package(url: "https://github.com/yeahdongcn/UIColor-Hex-Swift", from: "5.1.8"), ], targets: [ .target( name: "ColorPicker", dependencies: [ "SnapKit", - .product(name: "UIColorHexSwift", package: "UIColor-Hex-Swift") ] ), .testTarget( diff --git a/Sources/ColorPicker/Extensions/ColorFormatter.swift b/Sources/ColorPicker/Extensions/ColorFormatter.swift new file mode 100644 index 0000000..4db00f2 --- /dev/null +++ b/Sources/ColorPicker/Extensions/ColorFormatter.swift @@ -0,0 +1,37 @@ +import Foundation +import UIKit + +class ColorFormatter: Formatter { + + func color(from string: String) -> UIColor? { + let hexString = string.trimmingCharacters(in: .whitespacesAndNewlines) + let scanner = Scanner(string: hexString) + + if hexString.hasPrefix("#") { + scanner.currentIndex = scanner.string.index(after: scanner.string.startIndex) + } + + var rgbValue:UInt64 = 0 + scanner.scanHexInt64(&rgbValue) + + return UIColor( + red: Double((rgbValue & 0xFF0000) >> 16) / 255.0, + green: Double((rgbValue & 0x00FF00) >> 8) / 255.0, + blue: Double(rgbValue & 0x0000FF) / 255.0, + alpha: 1.0 + ) + } + + func string(from color: UIColor) -> String { + var r: CGFloat = 0 + var g: CGFloat = 0 + var b: CGFloat = 0 + var a: CGFloat = 0 + + color.getRed(&r, green: &g, blue: &b, alpha: &a) + + let rgb: Int = (Int)(r * 255) << 16 | (Int)(g * 255) << 8 | (Int)(b * 255) << 0 + + return String(format: "%06x", rgb) + } +} diff --git a/Sources/ColorPicker/Feature/SliderColorPicker/HexInputView.swift b/Sources/ColorPicker/Feature/SliderColorPicker/HexInputView.swift index cef36b4..69c7068 100644 --- a/Sources/ColorPicker/Feature/SliderColorPicker/HexInputView.swift +++ b/Sources/ColorPicker/Feature/SliderColorPicker/HexInputView.swift @@ -1,13 +1,12 @@ import UIKit -import UIColorHexSwift class HexInputView: UIControl { var color: UIColor { get { - UIColor(textField.text!) + ColorFormatter().color(from: textField.text!) ?? .white } set { - textField.text = newValue.hexString(false) + textField.text = ColorFormatter().string(from: newValue) } } @@ -41,7 +40,7 @@ class HexInputView: UIControl { } textField.addAction(UIAction { [unowned self] _ in - self.color = UIColor(textField.text!) + self.color = ColorFormatter().color(from: textField.text!) ?? .white self.sendActions(for: [.editingDidEnd, .primaryActionTriggered]) }, for: .editingDidEnd) } diff --git a/Tests/ColorPickerTests/ColorFormatterTests.swift b/Tests/ColorPickerTests/ColorFormatterTests.swift new file mode 100644 index 0000000..1a42490 --- /dev/null +++ b/Tests/ColorPickerTests/ColorFormatterTests.swift @@ -0,0 +1,18 @@ +import XCTest +@testable import ColorPicker + +class ColorFormatterTests: XCTestCase { + func testEncodeDecodeWithHash() { + let string = "#123456" + let color = ColorFormatter().color(from: string)! + let string2 = ColorFormatter().string(from: color) + XCTAssertEqual(string2, "123456") + } + + func testEncodeDecode() { + let string = "123456" + let color = ColorFormatter().color(from: string)! + let string2 = ColorFormatter().string(from: color) + XCTAssertEqual(string2, "123456") + } +}