Hex to UIColor Converter
Hex to UIColor Converter. Enter your Hex Color and the Hex to UIColor Converter instantly returns the matching Swift UIColor Output. Reverse-engineers examining a binary file's embedded timestamp can use the hex to date converter to convert it into a human-readable date.
You have a brand shade from a designer's style sheet and an iOS screen waiting for it, so you need the right color in code, not a guess. The Hex to UIColor Converter reads a hex color and hands back the matching UIColor initializer for Swift, Objective-C or Xamarin, which saves iOS developers from splitting digits by hand and keeps every channel exact. This guide explains how the conversion works, walks through a full example, and gives you a Swift extension you can paste into your own project.
How the Hex to UIColor Converter Turns a Hex Color Code into Swift
A hex color code is a compact way to write three or four color channels in hexadecimal, a base-16 number system. UIColor, Apple's color class, wants the same information as floating-point values. The converter bridges that gap in three steps: it splits the digits into channel pairs, turns each pair into a decimal, and divides by 255 so every channel lands between 0 and 1. The result is a UIColor syntax string that is ready to use in your code. The fastest way to find a plausible Pantone name for a design color is to paste its hex value into the hex to pantone converter.
Reading #RRGGBB and #RRGGBBAA in Base-16
A six-digit #RRGGBB value holds the red, green and blue channels as two digits each. Each digit runs from 0 to F, so a pair covers 0 to 255. An 8-digit #RRGGBBAA value adds a fourth pair for opacity. The three-digit shorthand such as #F80 doubles each digit, so it expands to #FF8800. Knowing which form you hold matters, because an 8-digit string with the opacity pair first will be read differently by different tools.
Why RGBA Components Run from 0 to 1
UIColor stores its RGBA components as CGFloat fractions rather than whole numbers, and it does that inside a device color space that the system manages for you. That is why 255 never appears in a UIColor call: full red is 1.0 and no red is 0.0. The alpha channel follows the same rule, so 1.0 is fully opaque and 0.0 is invisible.
Worked Example: Convert Hex to UIColor for #E8590CCC
Take a warm orange with some transparency for an overlay: #E8590CCC. Split it into pairs, convert each pair to decimal, then divide by 255. Enter your red, green, blue, and alpha values into the rgba to hex converter and it returns the matching hex output with transparency preserved.
$$component = \frac{\text{channel value}}{255}$$| Channel | Hex pair | Decimal | UIColor value |
|---|---|---|---|
| Red | E8 | 232 | 0.9098 |
| Green | 59 | 89 | 0.3490 |
| Blue | 0C | 12 | 0.0471 |
| Alpha | CC | 204 | 0.8000 |
So the answer is a color that is about 91% red, 35% green and 5% blue at 80% opacity. Paste the hex into the converter and you get the same four numbers without any arithmetic.
Swift, Objective-C and Xamarin Output
- Swift:
UIColor(red: 0.9098, green: 0.3490, blue: 0.0471, alpha: 0.8) - Objective-C:
[UIColor colorWithRed:0.9098 green:0.3490 blue:0.0471 alpha:0.8] - Xamarin (C#):
Color.FromRgba(232, 89, 12, 204)uses whole numbers, so the converter keeps the 0 to 255 values there.
Each line is a ready-to-use code snippet. Copy the one that matches your language and drop it into your view controller.
Hex String Parsing in a Swift Extension
If you would rather convert at runtime, for example when colors arrive from a server, add a Swift extension that does the parsing for you. This one accepts a leading #, the three-digit shorthand, and six or eight digits, and it returns nil for anything else.
extension UIColor {
convenience init?(hexColor: String) {
var digits = hexColor.trimmingCharacters(in: .whitespaces)
if digits.hasPrefix("#") { digits.removeFirst() }
if digits.count == 3 { digits = digits.map { "\($0)\($0)" }.joined() }
if digits.count == 6 { digits += "FF" }
guard digits.count == 8 else { return nil }
var packed: UInt64 = 0
guard Scanner(string: digits).scanHexInt64(&packed) else { return nil }
let r = CGFloat((packed >> 24) & 0xFF) / 255
let g = CGFloat((packed >> 16) & 0xFF) / 255
let b = CGFloat((packed >> 8) & 0xFF) / 255
let a = CGFloat(packed & 0xFF) / 255
self.init(red: r, green: g, blue: b, alpha: a)
}
}Handling the #RGB Shorthand and Bad Input
The method is a failable initializer, declared as a convenience init so it can delegate to the standard one. The Scanner reads the digits as a single number, and the shifts pull out one byte per channel. A string that is not valid hexadecimal, or has the wrong length, makes the scan fail and returns nil, so your app never draws a broken shade. Call it like this: let overlay = UIColor(hexColor: "#E8590CCC").
Converting a Brand Teal Hex Code into a UIColor Before It Ships
Lena is wiring a caption label in a SwiftUI settings screen, and the style guide lists the accent as #1B7F79. Before touching Xcode she pastes it into the hex converter, since the label sits on a white card and she wants to know the color survives the trip into code.
The tool splits the pairs into 1B, 7F and 79, which are 27, 127 and 121 in decimal, and returns UIColor(red: 0.1059, green: 0.4980, blue: 0.4745, alpha: 1.0). She pastes that into the view and the teal matches the mockup. Because the conversion gives her the exact shade the app will draw, she can test it, and the guide's own rule is that small captions must clear WCAG AAA at 7:1, while the lower AA threshold is 4.5:1. Against white, #1B7F79 measures 4.82:1: it passes AA but misses AAA by a wide margin.
That number tells her what to do next. She darkens the hue to #17625D, runs the hex through the converter again and gets UIColor(red: 0.0902, green: 0.3843, blue: 0.3647, alpha: 1.0). The contrast against white is now 7.14:1, which clears the 7:1 bar. She sends the new hex to the designer for sign-off and keeps the original teal for large headings, where the 3:1 large-text rule applies and 4.82:1 is comfortably enough.
Hex Color Values in UIKit and SwiftUI
The same hex string works across Apple's frameworks. In UIKit you assign the result to backgroundColor or tintColor. In SwiftUI, wrap it with Color(uiColor:) and the converted value drops straight into a view. Colors set through Interface Builder or an asset catalog use the same RGBA components, so a value you convert here will match what Xcode shows in its color panel. For a native iOS app, that consistency is what keeps the interface looking the same on every screen.
UIColor to Hex: Reversing the Conversion
The opposite direction, UIColor to hex, multiplies each component by 255, rounds it, and writes the result as two hexadecimal digits. Use a hex converter for this when you need to hand a color from code back to a designer or paste it into CSS. Alpha is the awkward part: CSS and Swift disagree on whether it comes first or last, so always check the position before you copy an 8-digit value. Rounding to the nearest whole number is also where small differences creep in, so convert once and reuse the string.
Convert a Hex Color to a UIColor Without Mistakes
Most errors come from the input, not the math. When you convert a hex color to a UIColor, check three things before you trust the output. First, confirm the hexadecimal color really has 3, 6 or 8 digits; a stray character or a missing digit produces a different shade or no shade at all. Second, decide whether the trailing pair is opacity, because a six-digit hex code is always fully opaque. Third, keep the casing consistent: e8590c and E8590C are the same color, but mixed casing in a shared file makes review harder.
The converter handles the same checks for you, and its UIColor format uses four decimals, which is more than the eye can tell apart on a Retina display. If you are processing many hex strings to UIColor values at once, such as a whole palette from a design file, run the Scanner based extension above in a loop instead of pasting each one. The Scanner reads each string in a single pass, so a palette of fifty entries costs nothing noticeable at launch. Teams that treat hex color to UIColor conversion as a one-time import, rather than something typed by hand, rarely ship an off-brand shade.
Store each converted UIColor as a named constant and reuse it. The same logo orange has to match across digital design files, the marketing site and the app, and web design teams usually quote only the hex value, so one converted branding constant keeps the shade identical wherever it appears.
Where This Hex Converter Fits in a Design Workflow
Designers hand over hex, and your app needs UIColor. Keeping the exact shade from the mockup to the shipped mobile app is a matter of accuracy, and the converter is the step in between.
- Design tools: Figma and Sketch export hex values, which you convert to UIColor with no manual math.
- Brand colors: keep one converted constant per shade so your style guides and your app stay in sync.
- Cross-platform development: the same hex serves web development, Android and iOS, so every team converts from one source of truth.
- Prototyping and debugging: paste a suspect hex to confirm the UIColor precision before you chase a rendering bug.
- Themes and gradients: convert each light and dark variant, or each stop in smooth gradients, once, and your themed UI and animations reuse the results.
- Collaboration: designers and developers can discuss the same hex and still get identical UIColor values.
Color shapes emotions on screen, so a consistent iOS design starts from one source. Store converted values in one place, whether that is a Swift file, an asset catalog or a remote database, so a brand refresh means changing one line. A pixel-perfect result depends on every screen reading that same source.
Related Conversions: Hex to HSV and Hex to CMYK
Sometimes the hex is only the starting point. Use hex to HSV when you want to adjust hue, saturation or brightness without touching the RGB numbers, and hex to CMYK when a print team needs ink values. HSL is the choice for CSS color functions. Each format describes the same shade in a different color space, and UIColor can also be built from hue, saturation and brightness, so keep the hex as your master and derive the UIColor or any other format from it.