I’m trying to create a gradient using the same color with a brightness modifier at one end. I tried this:
let gradient = LinearGradient(
colors: [Color.blue, Color.blue.brightness(0.1)],
startPoint: .top, endPoint: .bottom)
Unfortunately this does not compile with the following error: Cannot convert value of type 'some View' to expected element type 'Array<Color>.ArrayLiteralElement' (aka 'Color')
According to this thread I would have expected it to work. How can I fix this?
2
Answers
The
brightness
modifier creates a view, not a color. A possible solution is to use Color with HSB initialiser, like belowYou cannot use
brightness(_:)
modifier here. It returnsView
instead ofColor
.If you want to add brightness to Color, you may need help from UIKit and need to implement as below.
Then you can use it like this.
You can also use this initializer
Color(hue:saturation:brightness:opacity:)
for your custom color.