In Objective-C, we use this code to set RGB color codes for views:
#define UIColorFromRGB(rgbValue)
[UIColor colorWithRed:((float)((rgbValue & 0x
I can see its already been answered but still hope one liner will help in better way.
import UIKit
let requiredColor = UIColor(red: CGFloat((rgbValue & 0xFF0000) >> 16)/255,
green: CGFloat((rgbValue & 0x00FF00) >> 8)/255,
blue: CGFloat(rgbValue & 0x0000FF)/255, alpha :1)
Updated: :Changes done as per example explained by Author of question to provide hex values
Nate Cook's answer is absolutely correct. Just for greater flexibility, I keep the following functions in my pack:
func getUIColorFromRGBThreeIntegers(red: Int, green: Int, blue: Int) -> UIColor {
return UIColor(red: CGFloat(Float(red) / 255.0),
green: CGFloat(Float(green) / 255.0),
blue: CGFloat(Float(blue) / 255.0),
alpha: CGFloat(1.0))
}
func getUIColorFromRGBHexValue(value: Int) -> UIColor {
return getUIColorFromRGBThreeIntegers(red: (value & 0xFF0000) >> 16,
green: (value & 0x00FF00) >> 8,
blue: value & 0x0000FF)
}
func getUIColorFromRGBString(value: String) -> UIColor {
let str = value.lowercased().replacingOccurrences(of: "#", with: "").
replacingOccurrences(of: "0x", with: "");
return getUIColorFromRGBHexValue(value: Int(str, radix: 16)!);
}
And this is how I use them:
// All three of them are identical:
let myColor1 = getUIColorFromRGBHexValue(value: 0xd5a637)
let myColor2 = getUIColorFromRGBString(value: "#D5A637")
let myColor3 = getUIColorFromRGBThreeIntegers(red: 213, green: 166, blue: 55)
Hope this will help. Everything is tested with Swift 3/Xcode 8.