HEX to SwiftUI and UIColor Converter (plus RGB, HSL)

Type a colour in HEX, rgb() or hsl() and copy it as SwiftUI Color, UIColor, NSColor, Android XML or Jetpack Compose code. It also checks the WCAG contrast between two colours.

Colour converter

Values and ready-to-copy code

To use Color(hex: 0x3366CC) in SwiftUI, add this extension once:

extension Color {
    init(hex: UInt32, opacity: Double = 1) {
        self.init(
            .sRGB,
            red: Double((hex >> 16) & 0xFF) / 255,
            green: Double((hex >> 8) & 0xFF) / 255,
            blue: Double(hex & 0xFF) / 255,
            opacity: opacity
        )
    }
}

WCAG contrast checker

4.5:1
Large sample text
This is normal sample text to see how readable this combination is.

🔒 Everything is calculated in your browser. No data is sent to any server.

Using a HEX colour in SwiftUI

SwiftUI has no built-in initialiser for HEX strings. Add this extension once to your project:

extension Color {
    init(hex: UInt, opacity: Double = 1) {
        self.init(
            .sRGB,
            red: Double((hex >> 16) & 0xFF) / 255,
            green: Double((hex >> 8) & 0xFF) / 255,
            blue: Double(hex & 0xFF) / 255,
            opacity: opacity
        )
    }
}

let brand = Color(hex: 0x3366CC)

Using it in UIKit

let brand = UIColor(red: 0.2, green: 0.4, blue: 0.8, alpha: 1)

For colours you reuse across the app, consider adding them to the Asset Catalog as Color Sets: you get a name, a dark mode variant and Display P3 support for free.

WCAG contrast

Body text needs a contrast ratio of at least 4.5:1 (AA) and large text 3:1. The checker shows whether your text and background colours pass AA and AAA.

Frequently asked questions

Why does my colour look different on the device?

Check the colour space: the code uses sRGB. If your designer works in Display P3, use Color(.displayP3, …) or an Asset Catalog colour.

Is anything sent to a server?

No. All conversions happen in your browser.

¿Prefieres español? Esta herramienta en español.