Hex to UIColor Converter
Convert a hex color like #2563EB into Swift UIColor(red:green:blue:alpha:) syntax with normalized 0.0-1.0 float components, ready to paste into Xcode.
How Hex to UIColor Conversion Works
UIKit's UIColor initializer expects red, green, blue, and alpha as normalized floats between 0.0 and 1.0, not the 0-255 integers hex codes pack. This tool splits the hex code into its three channels, divides each by 255, and formats the result to 3 decimal places as a ready-to-paste UIColor(...) call.
Hex to UIColor Example, Step by Step
#2563EB = UIColor(red: 0.145, green: 0.388, blue: 0.922, alpha: 1.000)
#2563EB (hex) = UIColor(red: 0.145, green: 0.388, blue: 0.922, alpha: 1.000)
Split into pairs: 25 | 63 | EB 25 (hex) = 37 -> 37 / 255 = 0.145 63 (hex) = 99 -> 99 / 255 = 0.388 EB (hex) = 235 -> 235 / 255 = 0.922
| Step | Description | Result |
|---|---|---|
| Split the hex code | #2563EB -> 25, 63, EB | 3 byte pairs |
| Convert to decimal | 25, 63, EB -> 37, 99, 235 | 37, 99, 235 |
| Normalize each channel | divide by 255 | 0.145, 0.388, 0.922 |
| Format as UIColor | add alpha: 1.000 | UIColor(red: 0.145, green: 0.388, blue: 0.922, alpha: 1.000) |
Alpha is always 1.000 for a plain hex conversion since hex codes carry no transparency information on their own.
Common Mistakes With Hex to UIColor
- Passing 0-255 integers directly into UIColor's red:green:blue: parameters instead of normalized 0.0-1.0 floats.
- Forgetting the alpha: parameter, which UIColor's designated initializer requires.
- Rounding channel values too aggressively, which can produce a visibly different color at low precision.
Frequently Asked Questions
Why does UIColor use 0.0-1.0 instead of 0-255?
iOS and macOS UIKit's UIColor initializer takes normalized float components between 0.0 and 1.0 rather than 0-255 integers. This tool divides each RGB channel by 255 and formats the result to 3 decimal places.
What's the UIColor value of #2563EB?
UIColor(red: 0.145, green: 0.388, blue: 0.922, alpha: 1.000) — each channel divided by 255 and rounded to 3 decimals.
Can I paste this directly into Swift code?
Yes, the output is valid Swift syntax for the UIColor initializer and can be pasted directly into a Swift or Objective-C-bridged file.
Does the alpha value ever change?
This tool always outputs alpha: 1.000 (fully opaque) since a plain hex code has no alpha channel of its own.
Is there precision loss from rounding to 3 decimals?
It's negligible for display purposes — 3 decimal places is precise enough that the resulting color is visually indistinguishable from the original hex value.