UIColor to Hex Converter

UIColor to Hex Converter. Paste your Swift UIColor Input values and the UIColor to Hex Converter instantly converts them into Hex Output. For checking octal literals found in code or documentation, the octal to decimal converter returns the decimal equivalent instantly, with no sign-up required.

Pulling a brand shade out of an app and into a stylesheet should take seconds, not a calculator session. The UIColor to Hex Converter takes the red, green, blue and alpha numbers from your iOS code and instantly returns a hex color code, so designers and developers can share one color code for CSS, design tools and every app screen. It works with Swift, Objective-C and Xamarin values.

How the UIColor to Hex Converter Turns RGBA Components Into a Hexadecimal Value

In iOS development, a UIColor stores four floating-point components: red, green, blue and alpha, each between 0 and 1. A hex value stores the same information as base-16 text. Each channel is scaled to the 0-255 range, rounded to a whole number and written as two hexadecimal digits. The result is a #RRGGBB string such as #1A2B3C. The fastest way to confirm your C# hex formatting is correct is to run the number through the int to hex c# and compare results.

That is the whole job of a uicolor to hexadecimal converter: scale, round, then write each channel in base-16. You never type the maths yourself, and the hex string you copy is ready for web and design work.

  • Input: the red, green, blue and alpha numbers from your UIColor initializer
  • Process: multiply by 255, round, convert each channel to two hex digits
  • Output: a #RRGGBB hex color, or #RRGGBBAA when alpha is below 1

The Formula for Converting UIColor to Hex in Swift

To convert a UIColor to a hex code, every channel follows the same rule. For a component c between 0 and 1, the 8-bit channel value is:

$$\text{channel} = \text{round}(c \times 255)$$

Split that channel into a high and a low digit to get its two hex characters:

$$\text{hex} = 16 \times d_{1} + d_{2}, \quad d_{1} = \lfloor \text{channel} \div 16 \rfloor, \; d_{2} = \text{channel} \bmod 16$$

Decimal digits above 9 are written as A through F, so 14 becomes E and 10 becomes A. Joining the red, green and blue pairs behind a hash gives the final hex code.

Converting UIColor to Hex Inside Your Own Code

If you would rather keep the logic in your project, a short extension does the same thing. It reads the four CGFloat components and formats them as two-digit uppercase pairs:

extension UIColor {
    func toHexString(withAlpha: Bool = false) -> String? {
        var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
        guard getRed(&r, green: &g, blue: &b, alpha: &a) else { return nil }
        let rgb = [r, g, b].map { Int(($0 * 255).rounded()) }
        let base = String(format: "#%02X%02X%02X", rgb[0], rgb[1], rgb[2])
        return withAlpha ? base + String(format: "%02X", Int((a * 255).rounded())) : base
    }
}

Call toHexString() on any color and the method returns nil when the color lives in a color space that cannot express RGB, such as a pattern image. On a plain RGB color it gives the same answer as the converter above, so you can convert UIColor to hex both online and at runtime.

Worked Example: Converting a UIColor to a Hex Color Code

Take a calm green defined as UIColor(red: 0.18, green: 0.62, blue: 0.47, alpha: 1.0). Each channel is scaled by 255 and rounded, then written in base-16:

Worked Example: Converting a UIColor to a Hex Color Code
ChannelComponent× 255RoundedHex pair
Red0.1845.90462E
Green0.62158.101589E
Blue0.47119.8512078

Joined together, the hex value is #2E9E78. Because alpha is 1.0, the color is fully opaque and no alpha pair is added. Paste it straight into CSS as color: #2E9E78; or into a Figma fill and you get the identical shade.

Formula card and bars showing UIColor components 0.18, 0.62 and 0.47 scaled by 255 into bytes 46, 158 and 120, written as hex pairs 2E, 9E and 78
Each UIColor channel is scaled by 255, rounded and written as a two-digit hex pair.

Rounding or Truncating: Why a Hex Color Can Be Off by One

When you convert a UIColor to hex in Swift, some snippets cut the scaled component's decimals off with Int() instead of rounding. For the green above, truncation turns 45.90 into 45 and 119.85 into 119, producing #2D9E77 instead of #2E9E78. The two shades differ by a single step in two channels, which is invisible on screen but enough to fail an exact match in a design review or a snapshot test.

Rounding to the nearest integer is the safer default, because it is the inverse of dividing by 255 when you convert back. The converter rounds each UIColor component, so its output round-trips cleanly and may differ from a truncating snippet.

Matching an iOS Button Color With a UIColor to Hexadecimal Converter

Priya is porting an iOS checkout button to the web and needs its hex color code for a stylesheet. The Swift file defines the fill as UIColor(red: 0.847, green: 0.263, blue: 0.431, alpha: 1.0), and nobody can find the original design file.

She types 0.847, 0.263 and 0.431 into the converter. Scaling by 255 gives 215.985, 67.065 and 109.905, which round to 216, 67 and 110. The hex value comes back as #D8436E, with alpha 1.0 so no alpha pair is appended.

Before pasting it into CSS, she checks the white button label against it. The WCAG 2.1 AA threshold for normal text is a 4.5:1 contrast ratio, and #D8436E scores only 4.24:1, so the label falls short. The converted hex value tells her to adjust the UIColor components and convert again.

  • She darkens the three components to 0.78, 0.20 and 0.36 and reruns the conversion.
  • The converter returns #C7335C, and white text on it measures 5.18:1, clearing the 4.5:1 AA bar.
  • She updates the Swift color and the CSS variable to the same hex value, so app and web match again.

The whole check took two conversions and one contrast lookup, and it ended with a single hex code both teams could cite.

Hex to UIColor: Reverse Conversion in Swift and Objective-C

Going the other way is just as common. A hex to uicolor conversion splits the string into pairs, reads each pair as a number from 0 to 255 and divides by 255. Take #2E9E78: 2E is 46, 9E is 158 and 78 is 120, giving roughly 0.180, 0.620 and 0.471.

  • In Swift, write a failable initializer that returns nil when the string is malformed.
  • In Objective-C, a category on the UIColor class, with a method such as colorWithHex:, accepts a 32-bit integer and builds the color for you, and a CSS string helper can accept text like #2E9E78.
  • In Xamarin, use the framework color helpers and pass the same red, green and blue numbers.

When you need a hex color to UIColor result without writing a parser, a hex to uicolor converter does the splitting for you. To convert a hex color to UIColor in code, accept the #RGB shorthand too: each digit is doubled, so #3A9 expands to #33AA99. Always strip the optional hashmark prefix before scanning, and let the scanner reject anything that is not a valid hex digit.

Which Hex Strings Are Valid

A UIKit parser should accept three lengths: 3 digits for #RGB, 6 for #RRGGBB and 8 for #RRGGBBAA. Anything else, such as five digits, should return nil instead of guessing. A default fallback color is fine for a UI, but silently hiding a typo makes bugs hard to trace.

How a Scanner Parses a Hex String Into a UIColor

To parse the text yourself, a Scanner reads the digits as one 64-bit number, and shifts and masks pull out each byte. This version is a convenience init that rejects anything but six digits:

extension UIColor {
    convenience init?(hexString: String) {
        var cleaned = hexString.trimmingCharacters(in: .whitespaces)
        if cleaned.hasPrefix("#") { cleaned.removeFirst() }
        let scanner = Scanner(string: cleaned)
        var value: UInt64 = 0
        guard cleaned.count == 6, scanner.scanHexInt64(&value) else { return nil }
        let r = CGFloat(UInt8((value >> 16) & 0xFF)) / 255
        let g = CGFloat(UInt8((value >> 8) & 0xFF)) / 255
        let b = CGFloat(UInt8(value & 0xFF)) / 255
        self.init(red: r, green: g, blue: b, alpha: 1)
    }
}

The scanner stops at the first character it cannot read, which is why the length check comes first. Any hexString with a stray letter fails the scan and returns nil, so a bad value never turns into a surprise color.

Alpha, Transparency and the #RRGGBBAA Hex Format

UIColor carries an alpha component for opacity, and a plain six-digit hex code cannot hold it. Eight-digit hex adds two more digits for the alpha component. The same 0.8 alpha used on our green becomes 0.8 × 255 = 204, written as CC, so the color is #2E9E78CC in CSS-style RRGGBBAA order.

Be careful with channel order. CSS and most web tools put alpha last, while many Apple-side snippets put it first (AARRGGBB), which would read the same color as #CC2E9E78. Check which order your destination expects before you paste. When a tool only supports six digits, keep the transparency in a separate rgba() value, because opacity does not survive in a six-digit hex string.

Bar splitting the 0-255 alpha range at 204, the hex pair CC, for a UIColor alpha of 0.8
An alpha of 0.8 becomes the hex pair CC in an eight-digit hex color.

When to Convert a UIColor to a Hex Color Code

Designers usually hand over hex values, while developers write RGBA numbers in UIKit and SwiftUI. A converter closes that gap in seconds.

  • Brand guidelines: keep brand colors identical across iOS, web and Android.
  • Figma and Sketch: paste hex into fills and compare them with the values in your Xcode project.
  • Debugging and prototyping: read the color from a running view, then reuse it in HTML or CSS.
  • Cross-platform consistency: reuse one hex code in browsers, devices and design frameworks.
  • Migration: move an old Interface Builder palette into named colors in an asset catalog without eyeballing swatches.

Converting UIColor values to a hexadecimal color keeps a mobile app and its web design on the same shade. Programmers and web development leads can write the hex color codes once in a style guide, then check each browser and device build for compatibility and UI consistency across apps, whether the accent is a gold highlight or the colour of a button. A digital handoff then starts from one value, and both teams reuse the same hex codes.

Doing this by copy and paste also supports pixel-perfect handoff: the tool shows a preview next to the code, so a wrong digit is obvious before it reaches the App Store build.

Related Color Conversions: RGB, HSV, HSL and CMYK

Hex is only one way to write a color. The same RGB color space can be shown as HSV, HSL or CMYK when you need to adjust hue, saturation or brightness. A hex to HSV step is handy for generating lighter and darker shades of one brand color, while CMYK matters for print. Whichever format you choose, the UIColor to Hex Converter starts from the same three channel values, so your shade stays the same across tools.