You can easily register and use Dynamic Colors used to counteract Dark Mode as codes.
-
UIKit-UIColor & SwiftUI-Color both compatible.
-
DYColor contains basic, yet frequently needed, color extensions.
https://github.com/bangtori/DYColor.git
- iOS 14.0
- Swift 5
We provide Color extensions that not only help you implement DYColor, but also help with custom development.
1. hex code init Color
Color(hex:"#FFFFFF")
UIColor(hex:"#FFFFFF")You can easily create colors with the familiar HEX color code.
2. Adjusting brightness & saturation
// Instance Method
func adjusted(brightness: Double, saturation: Double) -> Color
func adjusted(brightness: Double, saturation: Double) -> UIColorYou can adjust the brightness and saturation of the current color.
let swiftUIColor = DYColor(lightColor: Color.black, darkColor: Color.red)
let uiKitColor = DYUIColor(lightColor: UIColor.black, darkColor: UIColor.red)
let swiftUIColor2 = DYColor(lightColor: Color.red)
let uiKitColor2 = DYUIColor(lightColor: UI.red)- As a basic initialization method, you can create a DYColor by entering a light mode color value and a dark mode color value.
- UIKit uses the DYUIColor model, and SwiftUI uses the DYColor model.
- For Initialization that enter only light mode, it automatically assigns a color with 70 percent saturation of the existing color as the dark mode color.
swiftUIColor.dynamicColor
uiKitColor.dynamicColorThe dynamicColor property returns the color value set for the screen mode.
❗️ In SwiftUI, you need to declare a colorScheme at the top of the view you use.
@Environment(\.colorScheme) var scheme// Setting colorsets with extensions
extension DYColor {
static let color1 = DYColor(lightColor: .black, darkColor: .red) // basic initialization
static let color2 = DYColor(lightColor: .blue) // lightColor only initialization
}
struct ContentView: View {
@Environment(\.colorScheme) var scheme // scheme Declarations
var body: some View {
VStack {
Text("Light Black, Dark Red")
.foregroundStyle(DYColor.color1.dynamicColor) // use color1 dynamicColor (DYColor)
.padding()
VStack{
Text("This is DYColor Blue")
Text("Dark - 70%saturation")
}
.font(.title)
.foregroundStyle(DYColor.color2.dynamicColor) // use color2 dynamicColor (DYColor)
.padding()
Text("This is normal Blue")
.font(.title)
.foregroundStyle(Color.blue)
// use basic Color (SwiftUI - Color)
// color2 VS basic color saturation in dark mode
}
.padding()
}
}DYColor is available under the MIT license. See the LICENSE file for more info.
