Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Swift-3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
yeahdongcn committed Sep 21, 2016
2 parents 21969f8 + f5420ef commit c64695a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
12 changes: 6 additions & 6 deletions HEXColor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@
PRODUCT_NAME = HEXColor;
SDKROOT = watchos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 4;
WATCHOS_DEPLOYMENT_TARGET = 2.1;
};
Expand All @@ -330,7 +330,7 @@
PRODUCT_NAME = HEXColor;
SDKROOT = watchos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 4;
WATCHOS_DEPLOYMENT_TARGET = 2.1;
};
Expand Down Expand Up @@ -448,7 +448,7 @@
PRODUCT_NAME = HEXColor;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -469,7 +469,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.pdq.HEXColor;
PRODUCT_NAME = HEXColor;
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -480,7 +480,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.pdq.HEXColorTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -491,7 +491,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.pdq.HEXColorTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
29 changes: 15 additions & 14 deletions HEXColor/UIColorExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import UIKit
UnableToScanHexValue: "Scan hex error"
MismatchedHexStringLength: "Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8"
*/
public enum UIColorInputError : ErrorType {
case MissingHashMarkAsPrefix,
UnableToScanHexValue,
MismatchedHexStringLength
public enum UIColorInputError : Error {
case missingHashMarkAsPrefix,
unableToScanHexValue,
mismatchedHexStringLength
}

extension UIColor {
Expand Down Expand Up @@ -84,13 +84,14 @@ extension UIColor {
*/
public convenience init(rgba_throws rgba: String) throws {
guard rgba.hasPrefix("#") else {
throw UIColorInputError.MissingHashMarkAsPrefix
throw UIColorInputError.missingHashMarkAsPrefix
}

guard let hexString: String = rgba.substringFromIndex(rgba.startIndex.advancedBy(1)),
var hexValue: UInt32 = 0
where NSScanner(string: hexString).scanHexInt(&hexValue) else {
throw UIColorInputError.UnableToScanHexValue
let hexString: String = rgba.substring(from: rgba.characters.index(rgba.startIndex, offsetBy: 1))
var hexValue: UInt32 = 0

guard Scanner(string: hexString).scanHexInt32(&hexValue) else {
throw UIColorInputError.unableToScanHexValue
}

switch (hexString.characters.count) {
Expand All @@ -103,7 +104,7 @@ extension UIColor {
case 8:
self.init(hex8: hexValue)
default:
throw UIColorInputError.MismatchedHexStringLength
throw UIColorInputError.mismatchedHexStringLength
}
}

Expand All @@ -112,20 +113,20 @@ extension UIColor {

- parameter rgba: String value.
*/
public convenience init(rgba: String, defaultColor: UIColor = UIColor.clearColor()) {
public convenience init(_ rgba: String, defaultColor: UIColor = UIColor.clear) {
guard let color = try? UIColor(rgba_throws: rgba) else {
self.init(CGColor: defaultColor.CGColor)
self.init(cgColor: defaultColor.cgColor)
return
}
self.init(CGColor: color.CGColor)
self.init(cgColor: color.cgColor)
}

/**
Hex string of a UIColor instance.

- parameter rgba: Whether the alpha should be included.
*/
public func hexString(includeAlpha: Bool) -> String {
public func hexString(_ includeAlpha: Bool) -> String {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
Expand Down
37 changes: 18 additions & 19 deletions HEXColorTests/HEXColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ class HEXColorTests: XCTestCase {
func testStringInputErrorMissingHashMarkAsPrefix() {
do {
let _ = try UIColor(rgba_throws: "FFFFFFFF")
} catch UIColorInputError.MissingHashMarkAsPrefix {
} catch UIColorInputError.missingHashMarkAsPrefix {
XCTAssertTrue(true)
} catch UIColorInputError.UnableToScanHexValue {
} catch UIColorInputError.unableToScanHexValue {
XCTAssertTrue(false)
} catch UIColorInputError.MismatchedHexStringLength {
} catch UIColorInputError.mismatchedHexStringLength {
XCTAssertTrue(false)
} catch {
XCTAssertTrue(false)
Expand All @@ -245,11 +245,11 @@ class HEXColorTests: XCTestCase {
func testStringInputErrorMismatchedHexStringLength() {
do {
let _ = try UIColor(rgba_throws: "#FFFFFFF")
} catch UIColorInputError.MissingHashMarkAsPrefix {
} catch UIColorInputError.missingHashMarkAsPrefix {
XCTAssertTrue(false)
} catch UIColorInputError.UnableToScanHexValue {
} catch UIColorInputError.unableToScanHexValue {
XCTAssertTrue(false)
} catch UIColorInputError.MismatchedHexStringLength {
} catch UIColorInputError.mismatchedHexStringLength {
XCTAssertTrue(true)
} catch {
XCTAssertTrue(false)
Expand All @@ -259,11 +259,11 @@ class HEXColorTests: XCTestCase {
func testStringInputErrorUnableToScanHexValue() {
do {
let _ = try UIColor(rgba_throws: "#ONMPQRST")
} catch UIColorInputError.MissingHashMarkAsPrefix {
} catch UIColorInputError.missingHashMarkAsPrefix {
XCTAssertTrue(false)
} catch UIColorInputError.UnableToScanHexValue {
} catch UIColorInputError.unableToScanHexValue {
XCTAssertTrue(true)
} catch UIColorInputError.MismatchedHexStringLength {
} catch UIColorInputError.mismatchedHexStringLength {
XCTAssertTrue(false)
} catch {
XCTAssertTrue(false)
Expand All @@ -273,12 +273,12 @@ class HEXColorTests: XCTestCase {
// MARK: - String (With default color)

func testStringDefaultColor() {
var color = UIColor(rgba: "FFFFFFFF")
XCTAssertEqual(color, UIColor.clearColor())
color = UIColor(rgba: "#FFFFFFF")
XCTAssertEqual(color, UIColor.clearColor())
color = UIColor(rgba: "#ONMPQRST")
XCTAssertEqual(color, UIColor.clearColor())
var color = UIColor("FFFFFFFF")
XCTAssertEqual(color, UIColor.clear)
color = UIColor("#FFFFFFF")
XCTAssertEqual(color, UIColor.clear)
color = UIColor("#ONMPQRST")
XCTAssertEqual(color, UIColor.clear)
}

// MARK: - Hex string output
Expand All @@ -288,20 +288,19 @@ class HEXColorTests: XCTestCase {
XCTAssertEqual("#224466", color.hexString(false))
XCTAssertEqual("#22446688", color.hexString(true))

let hexColor = UIColor(rgba: "#AABBCCDD", defaultColor: UIColor.yellowColor());
let hexColor = UIColor("#AABBCCDD", defaultColor: UIColor.yellow);
XCTAssertEqual("#AABBCC", hexColor.hexString(false))
XCTAssertEqual("#AABBCCDD", hexColor.hexString(true))
}

}

extension UIColor {
private func toUInt8(value: CGFloat) -> UInt8 {
fileprivate func toUInt8(_ value: CGFloat) -> UInt8 {
let multiplier = CGFloat(255)
return UInt8(value * multiplier)
}

private func rgba() -> (red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8) {
fileprivate func rgba() -> (red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</p>

UIColor+Hex, now Swift.
[![Build Status](https://travis-ci.org/yeahdongcn/UIColor-Hex-Swift.svg?branch=Swift-2.3)](https://travis-ci.org/yeahdongcn/UIColor-Hex-Swift)
[![Build Status](https://travis-ci.org/yeahdongcn/UIColor-Hex-Swift.svg?branch=master)](https://travis-ci.org/yeahdongcn/UIColor-Hex-Swift)
=================
Convenience method for creating autoreleased color using RGBA hex string.

Expand Down

0 comments on commit c64695a

Please sign in to comment.