Skip to content

Commit ac51ca2

Browse files
committed
remove Color dependency
1 parent 0c353a2 commit ac51ca2

File tree

6 files changed

+59
-16
lines changed

6 files changed

+59
-16
lines changed

Example.swiftpm/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ContentViewController: UIViewController {
1818

1919
let colorPickerButton = UIButton(primaryAction: UIAction(title: "noppefoxwolf/ColorPicker", handler: { _ in
2020
let vc = ColorPickerViewController()
21-
var configuration = ColorPickerConfiguration.default
21+
let configuration = ColorPickerConfiguration.default
2222
configuration.initialColor = .red
2323
configuration.initialColorItems = [.init(id: UUID(), color: .red)]
2424
vc.configuration = configuration

Package.resolved

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ let package = Package(
1515
],
1616
dependencies: [
1717
.package(url: "https://github.com/SnapKit/SnapKit", from: "5.0.1"),
18-
.package(url: "https://github.com/yeahdongcn/UIColor-Hex-Swift", from: "5.1.8"),
1918
],
2019
targets: [
2120
.target(
2221
name: "ColorPicker",
2322
dependencies: [
2423
"SnapKit",
25-
.product(name: "UIColorHexSwift", package: "UIColor-Hex-Swift")
2624
]
2725
),
2826
.testTarget(
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
import UIKit
3+
4+
class ColorFormatter: Formatter {
5+
6+
func color(from string: String) -> UIColor? {
7+
let hexString = string.trimmingCharacters(in: .whitespacesAndNewlines)
8+
let scanner = Scanner(string: hexString)
9+
10+
if hexString.hasPrefix("#") {
11+
scanner.currentIndex = scanner.string.index(after: scanner.string.startIndex)
12+
}
13+
14+
var rgbValue:UInt64 = 0
15+
scanner.scanHexInt64(&rgbValue)
16+
17+
return UIColor(
18+
red: Double((rgbValue & 0xFF0000) >> 16) / 255.0,
19+
green: Double((rgbValue & 0x00FF00) >> 8) / 255.0,
20+
blue: Double(rgbValue & 0x0000FF) / 255.0,
21+
alpha: 1.0
22+
)
23+
}
24+
25+
func string(from color: UIColor) -> String {
26+
var r: CGFloat = 0
27+
var g: CGFloat = 0
28+
var b: CGFloat = 0
29+
var a: CGFloat = 0
30+
31+
color.getRed(&r, green: &g, blue: &b, alpha: &a)
32+
33+
let rgb: Int = (Int)(r * 255) << 16 | (Int)(g * 255) << 8 | (Int)(b * 255) << 0
34+
35+
return String(format: "%06x", rgb)
36+
}
37+
}

Sources/ColorPicker/Feature/SliderColorPicker/HexInputView.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import UIKit
2-
import UIColorHexSwift
32

43
class HexInputView: UIControl {
54
var color: UIColor {
65
get {
7-
UIColor(textField.text!)
6+
ColorFormatter().color(from: textField.text!) ?? .white
87
}
98
set {
10-
textField.text = newValue.hexString(false)
9+
textField.text = ColorFormatter().string(from: newValue)
1110
}
1211
}
1312

@@ -41,7 +40,7 @@ class HexInputView: UIControl {
4140
}
4241

4342
textField.addAction(UIAction { [unowned self] _ in
44-
self.color = UIColor(textField.text!)
43+
self.color = ColorFormatter().color(from: textField.text!) ?? .white
4544
self.sendActions(for: [.editingDidEnd, .primaryActionTriggered])
4645
}, for: .editingDidEnd)
4746
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import XCTest
2+
@testable import ColorPicker
3+
4+
class ColorFormatterTests: XCTestCase {
5+
func testEncodeDecodeWithHash() {
6+
let string = "#123456"
7+
let color = ColorFormatter().color(from: string)!
8+
let string2 = ColorFormatter().string(from: color)
9+
XCTAssertEqual(string2, "123456")
10+
}
11+
12+
func testEncodeDecode() {
13+
let string = "123456"
14+
let color = ColorFormatter().color(from: string)!
15+
let string2 = ColorFormatter().string(from: color)
16+
XCTAssertEqual(string2, "123456")
17+
}
18+
}

0 commit comments

Comments
 (0)