Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential bug with UIColor.hexRGB() #37

Open
markpokornycos opened this issue Dec 1, 2020 · 0 comments
Open

Potential bug with UIColor.hexRGB() #37

markpokornycos opened this issue Dec 1, 2020 · 0 comments

Comments

@markpokornycos
Copy link

The subject method has this line:

    assert(getRed(&r, green: &g, blue: &b, alpha: &a), "Unable to get RGB channels from UIColor")

If the build settings do not enable testing, the compiler (Xcode 12.1) appears to optimize the code by removing this line, and the result is every color gets the string "000000"

Recommend splitting this line into 2, one to call getRed() and another to assert on the result of the previous call. Here is an alternate computed var implementation that might replace the existing method:

    var hexString: String {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
        let success = getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        assert(success, "Unable to get RGB channels from UIColor")
        return String(format: "%02X%02X%02X", Int(red * 255), Int(green * 255), Int(blue * 255))
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant