HEX to Swift UIColor Converter
Convert any HEX color to Swift UIKit, SwiftUI, and NSColor code with alpha. Copy-ready output for iOS, macOS, and SwiftUI projects.
HEX input
Alpha
Saved swatches
No saved swatches yet. Save a color to compare HEX, RGB, and alpha across multiple shades.
What is a HEX to Swift UIColor converter?
A HEX to Swift UIColor converter turns a CSS-style HEX color into a Swift UIColor, Color (SwiftUI), or NSColor initializer. UIKit has no built-in HEX initializer, so the converter splits the HEX into red, green, and blue components, divides each by 255, and writes the exact UIColor(red:green:blue:alpha:) call you can paste into your project. It also outputs SwiftUI's Color(.sRGB, ...), macOS's NSColor(srgbRed:...), and a reusable UIColor(hex:) extension.
Key takeaways
- UIKit has no native HEX initializer — you build UIColor from RGB floats.
- SwiftUI's
Colorhas no HEX initializer either; use RGB floats or a helper extension. - This tool copies UIKit, SwiftUI, AppKit, and helper extensions.
- Alpha is included automatically in every output.
- Values are in sRGB, so the rendered color matches design tools.
- Supports 3-, 6-, and 8-digit HEX, with or without the leading hash.
How to use the HEX to Swift UIColor converter
Every output updates live. Copy any of the five blocks straight into Xcode.
- Enter your HEX colorType or paste a HEX code in the input, or use the color picker. Both 3-digit and 6-digit HEX are supported, with or without the leading hash.
- Adjust alpha if neededDrag the alpha slider or enter a value from 0 to 1. Every Swift output updates to include the alpha component.
- Choose a target frameworkUIKit, SwiftUI, and AppKit output are shown side by side. Each format is copy-ready and framework-correct.
- Copy the Swift codeSelect Copy on any format. The tool copies clean Swift that compiles without editing, including the alpha parameter and whitespace.
- Use the extension snippetIf you prefer a reusable helper, copy the UIColor(hex:) or Color(hex:) extension. Drop it into your project once and call it from anywhere.
- Save and compare colorsSave colors to a strip to compare how the same HEX looks in your app under different alpha values, then export the strip as JSON.
HEX to UIColor, SwiftUI, and NSColor
Every iOS and macOS developer faces the same conversion. The differences are small but matter for correctness.
| Framework | Type | Initializer | Notes |
|---|---|---|---|
| UIKit (iOS) | UIColor | UIColor(red:green:blue:alpha:) | Floats 0–1, sRGB color space by default |
| SwiftUI | Color | Color(.sRGB, red:green:blue:opacity:) | Floats 0–1, explicit sRGB needed for accuracy |
| SwiftUI (from UIColor) | Color | Color(uiColor: uiColor) | Reuse an existing UIColor |
| AppKit (macOS) | NSColor | NSColor(srgbRed:green:blue:alpha:) | Available macOS 10.12+ |
| AppKit (calibrated) | NSColor | NSColor(calibratedRed:green:blue:alpha:) | Uses the calibrated color space instead of sRGB |
Why you need an extension
Because UIKit and SwiftUI do not accept HEX strings, most iOS projects end up with a small helper extension. Two flavors cover almost every case:
- UIColor(hex:) — parses a HEX string at runtime. Handles 3-, 6-, and 8-digit HEX with a single init. This tool copies a complete implementation.
- Color(hex:) — SwiftUI helper that internally builds the same UIColor and wraps it with
Color(uiColor:). Convenient inside SwiftUI views.
Choose inline values for small projects and the extension for larger ones. If you prefer no runtime parsing, use the ColorTokens struct output from this tool, which stores UIColor(red:green:blue:alpha:) literals in static properties.
Understanding sRGB vs P3
UIColor's default color space is sRGB, which matches HEX values and most design tools. If you want wide-gamut color, use UIColor(displayP3Red:green:blue:alpha:) with P3 values, or Color(.displayP3, red:green:blue:opacity:) in SwiftUI. P3 colors look more vivid on capable displays but render differently than the original HEX, so this tool outputs sRGB by default.
Using colors in your project
| Approach | Best for | Trade-off |
|---|---|---|
| Inline UIColor(red:...) | Small projects, prototypes, one-off colors | No central palette; hard to update later |
| Asset catalog named colors | Design-heavy apps with many screens | Access via UIColor(named:), but requires Xcode assets |
| ColorTokens struct | Design systems with a shared palette file | One file to maintain |
| UIColor(hex:) extension | Projects with HEX-based design specs | Tiny parsing cost per color; add once |
Common mistakes
- Forgetting to divide by 255. UIColor takes floats from 0 to 1, not 0 to 255.
- Using
displayP3for HEX colors. HEX is sRGB. Converting to P3 changes the rendered color on wide-gamut displays. - Rounding alpha to 0 or 1. A tiny float, like 0.98, is not the same as 1.0 when the color is composited.
- Hand-parsing 8-digit HEX without alpha. The last two characters are alpha, not blue.
- Hard-coding colors everywhere. Centralize in a ColorTokens file or asset catalog so a rebrand takes minutes, not days.
Frequently asked questions
How do I convert HEX to UIColor in Swift?
Split the HEX string into red, green, and blue components, divide each by 255, and pass the floats to UIColor(red:green:blue:alpha:). For example, #FF5733 becomes UIColor(red: 1.0, green: 0.341, blue: 0.2, alpha: 1.0). This converter does that calculation for you and outputs the exact line to paste.
How do I use HEX in SwiftUI?
SwiftUI's Color has no HEX initializer, so you either use Color(.sRGB, red:green:blue:opacity:) with float components, use a UIColor and wrap it as Color(uiColor:), or use a Color(hex:) extension. This tool outputs all three forms so you can pick the one that fits your project.
What is the UIColor initializer for HEX colors?
There is no HEX initializer in UIKit. You build the color from RGB floats: UIColor(red: r/255, green: g/255, blue: b/255, alpha: a). This converter outputs that exact call with correctly rounded values.
Does SwiftUI Color support HEX?
No. SwiftUI's Color type supports RGB floats through Color(.sRGB, red:green:blue:opacity:) but not HEX strings. You either convert HEX to RGB floats, wrap a UIColor, or add a Color(hex:) extension to your project. This tool provides all three.
How do I handle alpha in HEX colors?
HEX colors sometimes include alpha as an 8th digit (#RRGGBBAA). This tool reads 8-digit HEX and sets the alpha slider accordingly. For 6-digit and 3-digit HEX, alpha defaults to 1.0 and you can change it with the slider.
What is the difference between UIColor and Color in Swift?
UIColor is the UIKit color type, used in SwiftUI only through Color(uiColor:). Color is the SwiftUI native type and is used directly in SwiftUI views. NSColor is the AppKit type for macOS. All three represent the same color but have different APIs.
Can I use this for macOS NSColor?
Yes. The tool outputs NSColor using NSColor(srgbRed:green:blue:alpha:) so it works on macOS 10.12 and later with correct color space. For NSColor with a calibrated space, use NSColor(calibratedRed:green:blue:alpha:).
Why does my UIColor look different from the HEX in my design tool?
The most common cause is color space. UIColor defaults to sRGB when you use UIColor(red:green:blue:alpha:), which is correct. But if you display the color on a P3 display without specifying sRGB, or if you use UIColor(displayP3Red:...), the rendered color will be more saturated. This tool outputs sRGB values so colors match design tools by default.
How do I make a UIColor extension for HEX?
Add an extension on UIColor with an init(hex:) that trims the hash, reads the hex integer, and extracts red, green, blue, and alpha components. The tool provides a ready-to-paste extension that handles 3, 6, and 8 digit HEX.
What is the best way to store colors in Swift?
For small projects, use UIColor(red:green:blue:alpha:) with literal values. For larger projects, use an asset catalog with named colors, or a ColorPalette enum with static properties. The converter shows both the inline form and a struct you can paste into a ColorTokens file.
Is the HEX to Swift UIColor converter free?
Yes. The tool is free, needs no account, and runs entirely in your browser. Your colors are never uploaded.
Does this tool work for React Native or Flutter?
It outputs Swift only. For React Native, use the HEX value directly in StyleSheet since it accepts HEX strings. For Flutter, convert to ARGB with the same hex values. The HEX and RGB outputs in this tool will help you there.