Swift & SwiftUI Color Converter

HEX to SwiftUI Color Converter

Translate hex color codes (e.g. #FF7A1A) into native SwiftUI Color extensions, normalized RGB fractions, and UIKit UIColor definitions.

Presets:
RGB: 255, 122, 26Alpha: 100%
Contrast vs White: 2.6:1 ✕ Fail
vs Black: 8.1:1 ✓ AA
Inline SwiftUI Color
Color(red: 1.000, green: 0.478, blue: 0.102)
Semantic Color Extension
extension Color {
    static let brandAccent = Color(
        red: 1.000,
        green: 0.478,
        blue: 0.102
    )
}
Color(hex: "...") Reusable Initializer
// Reusable hex initialiser for SwiftUI
extension Color {
    init(hex: String) {
        let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int: UInt64 = 0
        Scanner(string: hex).scanHexInt64(&int)
        let a, r, g, b: UInt64
        switch hex.count {
        case 3: // RGB (12-bit)
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (255, 0, 0, 0)
        }
        self.init(
            .sRGB,
            red: Double(r) / 255,
            green: Double(g) / 255,
            blue: Double(b) / 255,
            opacity: Double(a) / 255
        )
    }
}

// Usage:
let color = Color(hex: "FF7A1A")
UIKit UIColor Equivalent
UIColor(red: 1.000, green: 0.478, blue: 0.102, alpha: 1.00)

Color Handling in Modern SwiftUI

0.0 to 1.0 Floating Fractions

Unlike CSS which accepts 0–255 integer ranges, Apple’s Color(red:green:blue:) initializer requires normalized Double values between 0.0 and 1.0.

Wide Gamut Display P3

OLED iPhone displays support Apple Display P3 color space with 25% richer color saturation than standard sRGB monitors.

Semantic Extensions

Adding static properties to extension Color enables auto-completion in Xcode like .foregroundStyle(.brandAccent).

Thorous icon

Build full iPhone apps without touching Xcode

Enjoying this free Color Converter? Thorous turns your natural language prompts into compiled native SwiftUI apps, with real-time iOS simulator streaming and visual on-screen editing.

Native Mac app · macOS 14 Sonoma or later · Apple silicon & Intel