|
I'm trying to pull rgb values from a UIColor (like here: https://www.hackingwithswift.com/example-code/uicolor/how-to-read-the-red-green-blue-and-alpha-color-components-from-a-uicolor) and it's mostly working. But for some colors, I'm getting unexpected results after they're selcted from a ColorPicker. Within an ios app, if I pass UIColor(red: 0.11873, green: 0.296969, blue: 0.386893, alpha: 1) to getRed, I get back the expected rgb values. Within my UI, I use a ColorPicker to select the same color, I then get back a negative value for red. Other colors selected via ColorPicker return expected results. I'm stuck at this point. Are there ways to narrow this problem down more? Am I missing some understanding around how getRed should be working? Are there ways to filter out colors that just won't work this way to prevent the user selecting wrong colors from ColorPicker?
|
|
I can't really look into this much right now since I'm away from my laptop for the next several hours, but I' curious if |
|
|
|
|
|
I can't explain why this happens, but I can suggest you try this instead and see if this will work for your needs:
Thi smight be a question for StackOverflow or even the Apple Developer forums. |
|
Thank you! That works for my test cases, so I'll apply it to the rest of my code. It will also simplify some things downstream, as I don't think I'll have to use UIColor at all. |
|
I must confess to not having a lot of understanding of this kind of thing, but it seems like the problem is that in iOS 10+ the system uses the extended sRGB color space, in which RGB values can fall outside the You can see this by altering the
and examing the output:
You can see that the Again, though, most of this color space stuff is beyond me so I'm not sure how to use this information to help you. |
|
I have also noticed that there are different color spaces, but I'm not sure what to do with that info, either. While updating to your suggested way of pulling the rgb pieces, I have found that using Color(hue: 200, saturation: 70, brightness: 39) for the initial setting for color returns values way outside the range of 0 - 1. I need to fiddle with it some more. Maybe someone who has a deeper understanding of the color spaces will chime in. |
|
Your solution is giving me more consistent behavior. For some of my tests, I create the Color using the sRGB color space. But I'm getting consistently usable values for any numbers I pick using the ColorPicker - and that was the important part. I think this is going to work for me. Thank you for your help! |
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.