UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Color Array in distinct color steps: so far a ChatGPT fail

Forums > SwiftUI

I've still (very slowly) been working on color arrays in SwiftUI, @roosterboy was incredibly helfpul here and yet I've been wanting to explore other ways to do it and tried ChatGPT for the first time today.

I stated: "In SwiftUI create an array of 3 rectangles with fills of distinct colors created in equal steps by blending two different parent colors"

And got code that looked promising in it's simplicity but did not work:

let parentColors = [Color.red, Color.blue]

var body: some View {
    VStack {
        ForEach(0..<3, id: \.self) { index in
            Rectangle()
                .fill(LinearGradient(
                    gradient: Gradient(colors: [
                        parentColors[0].opacity(1 - CGFloat(index) / 3),
                        parentColors[1].opacity(1 - CGFloat(index) / 3)
                    ]),
                    startPoint: .leading,
                    endPoint: .trailing
                ))
        }
    }
}

The error was Ambiguous use of 'init(_:)'

I then asked ChatGPT to try to fix for that error and it explained that:

The error message "Ambiguous use of 'init(_:)'" indicates that there is more than one initializer that matches the arguments you have provided. This means that the compiler is unable to determine which initializer you are trying to use.

In the code you provided, it seems that the error is occurring when you are trying to create a LinearGradient with the Gradient initializer. This is because the Gradient initializer has multiple signatures that take different arguments, and the compiler is unable to determine which one you are trying to use based on the arguments you have provided.

To fix this error, you can specify the type of gradient you want to create by using the appropriate initializer for that gradient type. For example, if you want to create a LinearGradient, you can use the initializer that takes a Gradient and two CGPoint values for the start and end points of the gradient:

But from there it just gave the same code. I tried learn about initializing Gradient etc. to no avail. I'm curious if you think ChatGPT is onto something or if SwiftUI is just not set up to create steps of color arrays without getting into something like Extension of Color... like we did in the link I shared at the top.

2      

It is wrong because it should be Double.

As it ChatGPT is also AI, it will learn. Until then.

let parentColors = [Color.red, Color.blue]

var body: some View {
    VStack {
        ForEach(0..<3, id: \.self) { index in
            Rectangle()
                .fill(LinearGradient(
                    gradient: Gradient(colors: [
                        parentColors[0].opacity(1 - Double(index) / 3),
                        parentColors[1].opacity(1 - Double(index) / 3)
                    ]),
                    startPoint: .leading,
                    endPoint: .trailing
                ))
        }
    }
}

2      

Thanks, that solved the error. Unfortunately for me, now that I see what ChatGPT thought I was doing would be satisfied by stepping the opacity. I should have noted that even with the error. Anyway, thanks for clearing this up, thanks from chatGPT.

And now Dear ChatGPT: I'm actually trying to step through hue / saturation / brightness or red / green / blue where each individual child is a solid color.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.