GamutDesk Logo
GamutDesk

HEX to SwiftUI Color Code Generator

Turn any HEX color into SwiftUI Color(.sRGB, red:, green:, blue:, opacity:) code, plus a UIKit UIColor snippet. Free, instant and processed locally.

Client-Side ArchitecturePrivate Client-Side Processing

Color

100%

Generated code

SwiftUI
Color(.sRGB, red: 0.231, green: 0.51, blue: 0.965, opacity: 1.0)
UIKit (UIColor)
UIColor(red: 0.231, green: 0.51, blue: 0.965, alpha: 1.0)

Technical Specification & Mathematical Principles

How the SwiftUI Color Code Generator Works

SwiftUI expects each channel as a floating point value from 0 to 1. The generator divides each 8-bit channel by 255, so #3B82F6 becomes red 0.231, green 0.51 and blue 0.965. The .sRGB argument tells SwiftUI the values are in the sRGB color space rather than the extended or Display P3 space.

Opacity is passed as the final argument and is independent of the HEX digits. A UIKit equivalent, UIColor(red:green:blue:alpha:), is provided for projects that still bridge to UIKit.

Frequently Asked Questions

Why use Color(.sRGB, ...) instead of Color(red:green:blue:)?
The short initializer uses the default color space, which on recent OS versions may be treated as extended sRGB. Passing .sRGB explicitly keeps the on-screen color identical to your design file.
How many decimals are generated?
Channels are rounded to three decimals, which maps back to the exact 8-bit value, since the smallest step is 1/255 (about 0.004).
Can I use these in an Asset Catalog?
Asset catalogs store colors visually. Use the generated code for programmatic colors, or enter the same 0 to 1 values in the Color Set editor.

Related Design & Color Instruments