DEV Community

divyesh vekariya
divyesh vekariya

Posted on

1 2 1 1 1

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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

๐Ÿ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay