diff --git a/README.md b/README.md index ab98a91..a1c079d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Build Status](https://github.com/lipka/Color/workflows/Tests/badge.svg)](https://github.com/lipka/Color/actions) ![Swift Version](https://img.shields.io/badge/swift-5.2.0-orange.svg) -Simple extensions for working with `Color` (Swift UI) and `UIColor` (UIKit). +Simple extensions for working with `Color` (SwiftUI) and `UIColor` (UIKit). ## Installation @@ -49,4 +49,14 @@ import Button let button = Button() button.setBackgroundColor(.blue, for: .normal) button.setBackgroundColor(.blue.adjust(0.1), for: .highlighted) -``` \ No newline at end of file +``` + +### Dynamic Colors + +Shorthand notation for creating dynamic colors (light/dark mode). + +```swift +Color(light: Color.white, dark: Color.black) + +UIColor(light: UIColor.white, dark: UIColor.black) +``` diff --git a/Sources/Color/Color+Dynamic.swift b/Sources/Color/Color+Dynamic.swift new file mode 100644 index 0000000..c30784b --- /dev/null +++ b/Sources/Color/Color+Dynamic.swift @@ -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()) + )) + } +} diff --git a/Sources/Color/UIColor+Dynamic.swift b/Sources/Color/UIColor+Dynamic.swift new file mode 100644 index 0000000..3d633f3 --- /dev/null +++ b/Sources/Color/UIColor+Dynamic.swift @@ -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() + } + } + } +} diff --git a/Tests/ColorTests/DynamicColorTests.swift b/Tests/ColorTests/DynamicColorTests.swift new file mode 100644 index 0000000..08d815f --- /dev/null +++ b/Tests/ColorTests/DynamicColorTests.swift @@ -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) + } +}