Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Introduction to Auto Layout in iOS Visual Format Language Views Dictionary and Format Strings

Gavin Hobbs
Gavin Hobbs
5,205 Points

Why the double in the RGBA color?

Hi there! Quick question. Why do we write the RGBA color denominator as a double?

orangeView.backgroundColor = UIColor(displayP3Red: 255/255.0, green: 148/255.0, blue: 0/255.0, alpha: 1.0)

Why is the 255.0 have the .0? Why doesn't the first number have a .0 on the end of it also? (148.0/255.0)

Thanks!

-Gavin

1 Answer

Copied from https://www.codingexplorer.com/create-uicolor-swift/

Each of the ones that take a CGFloat take a value between 0.0 and 1.0, referring to either the complete absence of, or the maximum amount of that color component respectively. So, this means that even if you have pure RGB values in decimal or hexadecimal format, you will have to divide them by decimal 255 to get the amount to input here.

orangeView.backgroundColor = UIColor(displayP3Red: 255/255.0, green: 148/255.0, blue: 0/255.0, alpha: 1.0) can be seen as: orangeView.backgroundColor = UIColor(displayP3Red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)