Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swift, SwiftUI – .getRed(&red, green: &green, blue: &blue, alpha: &alpha)

I am following Stanfords’ CS193p Developing Apps for iOS online course.

I’m trying to do the Assignment 6 Memorize.pdf.

It says to use this code below ⬇️, but it gives me always white color. Is there a bug somewhere? Please help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import SwiftUI

struct RGBAColor: Codable, Equatable, Hashable {
    let red: Double
    let green: Double
    let blue: Double
    let alpha: Double
}

extension Color {
    init(rgbaColor rgba: RGBAColor) {
        self.init(.sRGB, red: rgba.red, green: rgba.green, blue: rgba.blue, opacity: rgba.alpha)
    }
}

extension RGBAColor {
    init(color: Color) {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
        if let cgColor = color.cgColor {
            UIColor(cgColor: cgColor).getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        }
        self.init(red: Double(red), green: Double(green), blue: Double(blue), alpha: Double(alpha))
    }
}
import SwiftUI

struct ContentView: View {
    static let rgbaColorRed = RGBAColor(color: Color.red)
    static let thisShouldBeRed = Color(rgbaColor: rgbaColorRed)
    
    var body: some View {
        Text("Hello, world!")
            .padding()
            .foregroundColor(ContentView.thisShouldBeRed) // why white here, not red???
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

>Solution :

As the cgColor property’s documentation says:

For a dynamic color, like one you load from an Asset Catalog using init(_:bundle:), or one you create from a dynamic UIKit or AppKit color, this property is nil.

Color.red is actually a dynamic color:

A context-dependent red color suitable for use in UI elements.

So Color.red.cgColor is nil, causing the getRed(_:green:blue:alpha:) call to not run, and the red, green, blue, alpha local variables remain 0. So the color you got is not "white", but "transparent".

You can actually create a UIColor directly from Color, without going through CGColor:

var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0

// note here:
UIColor(color).getRed(&red, green: &green, blue: &blue, alpha: &alpha)
self.init(red: Double(red), green: Double(green), blue: Double(blue), alpha: Double(alpha))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading