Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I obtain a CGColor from RGB values?

I am 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?

like image 609
alionthego Avatar asked Apr 06 '15 06:04

alionthego


People also ask

What is CGColor?

CGColor is the fundamental data type used internally by Core Graphics to represent colors. CGColor objects, and the functions that operate on them, provide a fast and convenient way of managing and setting colors directly, especially colors that are reused (such as black for text).

How do I get RGB values from UIColor Swift?

let swiftColor = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1) println("RGB Value is:"); println(swiftColor.

What do RGB numbers go up to?

Typically, RGB values are encoded as 8-bit integers, which range from 0 to 255.


1 Answers

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 
like image 196
Shruti Avatar answered Oct 22 '22 02:10

Shruti