Pippo
Pippo

Reputation: 1473

Convert hex color as string to Uint

Ive been searching this for ages and cant find a solution that works or that i understand.

I have a string in database "0x6c8c93" that i want to convert into a Uint so i can convert it to a colour.

Below is the function I've been using to convert Uint to colour previously. and I've just been passing it the hex value from colour charts in this format 0x6c8c93. However now i need to pull some values from the DB so I've got to go with string.

class func UIColorFromHEX(hexValue: UInt) -> UIColor {
    return UIColor(
        red: CGFloat((hexValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((hexValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(hexValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}

Thanks for help.

Upvotes: 1

Views: 3909

Answers (3)

vacawama
vacawama

Reputation: 154513

Here is one way to do it. Use string.suffix(6) to keep the last 6 characters of the string, and then use the UInt initializer UInt(_:radix:) to convert the hexString to a UInt?. If the conversion succeeds it will return an Optional UInt that is then unwrapped by if let and assigned to hexValue:

let string = "0x6c8c93"

if let hexValue = UInt(string.suffix(6), radix: 16) {
    // create the color from hexValue
}

Upvotes: 8

Santiago Yeomans
Santiago Yeomans

Reputation: 1

Update to SwiftUI and Swift5

let string = "0x6c8c93"

let hexValue = UInt32(String(string.suffix(6)), radix: 16)
//This will return the UInt32 ready to be converted to a color

This is based on @vacawama 's answer

Upvotes: 0

user7014451
user7014451

Reputation:

With credit to the second answer here, try this:

public func UIColorFromRGB(_ rgbValue: String) -> UIColor {
    return UIColor(
        red: CGFloat((rgbValue.hexaToInt & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue.hexaToInt & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue.hexaToInt & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}

extension String {
    var hexaToInt      : Int    { return Int(strtoul(self, nil, 16))      }
    var hexaToDouble   : Double { return Double(strtoul(self, nil, 16))   }
    var hexaToBinary   : String { return String(hexaToInt, radix: 2)      }
    var decimalToHexa  : String { return String(Int(self) ?? 0, radix: 16)}
    var decimalToBinary: String { return String(Int(self) ?? 0, radix: 2) }
    var binaryToInt    : Int    { return Int(strtoul(self, nil, 2))       }
    var binaryToDouble : Double { return Double(strtoul(self, nil, 2))   }
    var binaryToHexa   : String { return String(binaryToInt, radix: 16)  }
}

extension Int {
    var binaryString: String { return String(self, radix: 2)  }
    var hexaString  : String { return String(self, radix: 16) }
    var doubleValue : Double { return Double(self) }
}

//"ff".hexaToInt              // "255"
//"ff".hexaToDouble           // "255.0"
//"ff".hexaToBinary           // "11111111"
//"255".intToHexa             // "ff"
//"255".intToBinary           // "11111111"
//"11111111".binaryToInt      // "255"
//"11111111".binaryToDouble   // "255.0"
//"11111111".binaryToHexa     // "ff"
//255.binaryString            // "11111111"
//255.hexaString              // "ff"
//255.doubleValue             // 255.0

It worked for me.

Upvotes: 1

Related Questions