how can i obtain a cgcolor from RGB values?

alionthego picture alionthego · Apr 6, 2015 · Viewed 40.8k times · Source

I'm trying to create a custom RGB CGColor using swift to use as a border color for a UIButton. I've tried following code but the color is not visible:

var red = UIColor(red: 100.0, green: 130.0, blue: 230.0, alpha: 1.0)
self.layer.borderColor = red.CGColor

Is there any way to create a CGColor directly from RGB values?

Answer

Shruti picture Shruti · Apr 6, 2015

You have to give the values between 0 and 1.0. So divide the RGB values by 255.

Change Your code to

var red = UIColor(red: 100.0/255.0, green: 130.0/255.0, blue: 230.0/255.0, alpha: 1.0)
self.layer.borderColor = red.CGColor