Blog
Wild & Free Tools

How to Convert Hex to RGB in Swift and Create UIColor

Last updated: February 2026 5 min read
Quick Answer

Table of Contents

  1. UIColor Extension for Hex
  2. SwiftUI Color from Hex
  3. Adding Opacity Support
  4. Using Asset Catalog Colors
  5. Frequently Asked Questions

UIColor and SwiftUI's Color type both accept RGB values, but neither accepts a hex string directly out of the box. The standard solution is a UIColor extension that parses the six-character hex string into three integer channels, then divides by 255 to produce the normalized float values Apple's color types require.

The converter above gives you the RGB integers instantly. The code examples below show how to use those values in Swift.

UIColor Extension: Parse Hex Strings in Swift

This extension handles hex codes with and without the # prefix:

extension UIColor {
  convenience init?(hex: String) {
    var hex = hex.trimmingCharacters(in: .whitespacesAndNewlines)
    hex = hex.hasPrefix("#") ? String(hex.dropFirst()) : hex
    guard hex.count == 6 else { return nil }
    var rgb: UInt64 = 0
    Scanner(string: hex).scanHexInt64(&rgb)
    let r = CGFloat((rgb >> 16) & 0xFF) / 255.0
    let g = CGFloat((rgb >>  8) & 0xFF) / 255.0
    let b = CGFloat((rgb      ) & 0xFF) / 255.0
    self.init(red: r, green: g, blue: b, alpha: 1.0)
  }
}

// Usage
let brandColor = UIColor(hex: "#3B82F6")
let labelColor = UIColor(hex: "374151")

The bitwise operations extract each two-character pair: right-shift by 16 gets red, by 8 gets green, and no shift gets blue. Masking with 0xFF isolates the last byte.

SwiftUI: Using Hex Colors with Color()

SwiftUI's Color initializer accepts normalized double values (0.0 to 1.0). Using the RGB integers from the converter:

// #3B82F6 = rgb(59, 130, 246)
let brandColor = Color(red: 59.0/255, green: 130.0/255, blue: 246.0/255)

For a reusable extension on Color that accepts a hex string:

extension Color {
  init(hex: String) {
    var hex = hex.trimmingCharacters(in: .whitespacesAndNewlines).replacingOccurrences(of: "#", with: "")
    var rgb: UInt64 = 0
    Scanner(string: hex).scanHexInt64(&rgb)
    let r = Double((rgb >> 16) & 0xFF) / 255
    let g = Double((rgb >>  8) & 0xFF) / 255
    let b = Double( rgb        & 0xFF) / 255
    self.init(red: r, green: g, blue: b)
  }
}

// Usage in SwiftUI
Text("Hello").foregroundColor(Color(hex: "#3B82F6"))
Sell Custom Apparel — We Handle Printing & Free Shipping

Adding Opacity Support to the Hex Extension

To support eight-character hex codes with an alpha channel, extend the parser to handle the optional last two characters:

extension UIColor {
  convenience init?(hex: String, alpha: CGFloat = 1.0) {
    var hex = hex.trimmingCharacters(in: .whitespacesAndNewlines).replacingOccurrences(of: "#", with: "")
    var rgb: UInt64 = 0
    Scanner(string: hex).scanHexInt64(&rgb)
    let r, g, b, a: CGFloat
    if hex.count == 8 {
      r = CGFloat((rgb >> 24) & 0xFF) / 255
      g = CGFloat((rgb >> 16) & 0xFF) / 255
      b = CGFloat((rgb >>  8) & 0xFF) / 255
      a = CGFloat( rgb        & 0xFF) / 255
    } else {
      r = CGFloat((rgb >> 16) & 0xFF) / 255
      g = CGFloat((rgb >>  8) & 0xFF) / 255
      b = CGFloat( rgb        & 0xFF) / 255
      a = alpha
    }
    self.init(red: r, green: g, blue: b, alpha: a)
  }
}

The Asset Catalog Alternative

For static brand colors that do not need to be set at runtime, Xcode's asset catalog is often the better choice. Add a Color Set in Assets.xcassets, enter your hex values in the color picker, and reference it in code:

let brandColor = UIColor(named: "BrandBlue")
let brandColor = Color("BrandBlue")  // SwiftUI

Asset catalog colors support dark mode variants automatically and do not require any hex parsing code. Use the programmatic extension approach when you need to set colors dynamically (from an API response, user preferences, or a theme system).

Get the RGB Values for Your Hex Code

Paste your hex code above to get the R, G, B integers — divide by 255 for Swift Color().

Open Hex to RGB Converter

Frequently Asked Questions

Why does UIColor not support hex strings natively?

UIColor predates the web convention of hex color codes and was designed around floating-point RGB values. The hex format is primarily a web/CSS convention; Apple's frameworks have never added native hex parsing, leaving it to extensions.

What is scanHexInt64 and is it still available in modern Swift?

scanHexInt64 is a method on Scanner that parses a hexadecimal integer from a string. It is available in all current iOS and macOS SDK versions. It replaced the older scanHexInt32 for 64-bit integer support.

Does this extension handle three-character hex shorthand like #F60?

The extension as written expects exactly six or eight characters. To support three-character shorthand, add an expansion step: if hex.count == 3 { hex = hex.map { String(repeating: String($0), count: 2) }.joined() }.

Can I use hex colors in SwiftUI Color assets?

Yes. In Assets.xcassets, when you add a Color Set, the color picker shows a hex input field. This is the recommended approach for static colors in SwiftUI apps — no code extension needed.

Alicia Grant
Alicia Grant Frontend Engineer

Alicia leads image and PDF tool development at WildandFree, specializing in high-performance client-side browser tools.

More articles by Alicia →
Launch Your Own Clothing Brand — No Inventory, No Risk