-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c353a2
commit ac51ca2
Showing
6 changed files
with
59 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |