TIL two instances of `UIColor` can't be directly compared
I spent half a day explaining to a colleague why callbacks aren’t always better than delegates. To clarify the concept, I created this gist to summarize the discussion and key differences.
Another interesting discovery: two instances of UIColor
can’t be directly compared. This is because color space needs to be considered.
CGColorEqualToColor clearly says:
Two colors are equal if they share the same color space and numerically equal color components.
let blackColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
blackColor.isEqual(UIColor.black) // false
This behavior highlights that simple equality checks don’t work as expected.
Another thing I have learned that func getRed(_ red:, green:, blue:, alpha:) -> Bool
may not return color components if color isn’t compatible with RGB.
It might be a good time to revise the assumption that color components are always in the 0…1 range:
On applications linked for iOS 10 or later, the red/green/blue component is specified in an extended range sRGB color space and can have any value. Values between 0.0 and 1.0 are inside the sRGB color gamut. On earlier versions of iOS, the specified value is always between 0.0 and 1.0.