Skip to content

Commit

Permalink
remove Color dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
noppefoxwolf committed Mar 19, 2022
1 parent 0c353a2 commit ac51ca2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Example.swiftpm/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
37 changes: 37 additions & 0 deletions Sources/ColorPicker/Extensions/ColorFormatter.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

Expand Down Expand Up @@ -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)
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/ColorPickerTests/ColorFormatterTests.swift
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit ac51ca2

Please sign in to comment.