Hex to Swift UIColor Converter
Convert hex color codes to Swift UIColor, SwiftUI Color, and Objective-C format instantly. Supports 3, 4, 6, and 8-digit hex values with live preview.
UIColor(red: 0.204, green: 0.596, blue: 0.859, alpha: 1.000)
About
Hardcoding raw hex strings in iOS projects leads to runtime crashes when the format is wrong and makes color management across large codebases nearly impossible to maintain. This converter parses hex color codes - including 3-digit shorthand, 6-digit standard, and 8-digit with alpha - and produces the exact UIColor, SwiftUI Color, or Objective-C initializer call with normalized floating-point components in the range 0.0 - 1.0. Each channel value is computed as channelInt255 and rounded to three decimal places. The tool assumes sRGB color space, which matches Appleβs default UIColor initializer behavior.
Limitation: this tool does not account for wide-gamut Display P3 colors. If your design assets use a P3 profile, the output values will be sRGB approximations. For projects targeting CGColor with custom color spaces, additional conversion is required beyond what this tool provides.
Formulas
Each hex color channel is a base-16 pair representing an integer from 0 to 255. The normalized floating-point value used by UIColor is computed as:
For 3-digit shorthand (#RGB), each character c is expanded to cc. For example, #F80 becomes #FF8800. Similarly, 4-digit (#RGBA) expands to 8-digit (#RRGGBBAA).
Where R = red channel, G = green channel, B = blue channel, A = alpha (opacity). If no alpha pair is present, A defaults to 1.000. All values are rounded to 3 decimal places via Math.round(val Γ 1000) Γ· 1000.
Reference Data
| Hex Format | Example | Expansion | Red | Green | Blue | Alpha | Swift UIColor |
|---|---|---|---|---|---|---|---|
| #RGB | #F80 | #FF8800 | 1.000 | 0.533 | 0.000 | 1.000 | UIColor(red: 1.000, green: 0.533, blue: 0.000, alpha: 1.000) |
| #RGBA | #F80A | #FF8800AA | 1.000 | 0.533 | 0.000 | 0.667 | UIColor(red: 1.000, green: 0.533, blue: 0.000, alpha: 0.667) |
| #RRGGBB | #3498DB | - | 0.204 | 0.596 | 0.859 | 1.000 | UIColor(red: 0.204, green: 0.596, blue: 0.859, alpha: 1.000) |
| #RRGGBBAA | #3498DB80 | - | 0.204 | 0.596 | 0.859 | 0.502 | UIColor(red: 0.204, green: 0.596, blue: 0.859, alpha: 0.502) |
| #RRGGBB | #E74C3C | - | 0.906 | 0.298 | 0.235 | 1.000 | UIColor(red: 0.906, green: 0.298, blue: 0.235, alpha: 1.000) |
| #RRGGBB | #2ECC71 | - | 0.180 | 0.800 | 0.443 | 1.000 | UIColor(red: 0.180, green: 0.800, blue: 0.443, alpha: 1.000) |
| #RRGGBB | #9B59B6 | - | 0.608 | 0.349 | 0.714 | 1.000 | UIColor(red: 0.608, green: 0.349, blue: 0.714, alpha: 1.000) |
| #RRGGBB | #1ABC9C | - | 0.102 | 0.737 | 0.612 | 1.000 | UIColor(red: 0.102, green: 0.737, blue: 0.612, alpha: 1.000) |
| #RRGGBB | #F39C12 | - | 0.953 | 0.612 | 0.071 | 1.000 | UIColor(red: 0.953, green: 0.612, blue: 0.071, alpha: 1.000) |
| #RRGGBB | #ECF0F1 | - | 0.925 | 0.941 | 0.945 | 1.000 | UIColor(red: 0.925, green: 0.941, blue: 0.945, alpha: 1.000) |
| #RRGGBB | #34495E | - | 0.204 | 0.286 | 0.369 | 1.000 | UIColor(red: 0.204, green: 0.286, blue: 0.369, alpha: 1.000) |
| #RRGGBB | #000000 | - | 0.000 | 0.000 | 0.000 | 1.000 | UIColor(red: 0.000, green: 0.000, blue: 0.000, alpha: 1.000) |
| #RRGGBB | #FFFFFF | - | 1.000 | 1.000 | 1.000 | 1.000 | UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000) |
| #RGB | #000 | #000000 | 0.000 | 0.000 | 0.000 | 1.000 | UIColor(red: 0.000, green: 0.000, blue: 0.000, alpha: 1.000) |
| #RGB | #FFF | #FFFFFF | 1.000 | 1.000 | 1.000 | 1.000 | UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000) |