1

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.

Pippo
  • 1,439
  • 1
  • 18
  • 35
  • Not an exact duplicate because your string format is slightly different, but there are many great ideas here: http://stackoverflow.com/q/1560081/1630618 – vacawama Jan 25 '17 at 04:53

3 Answers3

8

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
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
0

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.

Community
  • 1
  • 1
  • there is inconvinience with naming in the original answer: `decimalToHexa` -> `intToHexa' and `decimalToBinary` -> `intToBinary` – muescha Jan 25 '17 at 05:11
  • I'm not sure what you are saying, but: (1) the OP accepted an answer - which is all I care about - (2) the OP suggested the hex values are stored as a string and not Uint - I use the exact same function as the OP uses - so (3) for now I'll keep answer out there. I credited where I found the code I used, it includes many more conversions if needed, and I've always respected @vacawama's comments and answers. I think I'm acting appropriately. But if not, I'll gladly delete this. (After I make sure I store it for *my* future reference!) Peace. –  Jan 25 '17 at 05:20
  • ? you answer is fine. i only suggested that there is a naming inconsitency beetween `hexaToInt` but the reverse name is `decimalToHexa` and should be better `intToHexa`. just noting more complaints – muescha Jan 25 '17 at 05:37
0

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