iosswiftswift3uicolorcgcolor

Wrong color when convert UIColor to CGColor


I don't get the correct color when i convert a UIColor to a CGColor.

This color :

 let color = UIColor(red: 198, green: 35.0, blue: 80.0, alpha: 1.0)

enter image description here

When i apply it on a CAlayer

layer.fillColor = UIColor.red.cgColor
layer.strokeColor = color.cgColor

will appear white on my phone !

enter image description here

As you can see the circle is white...


Solution

  • Colors in UIKit are specified using value is between 0 and 1 in float not int from 0 to 255, so you need to divide all your RGB values by 255.0.

    let color = UIColor(red: 198.0/255.0, green: 35.0/255.0, blue: 80.0/255.0, alpha: 1.0)