Updating the test cases

This commit is contained in:
Yannick Loriot 2018-08-21 12:52:07 +02:00
parent b5750c541c
commit 8b3e06c00f
3 changed files with 30 additions and 5 deletions

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

View File

@ -80,11 +80,12 @@ public extension DynamicColor {
*/
public convenience init(hex: UInt32, useAlpha alphaChannel: Bool = false) {
let mask = 0xFF
let cappedHex = !alphaChannel && hex > 0xffffff ? 0xffffff : hex
let r = Int(hex >> (alphaChannel ? 24 : 16)) & mask
let g = Int(hex >> (alphaChannel ? 16 : 8)) & mask
let b = Int(hex >> (alphaChannel ? 8 : 0)) & mask
let a = alphaChannel ? Int(hex) & mask : 255
let r = Int(cappedHex >> (alphaChannel ? 24 : 16)) & mask
let g = Int(cappedHex >> (alphaChannel ? 16 : 8)) & mask
let b = Int(cappedHex >> (alphaChannel ? 8 : 0)) & mask
let a = alphaChannel ? Int(cappedHex) & mask : 255
let red = CGFloat(r) / 255
let green = CGFloat(g) / 255

View File

@ -81,6 +81,13 @@ class DynamicColorTests: XCTestCase {
XCTAssert(custom.toHex() == 0x769a2b, "Color string should be equal to 0x769a2b")
}
func testToHexWithAlpha() {
let custom = DynamicColor(hex: 0x769a2b80, useAlpha: true)
XCTAssert(custom.toHex() == 0x769a2b, "Color string should be equal to 0x769a2b")
XCTAssertEqual(custom.alphaComponent, 0.5, accuracy: 0.01, "Alpha component should be equal to 0.5 (not \(custom.alphaComponent))")
}
func testHexPrecision() {
let allHexes: CountableRange<UInt32> = 0 ..< 0xFFFFFF
let impreciseConversions = allHexes.filter { hex in
@ -90,6 +97,15 @@ class DynamicColorTests: XCTestCase {
XCTAssert(impreciseConversions.count == 0, "Imprecise hex convertions on \(impreciseConversions.count > 50 ? " more than 50 entries (\(impreciseConversions.count) entries exactly)" : ": \(impreciseConversions)")")
}
func testOverflowedColor() {
let positiveColor = DynamicColor(hex: 0xffffffff)
let alphaColor = DynamicColor(hex: 0xffffffff)
XCTAssert(positiveColor.toHex() == 0xffffff, "Color string should be equal to 0xffffff")
XCTAssert(alphaColor.toHex() == 0xffffff, "Color string should be equal to 0xffffff")
XCTAssertEqual(alphaColor.alphaComponent, 1, accuracy: 0.01, "Alpha component should be equal to 0.5 (not \(alphaColor.alphaComponent))")
}
func testIsEqualToHexString() {
let red = DynamicColor.red
let blue = DynamicColor.blue