Skip to content

Commit

Permalink
Add dynamic color shorthand.
Browse files Browse the repository at this point in the history
  • Loading branch information
lipka committed Jul 5, 2022
1 parent da5b28f commit 1549a35
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -49,4 +49,14 @@ import Button
let button = Button()
button.setBackgroundColor(.blue, for: .normal)
button.setBackgroundColor(.blue.adjust(0.1), for: .highlighted)
```
```

### 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)
```
14 changes: 14 additions & 0 deletions Sources/Color/Color+Dynamic.swift
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())
))
}
}
22 changes: 22 additions & 0 deletions Sources/Color/UIColor+Dynamic.swift
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()
}
}
}
}
28 changes: 28 additions & 0 deletions Tests/ColorTests/DynamicColorTests.swift
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)
}
}

0 comments on commit 1549a35

Please sign in to comment.