-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
76 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import SwiftUI | ||
|
||
@available(iOS 14.0, *) | ||
extension Color { | ||
init( | ||
light lightColor: @escaping @autoclosure () -> Color, | ||
dark darkColor: @escaping @autoclosure () -> Color | ||
) { | ||
self.init(UIColor( | ||
light: UIColor(lightColor()), | ||
dark: UIColor(darkColor()) | ||
)) | ||
} | ||
} |
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,22 @@ | ||
import UIKit | ||
|
||
@available(iOS 13.0, *) | ||
extension UIColor { | ||
convenience init( | ||
light lightColor: @escaping @autoclosure () -> UIColor, | ||
dark darkColor: @escaping @autoclosure () -> UIColor | ||
) { | ||
self.init { traitCollection in | ||
switch traitCollection.userInterfaceStyle { | ||
case .light: | ||
return lightColor() | ||
case .dark: | ||
return darkColor() | ||
case .unspecified: | ||
return lightColor() | ||
@unknown default: | ||
return lightColor() | ||
} | ||
} | ||
} | ||
} |
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,28 @@ | ||
import XCTest | ||
import SwiftUI | ||
import UIKit | ||
@testable import Color | ||
|
||
final class DynamicTests: XCTestCase { | ||
@available(iOS 14.0, *) | ||
func testDynamicColor() { | ||
let c = Color(light: Color.black, dark: Color.white) | ||
let rc = UIColor(c).resolvedColor(with: UITraitCollection(userInterfaceStyle: .dark)) | ||
|
||
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, o: CGFloat = 0 | ||
rc.getRed(&r, green: &g, blue: &b, alpha: &o) | ||
|
||
XCTAssertEqual(r, 0) | ||
XCTAssertEqual(g, 0) | ||
XCTAssertEqual(b, 0) | ||
XCTAssertEqual(o, 1) | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
func testDynamicUIColor() { | ||
let c = UIColor(light: UIColor.black, dark: UIColor.white) | ||
let rc = c.resolvedColor(with: UITraitCollection(userInterfaceStyle: .dark)) | ||
|
||
XCTAssertEqual(rc, UIColor.white) | ||
} | ||
} |