I have an object with a UIColor property:
class Beer: NSObject {
var color: UIColor?
...
}
I'm saving this to a DB, so I need to make this property into a valid JSON type, so I'm thinking of converting it to a string. How can I convert into a string to store, and then when loading using that string to create the UIColor?
To convert an Int value to a String value in Swift, use String(). String() accepts integer as argument and returns a String value created using the given integer value.
Convert Swift String to DoubleUse Double , Float , CGFloat to convert floating-point values (real numbers). let lessPrecisePI = Float("3.14") let morePrecisePI = Double("3.1415926536") let width = CGFloat(Double("200.0")!)
In Objective-C, we use this code to set RGB color codes for views: #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] view.
I have put some sample for both conversion, still you can find many code for the conversion
For the conversion from UIColor to hex string you can use the following code:
extension UIColor {
var rgbComponents:(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var r:CGFloat = 0
var g:CGFloat = 0
var b:CGFloat = 0
var a:CGFloat = 0
if getRed(&r, green: &g, blue: &b, alpha: &a) {
return (r,g,b,a)
}
return (0,0,0,0)
}
// hue, saturation, brightness and alpha components from UIColor**
var hsbComponents:(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) {
var hue:CGFloat = 0
var saturation:CGFloat = 0
var brightness:CGFloat = 0
var alpha:CGFloat = 0
if getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha){
return (hue,saturation,brightness,alpha)
}
return (0,0,0,0)
}
var htmlRGBColor:String {
return String(format: "#%02x%02x%02x", Int(rgbComponents.red * 255), Int(rgbComponents.green * 255),Int(rgbComponents.blue * 255))
}
var htmlRGBaColor:String {
return String(format: "#%02x%02x%02x%02x", Int(rgbComponents.red * 255), Int(rgbComponents.green * 255),Int(rgbComponents.blue * 255),Int(rgbComponents.alpha * 255) )
}
}
Sample use:
let myColorBlack = UIColor.blackColor().webColor //#000000ff
let myLghtGrayColor = UIColor.lightGrayColor().webColor //#aaaaaaff
let myDarkGrayColor = UIColor.darkGrayColor().webColor
For more info you can check: https://stackoverflow.com/a/28697136/4557505
https://gist.github.com/yannickl/16f0ed38f0698d9a8ae7
You can store this string inside the db and retrieve it when you need it
From HexString to UIColor
extension UIColor {
public convenience init?(hexString: String) {
let r, g, b, a: CGFloat
if hexString.hasPrefix("#") {
let start = hexString.startIndex.advancedBy(1)
let hexColor = hexString.substringFromIndex(start)
if hexColor.characters.count == 8 {
let scanner = NSScanner(string: hexColor)
var hexNumber: UInt64 = 0
if scanner.scanHexLongLong(&hexNumber) {
r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
a = CGFloat(hexNumber & 0x000000ff) / 255
self.init(red: r, green: g, blue: b, alpha: a)
return
}
}
}
return nil
}
}
Usage:UIColor(hexString: "#ffe700ff")
Ref: https://www.hackingwithswift.com/example-code/uicolor/how-to-convert-a-hex-color-to-a-uicolor
https://github.com/yeahdongcn/UIColor-Hex-Swift
https://gist.github.com/arshad/de147c42d7b3063ef7bc
Here is an update for:
__
import UIKit
import SwiftUI
extension Color {
init?(hexString: String) {
let rgbaData = getrgbaData(hexString: hexString)
if(rgbaData != nil){
self.init(
.sRGB,
red: Double(rgbaData!.r),
green: Double(rgbaData!.g),
blue: Double(rgbaData!.b),
opacity: Double(rgbaData!.a)
)
return
}
return nil
}
}
extension UIColor {
public convenience init?(hexString: String) {
let rgbaData = getrgbaData(hexString: hexString)
if(rgbaData != nil){
self.init(
red: rgbaData!.r,
green: rgbaData!.g,
blue: rgbaData!.b,
alpha: rgbaData!.a)
return
}
return nil
}
}
private func getrgbaData(hexString: String) -> (r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat)? {
var rgbaData : (r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat)? = nil
if hexString.hasPrefix("#") {
let start = hexString.index(hexString.startIndex, offsetBy: 1)
let hexColor = String(hexString[start...]) // Swift 4
let scanner = Scanner(string: hexColor)
var hexNumber: UInt64 = 0
if scanner.scanHexInt64(&hexNumber) {
rgbaData = { // start of a closure expression that returns a Vehicle
switch hexColor.count {
case 8:
return ( r: CGFloat((hexNumber & 0xff000000) >> 24) / 255,
g: CGFloat((hexNumber & 0x00ff0000) >> 16) / 255,
b: CGFloat((hexNumber & 0x0000ff00) >> 8) / 255,
a: CGFloat( hexNumber & 0x000000ff) / 255
)
case 6:
return ( r: CGFloat((hexNumber & 0xff0000) >> 16) / 255,
g: CGFloat((hexNumber & 0x00ff00) >> 8) / 255,
b: CGFloat((hexNumber & 0x0000ff)) / 255,
a: 1.0
)
default:
return nil
}
}()
}
}
return rgbaData
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With