How can I use UIColorFromRGB in Swift?
Here's a Swift version of that function (for getting a UIColor representation of a UInt
value):
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
view.backgroundColor = UIColorFromRGB(0x209624)
How to initialize UIColor from RGB values properly?
The values are in the 0.0
to 1.0
range.
E.g. divide by 255.
, but remember the decimal dot so you get floating point division and not integer division.
Like
selectedColor = [UIColor colorWithRed:14.0/255.0 green:114.0/255.0 blue:199.0/255.0 alpha:1];
How to use hex color values
#ffffff
are actually 3 color components in hexadecimal notation - red ff
, green ff
and blue ff
. You can write hexadecimal notation in Swift using 0x
prefix, e.g 0xFF
To simplify the conversion, let's create an initializer that takes integer (0 - 255) values:
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
convenience init(rgb: Int) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF
)
}
}
Usage:
let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF)
let color2 = UIColor(rgb: 0xFFFFFF)
How to get alpha?
Depending on your use case, you can simply use the native UIColor.withAlphaComponent
method, e.g.
let semitransparentBlack = UIColor(rgb: 0x000000).withAlphaComponent(0.5)
Or you can add an additional (optional) parameter to the above methods:
convenience init(red: Int, green: Int, blue: Int, a: CGFloat = 1.0) {
self.init(
red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: a
)
}
convenience init(rgb: Int, a: CGFloat = 1.0) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF,
a: a
)
}
(we cannot name the parameter alpha
because of a name collision with the existing initializer).
Called as:
let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF, a: 0.5)
let color2 = UIColor(rgb: 0xFFFFFF, a: 0.5)
To get the alpha as an integer 0-255, we can
convenience init(red: Int, green: Int, blue: Int, a: Int = 0xFF) {
self.init(
red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: CGFloat(a) / 255.0
)
}
// let's suppose alpha is the first component (ARGB)
convenience init(argb: Int) {
self.init(
red: (argb >> 16) & 0xFF,
green: (argb >> 8) & 0xFF,
blue: argb & 0xFF,
a: (argb >> 24) & 0xFF
)
}
Called as
let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF, a: 0xFF)
let color2 = UIColor(argb: 0xFFFFFFFF)
Or a combination of the previous methods. There is absolutely no need to use strings.
UIColor not working with RGBA values
RGB values for UIColor are between 0 and 1 (see the documentation "specified as a value from 0.0 to 1.0")
You need to divide your numbers by 255:
passwordTextField.textColor = UIColor(red: CGFloat(202.0/255.0), green: CGFloat(228.0/255.0), blue: CGFloat(230.0/255.0), alpha: CGFloat(1.0))
Another thing, you don't need to create CGFloats:
passwordTextField.textColor = UIColor(red:202.0/255.0, green:228.0/255.0, blue:230.0/255.0, alpha:1.0)
How to define a HEX color macro in swift
func HColorToRGB(rgbValue: Int, alpha: Float) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0xFF00) >> 8) / 255.0,
blue: CGFloat(rgbValue &0xFF) / 255.0,
alpha: CGFloat(alpha)
)
}
yourView.backgroundColor = HColorToRGB("passyourValue","alphaValue")
How to pass value through UIColorFromRGB in Objective-C
You are retrieving a NSString object (coloring) but the macro expects an integer.
To get an integer from a hex string you can use NSScanner class.
Along the lines of:
unsigned int hexValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:coloring];
[scanner setScanLocation:0]; // depends on your exact string format you may have to use location 1
[scanner scanHexInt:&hexValue];
shapeLayer.strokeColor = UIColorFromRGB(hexValue).CGColor;
How to use hex color values
#ffffff
are actually 3 color components in hexadecimal notation - red ff
, green ff
and blue ff
. You can write hexadecimal notation in Swift using 0x
prefix, e.g 0xFF
To simplify the conversion, let's create an initializer that takes integer (0 - 255) values:
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
convenience init(rgb: Int) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF
)
}
}
Usage:
let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF)
let color2 = UIColor(rgb: 0xFFFFFF)
How to get alpha?
Depending on your use case, you can simply use the native UIColor.withAlphaComponent
method, e.g.
let semitransparentBlack = UIColor(rgb: 0x000000).withAlphaComponent(0.5)
Or you can add an additional (optional) parameter to the above methods:
convenience init(red: Int, green: Int, blue: Int, a: CGFloat = 1.0) {
self.init(
red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: a
)
}
convenience init(rgb: Int, a: CGFloat = 1.0) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF,
a: a
)
}
(we cannot name the parameter alpha
because of a name collision with the existing initializer).
Called as:
let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF, a: 0.5)
let color2 = UIColor(rgb: 0xFFFFFF, a: 0.5)
To get the alpha as an integer 0-255, we can
convenience init(red: Int, green: Int, blue: Int, a: Int = 0xFF) {
self.init(
red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: CGFloat(a) / 255.0
)
}
// let's suppose alpha is the first component (ARGB)
convenience init(argb: Int) {
self.init(
red: (argb >> 16) & 0xFF,
green: (argb >> 8) & 0xFF,
blue: argb & 0xFF,
a: (argb >> 24) & 0xFF
)
}
Called as
let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF, a: 0xFF)
let color2 = UIColor(argb: 0xFFFFFFFF)
Or a combination of the previous methods. There is absolutely no need to use strings.
Setting custom RGB color in drawing class in Swift
If you want to set the color try this:
var Color = UIColor(red: 200.0/255.0, green: 16.0/255.0, blue: 46.0/255.0, alpha: 1.0)
also a very useful tool/ website for converting the colors into useful Swift code:
https://www.ralfebert.de/snippets/ios/swift-uicolor-picker/
Edit:
self.button.setTitleColor(UIColor(red:0.96, green:0.28, blue:0.28, alpha:1.0), forState: .Normal)
or if you want a cleaner solution:
var myColor = UIColor(red:0.96, green:0.28, blue:0.28, alpha:1.0)
self.button.setTitleColor(myColor, forState: .Normal)
How do I create a UIColor from RGBA?
Your values are incorrect, you need to divide each color value by 255.0.
[UIColor colorWithRed:66.0f/255.0f
green:79.0f/255.0f
blue:91.0f/255.0f
alpha:1.0f];
The docs state:
+ (UIColor *)colorWithRed:(CGFloat)red
green:(CGFloat)green
blue:(CGFloat)blue
alpha:(CGFloat)alpha
Parameters
red
The red component of the color object, specified as a value from 0.0 to 1.0.
green
The green component of the color object, specified as a value from 0.0 to 1.0.
blue
The blue component of the color object, specified as a value from 0.0 to 1.0.
alpha
The opacity value of the color object, specified as a value from 0.0 to 1.0.
Reference here.
Related Topics
Get Pixel Value from Cvpixelbufferref in Swift
Setting Up Uiscrollview to Swipe Between 3 View Controllers
Ckquery from Private Zone Returns Only First 100 Ckrecords from in Cloudkit
An Elegant Way to Ignore Any Errors Thrown by a Method
Press-And-Hold Button for "Repeat Fire"
Ios13 Share Sheet: How to Set Preview Thumbnail When Sharing Uiimage
Enable/Disable Apple Push Notification from iPhone App
Leak from Nsurl and Avaudioplayer Using Arc
How to Build Boost-Libraries for Iphone
Make Uicollectionview Zoomable
iOS 11 Layout Guidance About Safe Area for iPhone X
What Is Difference Between Urlwithstring and Fileurlwithpath of Nsurl
How to Properly Group a List Fetched from Coredata by Date
Receive Accelerometer Updates in Background Using Coremotion Framework
Swift - Tableview Row Height Updates Only After Scrolling or Toggle Expand/Collapse
Posting Photos to Facebook Fan Page with iOS Sdk
Ui Testing Failure - Neither Element Nor Any Descendant Has Keyboard Focus on Securetextfield