DEV Community

divyesh vekariya
divyesh vekariya

Posted on

Adjust the intensity/brightness of colors in SwiftUI views

SwiftUI makes it incredibly easy to tweak colors with the brightness(_:) modifier. You can lighten or darken any color dynamically by passing a Double value:

0 retains the original color.
1 makes the color fully white.
Negative values darken the color toward black.
Here are two examples:

Lighten Colors

ForEach(0..<8) { num in
Color.blue
.brightness(Double(num) * 0.1)
}

Image description

This creates a gradient where blue transitions to almost white.


Darken Colors

ForEach(0..<8) { num in
    Color.blue
        .brightness(Double(num) * -0.1)
}
Enter fullscreen mode Exit fullscreen mode

Image description


This generates a gradient where blue transitions to almost black.

Want to bring SwiftUI into your UIKit apps or master advanced Swift programming?
📘 Check out Integrating SwiftUI into UIKit Apps for seamless adoption.
📘 Explore Swift Gems for expert tips and techniques.

SwiftUI #Swift #iOSDev #ColorDesign #ProgrammingTips

Top comments (0)